Sed / grep / awk ?: add matching pattern to end of line
I have a file like
1,ab012a800,20141205 2,ab023a801,20141205 3,ab012a802,20141205 1,ab024a803,20141205 1,ab012a804,20141205
I want to extract the "ab012a" part and add it to the end of the line.
1,ab012a800,20141205,ab012a 2,ab023a801,20141205,ab023a 3,ab012a802,20141205,ab012a 1,ab024a803,20141205,ab024a 1,ab012a804,20141205,ab012a
I can extract with grep:
grep -o '^[a-z][a-z][0-9]*[a-z]' file
and add a line with sed:
sed "s/$/,whatever/"
or even replace the sed pattern:
sed '/^[a-z][a-z][0-9]*[a-z]/ s/$/something/' file
but how would I add the appropriate pattern to the end of the line?
Many thanks
+3
source to share
7 replies
You can use:
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
&
is a special replacement character that represents a complete string using a regular expression, and \1
represents the # 1 matched group.
+4
source to share
GAWK method
awk 'match($0,/[a-z][a-z][0-9]+[a-z]/,a){print $0","a[0]}' file
Matches string, then prints string and string with string
Alternative portable awk way (courtesy of EdMorton)
awk 'match($0,/[a-z][a-z][0-9]+[a-z]/{$0=$0","substr($0,RSTART,RLENGTH)}1' file
And with a character class for maximum portability
awk 'match($0,/[[:lower:]][[:lower:]][[:digit:]]+[[:lower:]]/{
$0=$0","substr($0,RSTART,RLENGTH)}1' file
+2
user3442743
source
to share