Insert digits into match - sed command help

Hi Everyone,

I need some help writing a sed command that will insert 19 in the following lines, the column with 6 digits is supposed to represent the date of birth. My goal is to insert 19 four digits in so the year will be 1988 and 1995 instead of just 88 and 95. The finished result should look like 03191988 for the first line and 07281995 for the second. Thanks for the help

Amy E. King        575-242-7304 Plymouth        031988   Art   2.93  65
Charles E. Jones   505-142-5757 Reno            072895   CS    1.21  96

Hi iXLnu,

Welcome to our forums!

Interesting task. Since we cannot predict the length of the full name and city we need to match group of digits. Just to make it harder I have added extra row to make sure that the solution works regardless of the full name and city length:

$ cat table.txt 
Amy E. King        575-242-7304 Plymouth        031988   Art   2.93  65
Charles E. Jones   505-142-5757 Reno            072895   CS    1.21  96
Lubos              505-142-5757 Foo Bar         050438   CS    1.21  96

The following sed command will do the trick:

Solution:
$ sed 's/\B[0-9][0-9]\{1\}\>/19&/4' table.txt

Output:

Amy E. King        575-242-7304 Plymouth        03191988   Art   2.93  65
Charles E. Jones   505-142-5757 Reno            07281995   CS    1.21  96
Lubos              505-142-5757 Foo Bar         05041938   CS    1.21  96

Hope this helps

Lubos

Thank you very much Lubos,

I’ve been trying to learn linux for a couple of months now and I just started using sed with sub-expressions which have my head spinning. I really appreciate the help!

I’m also trying to figure out working with these same lines (8 total) how to move the Last name over into the first spot, so far I have this disaster of a command line which only moves the top 3 lines over, and I had to add a space and a period after the second sub expression to fix the formatting. I also have them listed backwards because when I would try to start off the command with a space before [A-Z] to signify the space before the last name the command no longer works.

sed ‘s/([A-Z]…)([A-Z]). ([A-Z]…)*/\3\2. \1/’ students

I figured it out.
sed ‘s/([A-Z][a-z]* )([A-Z]). ([A-Z][a-z])/\3 \2. \1 /’ file name

Good to hear that you sorted it out! Feel free to create a new question/thread if you need additional help.

lubos