我有以下格式的文件:在文本文件中如何連接的話
B: that
I: White
I: House
B: the
I: emergency
I: rooms
B: trauma
I: centers
我需要做的是從頂部逐行讀取,如果符合B,則除去乙開始: 如果以I開頭:然後刪除I:並連接到前一個(前一個按照相同的規則處理)。
預期輸出:
that White House
the emergency rooms
trauma centers
我試了一下:
while read line
do
string=$line
echo $string | grep "B:" 1>/dev/null
if [ `echo $?` -eq 0 ] //if start with " B: "
then
$newstring= echo ${var:4} //cut first 4 characters which including B: and space
echo $string | grep "I:" 1>/dev/null
if [ `echo $?` -eq 0 ] //if start with " I: "
then
$newstring= echo ${var:4} //cut first 4 characters which including I: and space
done < file.txt
什麼我不知道的是如何把它回線(在文件中),以及如何連接行到以前處理過的一個。
是的,它的工作原理!這也是非常好的和簡短的答案,如果有關於參數的評論,會是完美的! – user1314404
增加了一些更多的敘述 - 希望有所幫助。 – suspectus