John, 1234567
Bob, 2839211
Alex, 2817821
Mary, 9371281
I am currently trying to retrieve the first column with the last 4 digits of the second column using sed, so the output should look like this:
John, 4567
Bob, 9211
Alex, 7821
Mary, 1281
This is my command: 's/\(.*,\)\(.*\)//'
, I think that this command matches the first column until the comma and the second column until the end, but I am unsure on how to continue.
You can use
sed 's/^\([^,]*\), *[0-9]*\([0-9]\{4\}\).*/\1, \2/' file
See the online demo.
Details
^
- start of string\([^,]*\)
- Group 1: any zero or more chars other than a comma, *
- a comma and zero or more spaces[0-9]*
- zero or more digits\([0-9]\{4\}\)
- Group 2: four digits.*
- the rest of the line\1, \2
- The replacement is: Group 1, ,
, space and Group 2 value.Just capture the last four digits of each line and delete any preceding digits:
$ sed 's/[0-9]*\([0-9]\{4\}\)$/\1/' input.txt
John, 4567
Bob, 9211
Alex, 7821
Mary, 1281
If using a version of sed
that supports POSIX Extended Regular Expressions, it can be cleaned up a bit to
sed -E 's/[0-9]*([0-9]{4})$/\1/' input.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With