Issue
EDIT: Thank you all for answering the question, all the answers work. Stack Overflow indeed has a great community.
Receiving a flat file as a source. In one of the fields, the value is segregated into new lines, but there is a need to break the newline and combine it into a single content.
Ex: File is as below:
PO,MISC,yes,"This
is
an
example"
PO,MISC,yes,"This
is
another
example"
In the above ex, the data is being read as 9 lines, but we need the input to be read as a single line, as shown below -
PO, MISC, yes, "This is an example"
PO, MISC, yes, "This is another example"
Tried via the below syntax but did not succeed. Is there any way to achieve this? I also need to print the file contents into another file.
Syntax:
awk -v RS='([^,]+\\,){4}[^,]+\n' '{gsub(/\n/,"",RT); print RT}' sample_attachments.csv > test.csv
Solution
With your shown samples Only, please try following awk
, written and tested in GNU awk
. Simple explanation would be, setting RS
to \"\n
and setting field separator as ,
. In main block Globally substituting new lines with spaces in $NF. Then using printf
printing current line along with value of RT
.
awk -v RS="\"\n" 'BEGIN{FS=OFS=","} {gsub(/\n/," ",$NF);printf("%s",$0 RT)}' Input_file
Answered By - RavinderSingh13 Answer Checked By - Terry (WPSolving Volunteer)