I have a file which is separated with 3 PIPES (|||). For e.g name|||url|||date|||amount|||

I am trying to do 2 things

  1. extract specific column in another file say - amount to be separated in new file (it is 4th column in file)

  2. replace all the 3 pipes with comma, so new files looks like - name,url,date,amount,

I want to replace comma with exactly 3 pipes as my data also has single pipe characters in between.

I have tried this - awk -F '[\|]'+ '{print $4}' temp.csv > temp1.csv. But this extracts if even single pipe is there in data.

Thanks in advance Rahul


Use [|]{3} as the field delimiter to extract a particular field from the original data:

$ awk -F '[|]{3}' '{ print $4 }' input.csv

To use a regular expression as the field separator requires GNU awk.

To replace all ||| with a comma:

$ sed 's/|||/,/g' input.csv >output.csv
  • thanks "Kusalananda" for quick reply. This solved the problem. – user3749706 Jan 23 '17 at 18:39

This should do the trick for extracting the fourth field of triple-pipe-delimited data:

sed 's/|||/,/g' /path/to/input | awk -F, '{print $4}'

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.