dnat
May 16, 2018, 6:15pm
1
Hi
I have a file where there are line feeds in the data. I am not able to read the file from an application. I exported this data from Access database and many columns contain line feed.
My data looks like this
abcd,efgh,ijkl,mnop
abcd,ef
gh,ijkl,mnop
abcd,efgh,ij
kl,mnop
I want the data as
abcd,efgh,ijkl,mnop
abcd,efgh,ijkl,mnop
abcd,efgh,ijkl,mnop
I am thinking of looping through every line until it reaches the count of delimiter(in this case 3) and remove the line feed.
Can anyone help me out!!
awk -F, '
NF == 4 {if (l) print l; print $0; l=""}
NF < 4 {l=l $0; if (gsub(",", ",", l) > 2) {print l; l=""}}
END {if (l) print l}
' data
RudiC
May 17, 2018, 5:12am
3
Try also
awk -F, '
{while (NF < 4) {getline X
$0 = $0 X
}
}
1
' file
Another way you could try:
awk -F, 'p!=""{$0=p $0} NF<4{p=$0; next} {p=x}1' file
Hello dnat,
In case your Input_file is same as shown sample then following may also help you on same.
awk '{printf("%s%s",$0!~/^abcd/?"":(FNR==1?"":ORS),$0)} END{if($0!~/^abcd/){print ""}}' Input_file
Thanks,
R. Singh
bmelon
May 31, 2018, 10:21am
6
You've got to love the charity of people on this forum. You have a question, and you get multiple solutions on how to solve it!
Better ensure that getline was successful
awk -F, '
{while (NF < 4 && getline X) $0 = $0 X
}
1
' file
A sed solution
sed '
:L
s/,/&/3
t
$!N
s/\n//
tL
' file
1 Like
RudiC
June 1, 2018, 12:22pm
8
Yes, thanks, but then better
awk -F, '
{while (NF < 4 && 1 == getline X) $0 = $0 X
}
1
' file
as getline
returns -1 on error.
1 Like