0

In csv file couple of lines dont fit the data I need. For example, this is my example.csv file:

14/Feb/2016:13:21:33-0500,mk
15/Feb/2016:14:21:33-0500,mk
16/Feb/2016:15:21:33-0500]http://map1.link.de/mk
17/Feb/2016:16:21:33-0500]http://map5.link.de/mk

I can fetch that lines and correct them with:

$ cat example.csv | grep "map" | sed 's/\(.*\)].*\/\(.*$\)/\1,\2/'

Result:

16/Feb/2016:15:21:33-0500,mk
17/Feb/2016:16:21:33-0500,mk

But what I really want is to correct them and save them like that in the same file. How can I do that?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

1

If you are using GNU Sed, just run:

sed -i '/map/s/\(.*\)].*\/\(.*$\)/\1,\2/' example.csv

Warning: This will overwrite the contents of the file. Make a copy first if you're not sure the regex is exactly what you want.


If you don't have GNU Sed, or you just want a more portable solution that will run on any POSIX system, use ex:

For a trial run without editing the file:

printf '%s\n' 'g/map/s/\].*\/\([^/]*\)$/,\1/' %p | ex example.csv

To save changes once you're happy with the result:

printf '%s\n' 'g/map/s/\].*\/\([^/]*\)$/,\1/' x | ex example.csv
Wildcard
  • 36,499