I am very new to awk and thought of trying with a simple exercise of splitting a file based on a pattern. Please note:
- My file is a notepad file .txt (with CRLF format).
- File has exaclty the below content (there is no blank line in the input file at the beginning)
string file1
line1
line2
line3
string file2
line1
line2
line3
string file3
line1
line2
line3
- What am I trying to achieve (want to use only awk at this point of time)?
Split file as soon as I find expression "string" and excluding it. So, my output would be like
"file1" containing only
line1
line2
line3
"file2" containing only
line1
line2
line3
and so on....Below is what I tried...but it leaves a newline at the end of each file and in the beginning of each file in case A and B respectively.
CASE A:
BEGIN {RS="\r\n";FS=" ";ORS="\r\n"}
/string/ { fname = $2; next } { print > fname".txt"}
CASE B:
BEGIN {RS="\r\n"; FS=" "; ORS=""}
/string/ { if (NR>2) print prev_line>fname".txt"; fname=$2; next} {print (prev_line="") ? $0 : "\r\n" $0 > fname".txt"; prev_line=$0}
Can someone provide me some better methods/hint at modifying the above awk script?
Thanks.
GNU Awk 4.0.1
"Case A" works as expected. Output even retains the CRLF format, with no extra lines – user000001vi
). However runnignod -c
on the files I can confirm that they do contain a final\r\n
and if you want to avoid this, the solution you gave in your answer seems to be the way to go. – mschilli