How can I rewrite lines in a txt file using grep [sed]?

I have a text file where each line has the format like this...

25126 Akers, David

And I'm needing to put it in a format like this to insert into a database...

25126;Akers, David;

Only problem is there might be some with names having spaces like...

25257 Ah You, C.J.

I was told I should use grep but I'm not sure how to go about doing it.

4

1 Answer

grep is not suitable for such a task, because it is a search tool, but if I understand correctly the problem, you can use sed, as in the following example:

sed 's/^\([0-9]\+\) \(.*\)$/\1;\2;/' input-file >output-file

To preventively check that all lines conform to the above pattern, run the following command

sed -n '/^\([0-9]\+\) \(.*\)$/!p' input-file

that should return nothing.

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like