我将以下输入分配给变量:
echo $var1
abc001: text goes here yyy003: text goes here uuuu004: text goes here
输出应该如下(没有冒号:)基本上我想在没有冒号的新行上打印以主机名开头的每个列表
abc001 text goes here
yyy003 text goes here
uuuu004 text goes here
答案 0 :(得分:1)
awk
版本:
awk '{for (i=1;i<=NF;i++) printf $i~":"?"\n"$i" ":$i" "}' <<< "$var1"| awk 'NF {sub(/:/,x);print}'
abc001 text goes here
yyy003 text goes here
uuuu004 text goes here
另一个版本,基于Fredriks正则表达式:
awk '{gsub(/[a-zA-Z]+[0-9]+/,"\n&");gsub(/:|^\n/,x)}1' <<< "$var1"
abc001 text goes here
yyy003 text goes here
uuuu004 text goes here
sed
版本:
sed -E 's/://;s/(\w+):/\n\1/g' <<< "$var1"
abc001 text goes here
yyy003 text goes here
uuuu004 text goes here
答案 1 :(得分:1)
使用GNU awk
的一种方式:
$ gawk 'NR>1{print host, $0}{host=RT}' RS='[[:alnum:]]+:' OFS='\b' <<< $var1
abc001 text goes here
yyy003 text goes here
uuuu004 text goes here
答案 2 :(得分:0)
或许这样的事情:
$ echo $var1
host1: text goes here host2: text goes here Host3:text goes here
$ sed -e 's/\([Hh]ost\)/\n\1/g' -e 's/://g' <<< $var1
host1 text goes here
host2 text goes here
Host3text goes here
更新
$ echo $var1
aaa1: text goes here bbbb0002: text goes here AAAA0012: text goes here
$ sed -e 's/\([a-zA-Z]\+[0-9]\+\)/\n\1/g' -e 's/://g' <<< $var1
aaa1 text goes here
bbbb0002 text goes here
AAAA0012 text goes here
答案 3 :(得分:0)
grep -Po '.*?(?=( [^: ]*:|$))' file|sed 's/://'
以你的例子:
kent$ echo "abc001: text goes here yyy003: text goes here uuuu004: text goes here"|grep -Po '.*?(?=( [^: ]*:|$))'|sed 's/://'
abc001 text goes here
yyy003 text goes here
uuuu004 text goes here