在bash shell中,(solaris 5.8上的bash ver可能是舊的), 使用awk或sed我將如何將行合併爲一個「重複」模式之間的行:在shell中如何將兩個字符串之間的行整合成一行
[編輯:更好地解釋自己)
我的文件中包含這樣的一個條目很多:
my-group<--------------------------(main entry)
<tab>group-code<spcaes>AXZ1<-------(sub-section under main entry)
<tab>description
<tab>state<spaces>CA
<tab>items
<tab><spaces>item_value_1
<tab><spaces>item_value_2
<tab><tab>header_3 <---------------(sub-section under sub-section) (can have upto 5th level)
<tab><tab>header_3_item_1<spaces>vlaue
我希望這可以變成: 新行頭開始每次該行的列包含字母數字值。當它沒有的時候,它應該被附加爲: -
所有的TAB被一個「|」替代。以及由「」分隔的參數和值:
my-group|group-code:AXZ1|description:|state:CA|items:something:something2|last-member-name:XYZ
my-group|group-code:PORTU1|description:|state:CT|items:something:something2|last-member-name:FQRTZ
我該怎麼做?我唯一能想到的方式就是在內存中打開文件並逐行讀取並執行操作。是唯一的方法還是可以有一個sed/awk命令?
我在這裏把我試圖實現這個bash代碼。 (還沒有工作)
#!/bin/bash
myFile=$1
function trim()
{
local [email protected]
var=$(echo $var|sed -e "s/^\s*//" -e "s/\s*$//" -e "s/[ \t]/:/g")
echo -n "$var"
}
newLine=''
i=0
while read line
do
i=$[i + 1]
[ -z "$line" ] && continue
if [[ $line =~ ^[[:alnum:]] ]] <-----this is not working....matching every line
then
newLine=$(trim "$line")
match="matched ^a-zA-Z0-9"
elif [[ $line =~ ^[[:space:]] ]]
then
line="$(trim "$line")"
newLine="${newLine}|${line}"
match="matched ^tab/space"
fi
echo -e "line number=$i match=$match line=$line new-ine value-->"$newLine"<--"
echo
done < $myFile
ty。
我們應該如何知道「某物」是關鍵還是值? 「描述」顯然沒有任何價值,但是「物品」的確如此,當兩者都是唯一的詞。空白有多重要? – 2014-09-05 01:09:30