在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