Removing two newlines with awk / sed

Hello everybody. I have a file full of data, each line consists of something like "755545; 45634; 1244". Sometimes an unknown amount of additional new lines may appear somewhere, which I don’t want. Example:

256163;16816;1651
16156;165165;1165

15153;135135;15351
15153;1351;8



165;15313;153513
254;45;45

Required Conclusion:

256163;16816;1651
16156;165165;1165    
15153;135135;15351
15153;1351;8
165;15313;153513
254;45;45

Is it possible to do this with awk / sed utility in unix?

+3
source share
5 answers

The answer from @Luixv is correct if there are no spaces on empty lines. If a space is used, use instead:

sed '/^[ \t]*$/d'

This is the space before \ t in brackets, i.e. [space \ t]

If this does not work, you may have a problem with the new characters. Do a:

$ file test_file
test_file: ISO-8859 text, with CRLF, LF line terminators

If you get the output above, convert the file to unix using:

$ dos2unix test_file
+9

sed '/^ $/d'

+6
sed -nre "s/([^$])/\1/p" input
+1
sed -n 's/^[ ,\t]*$/!p' filename
+1

Solved with ssedsuper_sed, if you do not have it, install it.

ssed -R -e '/(^$|\s)/ d' yourFile

or

cat yourFile| ssed -R -e '/(^$|\s)/ d'

Happy sedation

PS: will work even if you have tabs or \r \t \ntherefore \sin ReqExp.

\r = Return Carriage
\n = New Line
\t = Tab
0
source

Source: https://habr.com/ru/post/1775392/


All Articles