0

In line((s) that originate from csv file)

14/Feb/2016:15:21:33-0500]http://map1.link.de/mk

what is the easiest part to replace/delete&add

]http://map1.link.de/

with

,

having in mind that after map can come any number map1, map2, map3

Example of couple of lines:

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

Final result

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

I would use sed:

sed 's/\(.*\)].*\/\(.*$\)/\1,\2/' csv
14/Feb/2016:15:21:33-0500,mk
14/Feb/2016:16:21:33-0500,mk

Where csv is this file:

cat csv 
14/Feb/2016:15:21:33-0500]http://map1.link.de/mk
14/Feb/2016:16:21:33-0500]http://map5.link.de/mk


Explanation:
\(.*\)] = search and save every char until the first [
.*\/ = after that, search - do not save - everything until the last /
\(.*$\) = save everything after that until the end of line (mk)
\1,\2 = replace with what we saved and add a comma in between

  • Thanks! It works, so I am perfectly fine with sed. Can you please give me some sort of explanation, me and regex are not best friends! – newbie_girl Oct 18 '16 at 17:19
  • @newbie_girl No problem. Regular expressions are hard, but even harder to explain in non-geek words! – maulinglawns Oct 18 '16 at 17:26
0

You can express sequence of one or more decimal digits POSIXly as [0-9]\{1,\} or less portably using the extended regular expression operator + as [0-9]+ so either

sed 's@]http://map[0-9]\{1,\}\.link\.de/@,@' file.csv
14/Feb/2016:15:21:33-0500,mk
14/Feb/2016:16:21:33-0500,mk

or

sed -E 's@]http://map[0-9]+\.link\.de/@,@' file.csv
14/Feb/2016:15:21:33-0500,mk
14/Feb/2016:16:21:33-0500,mk

If you want to replace any URI (not just ones whose qualified hostname matches map?.link.de) then you can simplify the latter to

$ sed -E 's@]http://[^/]*/@,@' file.csv
14/Feb/2016:15:21:33-0500,mk
14/Feb/2016:16:21:33-0500,mk

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.