0

I have a text file as follows:

aaaa 
bbbb
----  
cccc
----  
dddd

How can I use ---- as delimiter and take out for example dddd or cccc? By take out I mean run a command and get "dddd" if my input indicates I want the 3rd field of the whole file. It seems that awk or cut operates on only one line, so I couldn't think of any way to do this with these commands.

  • 1
    awk operates on records - by default the record separator (RS) is newline but you can set it to another value – steeldriver Jan 13 at 19:08
  • 3
    Can you edit your question and explain a little more about what you want to do. What do you mean by "take out"? It'd be helpful to know what you'd like the result to look like. – Andy Dalton Jan 13 at 19:42
0

You can use sed and csplit to do this.

sed -i.bak 's/----/-/g' file && csplit --suppress-matched file '/-/' '{*}'
  • sed will replace the "----" with a single "-" in file (and create a backup just in case)

  • csplit will then split the file based on the single "-" and output multiple files [e.g. xx00, xx01 etc.]

EDIT: Thanks @AdminBee for pointing out the typo. removed the extra "file" from the command.

  • 1
    Are you sure the first file statement after -i.bak is correct? – AdminBee Jan 14 at 10:51

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.