1

I have a csv file looks like this:

c1,c2,c3,http://aaa.com/blblbblb\nhttp://bbb.com/sdsdsds\nhttp://ccc.com\nhttp://foo.com/ghghghgh

cc1,cc2,cc3,http://eee.com/blblbblb\nhttp://foo.com/sdsdsds\nhttp://fff.com\nhttp://ttt.com/ghghghgh

ccc1,ccc2,ccc3,http://foo.com/blblbblb\nhttp://vvv.com/sdsdsds\nhttp://foo.com/nmnmnmnm\nhttp://qqq.com\nhttp://kkk.com/ghghghgh

is it possible manipulate above csv file and export as follows: (using sed or awk or similar bash commands)

c1,c2,c3,http://foo.com/ghghghgh 

cc1,cc2,cc3,http://foo.com/sdsdsds

ccc1,ccc2,ccc3,http://foo.com/blblbblb;http://foo.com/nmnmnmnm

Actually i want to manipulate only 4th column and Remain http://foo.com/{some string} pattern (in other words, extract links from 4th column when contain foo.com domain)

  • 1
    Do the \n mean 'newline' ? – Costas Oct 30 '16 at 9:07
  • yes \n mean 'newline'.but Is ineffective in csv file. – alrz Oct 31 '16 at 6:42
  • It is matter whether each http on separate line or not. What output produce cat file.csv ? – Costas Nov 1 '16 at 17:02
1
sed '
    s|http://foo.com|@|g #replace `foo.com` domain with rare symbol
    /./s/\\n\|$/;/g      #replace `\n` by `;`  and add it to end 
    s/http[^@]*;//g      #remove all domain(s) without `foo.com`
    s|@|http://foo.com|g #place `foo.com` back
    s/;$//               #remove `;` from the end of line
    ' csv.file
0

You can do the following:

cat your_csv.csv | sed 's/\\n/,/g' | cut -d ',' -f 4

sed will change all the \ns to , and cut chooses the 4th field when the delimiter is ,

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.