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


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


source


You can use this one GNU awk

:

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

      

Output:

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

      

+1


source


With capture groups:

$ 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

      

0


source


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

      

without any other limitation and based on this sample ...

0


source


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

      

0


source


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

      

0


source







All Articles