Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove specific number of digits using sed

Tags:

linux

sed

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.

like image 333
NixyCron Avatar asked Oct 14 '25 08:10

NixyCron


2 Answers

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.
like image 76
Wiktor Stribiżew Avatar answered Oct 16 '25 22:10

Wiktor Stribiżew


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
like image 22
Shawn Avatar answered Oct 16 '25 22:10

Shawn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!