7

I have a text file contaning the following:

"random
textA"
"random
random
textB"

The separator is "

How can I split the containt into multiple file as follow using a bash command?

File 1 :

random
textA

File 2 :

random
random
textB

I came into examples using csplit or awk but they does not cover this text layout.

  • Is that the only text in the file? – Hauke Laging Dec 8 '14 at 17:15
  • yes only text in the file – Fabio Dec 9 '14 at 9:38
5

Simple awk command:

awk 'NR%2==0{ print > "File "++i }' RS='"' file

RS defines " as record separator and NR is the record number. If the record number was modulo of 2 (because we have another first " for records), then print the current record $0 into a File #.

  • 1
    This is IMO the best answer as it works even if other lines exist between "$ and the next ^". – don_crissti Dec 8 '14 at 21:26
  • don't need the begin just use ++i instead of i++.Could be shortened to awk 'NR%2==0{ print >"File "++i }' RS='"' file – user78605 Dec 8 '14 at 22:33
  • @Jidder Uh,yes. thank you. updated with that, – αғsнιη Dec 9 '14 at 4:40
4

If the opening quote is always at the beginning of the line, csplit will work just fine like this:

bash$ csplit /tmp/data '/^"/'

That produces files called xx00, etc. Note, your example removes the quotation marks and this doesn't. You'd need a command line sed to do that:

bash$ for file in xx* ; do { sed 's/^"//;s/"$//;' ${file} >x${file}; } ; done
  • With coreutils csplit: csplit -z filesample '/^"/' {*} so as to split more than once and ignore the first piece (xx00) which will always be empty (for the given text sample). – don_crissti Dec 8 '14 at 18:20
3

With GNU awk

awk -v RS='"[[:space:]]*"' '
{sub(/^"|"[[:space:]]*$/, "");print > "output." ++n; close("output." n)}' file.txt
  • An alternative: awk -v RS='"' '!/^[[:space:]]*$/ {print > "output." n++; close...}' – muru Dec 8 '14 at 17:49
2

You can do:

awk -v RS=\" -v ORS= \
  '{ sub(/^\n*/,"");sub(/\n*$/,""); };
  /^\n*$/ { next;};
  { if(strcnt==0) { print $0 >"file1"; strcnt++;} else
  { print $0 >"file2"; }; }' file

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.