sed / grep / awk? :将匹配模式附加到行尾

时间:2014-12-11 11:26:09

标签: regex awk sed grep

我有一个文件,如

1,ab012a800,20141205
2,ab023a801,20141205
3,ab012a802,20141205
1,ab024a803,20141205
1,ab012a804,20141205

我想提取' ab012a'部分并将其附加到该行的末尾。

1,ab012a800,20141205,ab012a
2,ab023a801,20141205,ab023a
3,ab012a802,20141205,ab012a
1,ab024a803,20141205,ab024a
1,ab012a804,20141205,ab012a

我可以用grep提取:

grep -o '^[a-z][a-z][0-9]*[a-z]' file    

并附加到sed:

的行
sed "s/$/,whatever/"

甚至用sed替换模式:

sed '/^[a-z][a-z][0-9]*[a-z]/ s/$/something/' file

但是如何将匹配模式附加到行尾?

非常感谢

7 个答案:

答案 0 :(得分:4)

您可以使用:

sed -i.bak 's/\(,[a-z][a-z][0-9]*[a-z]\).*$/&\1/' file
1,ab012a800,20141205,ab012a
2,ab023a801,20141205,ab023a
3,ab012a802,20141205,ab012a
1,ab024a803,20141205,ab024a
1,ab012a804,20141205,ab012a

&是替换中的特殊符号,表示使用正则表达式的完全匹配字符串,\1表示匹配的组#1。

答案 1 :(得分:2)

GAWK方式

awk 'match($0,/[a-z][a-z][0-9]+[a-z]/,a){print $0","a[0]}' file

匹配字符串然后打印行和匹配的字符串

替代便携式awk方式(由EdMorton提供)

awk 'match($0,/[a-z][a-z][0-9]+[a-z]/{$0=$0","substr($0,RSTART,RLENGTH)}1' file

具有最大可移植性的字符类

awk 'match($0,/[[:lower:]][[:lower:]][[:digit:]]+[[:lower:]]/{
     $0=$0","substr($0,RSTART,RLENGTH)}1' file

答案 2 :(得分:1)

您可以使用此GNU awk

awk -F"," '{print $1","$2","$3"," gensub(/(.*)(...$)/, "\\1", "g", $2)}' FileName

输出

1,ab012a800,20141205,ab012a
2,ab023a801,20141205,ab023a
3,ab012a802,20141205,ab012a
1,ab024a803,20141205,ab024a
1,ab012a804,20141205,ab012a

答案 3 :(得分:0)

使用捕获组:

$ sed -r 's@^([0-9]+,)(ab[0-9]+[a-z]+)(.*)@\1\2\3,\2@g' file
1,ab012a800,20141205,ab012a
2,ab023a801,20141205,ab023a
3,ab012a802,20141205,ab012a
1,ab024a803,20141205,ab024a
1,ab012a804,20141205,ab012a

答案 4 :(得分:0)

sed 's/.\(.\{7\}\).*/&\1/' YourFile

没有任何其他约束并且基于此示例...

答案 5 :(得分:0)

perl -pe 's/(.{7})(.*)/$1$2,$1/' file

答案 6 :(得分:0)

awk '{print $1"," substr($0,3,6)}' file

1,ab012a800,20141205,ab012a
2,ab023a801,20141205,ab023a
3,ab012a802,20141205,ab012a
1,ab024a803,20141205,ab024a
1,ab012a804,20141205,ab012a