Register
It is currently Wed Oct 22, 2014 5:55 pm

Collapse empty blank lines?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Sat Oct 24, 2009 1:01 pm   

Joined: Fri Oct 09, 2009 2:20 pm
Posts: 3
I'm using m4 (the macro processor) foe a couple of tasks .. works great (of course). Trouble is, a side-effect of some of the macros (even with careful use of dnl) leaves a lot of extra whitespace in the form of blank lines in the output.

Anyone know what command/tools are good for collapsing these multiple blank lines down to a single one? That is, I want to collapse any run of N blank lines down to one blank line. I'm on OS X, so have access to the usual BSD/Linux/Unix command set.

tia,
Tim


Top
 Profile  
 PostPosted: Mon Oct 26, 2009 2:26 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
There are several ways to remove _all_ whitespace newlines which does not contain anything but that.

However it is much more difficult to remove and add just one.

If you can except that it will truncate all lines which just contains (regexp description) ^\n$ then this command will do the trick:
Code:
# cat file.txt
test



test
# cat file.txt | tr -s "\n"
test
test


this page suggests a wide range of ways to remove newlines, maybe one of them can be modified for your purpose.
http://linux.dsplabs.com.au/rmnl-remove ... ssam-p65/#

Atm I'm at work and cannot research this matter much further

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Oct 26, 2009 5:52 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
I thought I'd do this in awk as I don't use it often and it turned out to be a bit of a pain, I'm sure there's a better way but this should work;

Code:
[david ~]# cat temp/awktest.awk
BEGIN { retro=0 }
($0 ~ /^$/ && retro == 0) { retro=1; print; next }
($0 ~ /^$/ && retro == 1) { retro=1; next }
($0 !~ /^$/) { retro=0; print }

[david ~]# echo "one

two


three



end" | awk -f temp/awktest.awk
one

two

three

end
[david ~]#


Top
 Profile  
 PostPosted: Sat Jun 25, 2011 6:31 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
You can use GNU/BSD sed(1) and its neat commands ( since -i is not defined by POSIX )

Code:
sed -i '/^$/ {N; /^\n$/D;}' file


It processes each line as described below:

If we encounter a blank line ( « /^$/ » ), then we load the next line from the file in the pattern space ( « N » ), and we check whether the line corresponds to the pattern /^\n$/ (only a newline) ; if the statement is true (in another words: if the pattern is matched, so if the line is strictly a newline), we remove the first line from the pattern space in case there are two blank lines ( « D » ).

------

I also had another approach with bash(1) for fun...

Code:
IFS=$'\n' read -rd '' -a lines < file

for ((i = 0; i < ${#lines[@]} - 1; i++)); do
    printf '%s\n\n' "${lines[i]}"
done
printf '%s\n' "${lines[@]:(-1)}"


Top
 Profile  
 PostPosted: Sun Jul 10, 2011 7:53 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Just figured by chance that GNU/BSD cat(1) has an option which
« suppress[es] repeated empty output lines » / « squeeze[s] multiple adjacent
empty lines, causing the output to be single spaced ».

Code:
cat -s file > file.tmp && mv file.tmp "$_"


Last edited by jsz on Sun Jul 10, 2011 8:44 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Sun Jul 10, 2011 8:44 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
jsz ....... you do realize this post was from 2009 ???????

I think it's safe to assume the OP has moved on....


Top
 Profile WWW  
 PostPosted: Mon Jul 11, 2011 10:52 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
In case someone else have the same issue or is just interested in this topic... or simply the original poster come back.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP