Remove blank lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove blank lines
# 1  
Old 10-04-2010
Question Remove blank lines

I really hope someone can help me with this. I have several php files from a forum that I run, that now for some reason have blank lines after every line. Is there an easy way to make a script that does the following:

* If there are consecutive blank lines, delete all of them except one.
* If there is a blank line between lines with text, delete it.

Example - I want this:

Code:
<?php

/*

 * @text

 * @text

 * @text



 * @text

 * @text

 * @text


 */

?>

to result in this:

Code:
<?php
/*
 * @text
 * @text
 * @text

 * @text
 * @text
 * @text

 */
?>

Anyone?
# 2  
Old 10-04-2010
First sed cleans ends of lines of spaces and tabs (I show \t but I mean a real tab).
Second and third sed's are loopers with N, and so cannot share buffer with others.
Second sed removes the lonesome blank lines by examining three lines in the buffer.
Third sed removes adjacent blank lines by examining two lines in the buffer.

Code:
sed '
  s/[ \t][ \t]*$//
 ' | sed '
  :two
  $b
  N
  :one
  $b
  N
  s/\(.)\n\n\(.)/\1\
\2/
  t twox
  P
  s/.*\n//
  t one
  :twox
  P
  s/.*\n//
  t two
 ' | sed '
  :loop
  $b
  N
  /^\n$//!P
  s/.*\n//
  t loop
 '


---------- Post updated at 04:15 PM ---------- Previous update was at 04:14 PM ----------

The smiley is colon and lower case o.

Last edited by Scott; 10-04-2010 at 05:33 PM.. Reason: Code tags, please...
# 3  
Old 10-04-2010
Thanks for the reply, but I can't get that to work at all. Smilie

Is there an easier way to do it like this:

1. Delete all consecutive blank lines except the first, and replace that blank line with a chosen word, with something like this (which just removes consecutive blank lines except the first):
Code:
sed '/./,/^$/!d'

If so, I can then just easily remove the rest of the blank lines, and the replace the word I chose with a blank line.
# 4  
Old 10-04-2010
Code:
awk 'NF{if(n>1)print x;n=0;print;next}{n++}' infile

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 10-04-2010
Quote:
Originally Posted by Scrutinizer
Code:
awk 'NF{if(n>1)print x;n=0;print;next}{n++}' infile

Awesome, thank you! That works like a charm, exactly how I want it. Smilie Smilie

---------- Post updated at 10:31 PM ---------- Previous update was at 10:16 PM ----------

If I would want to do this on a number of .php files in a folder and get the output from each file as filename_2.php, how can I script that too?
# 6  
Old 10-04-2010
Quote:
Originally Posted by KidCactus
Awesome, thank you! That works like a charm, exactly how I want it. Smilie Smilie

---------- Post updated at 10:31 PM ---------- Previous update was at 10:16 PM ----------

If I would want to do this on a number of .php files in a folder and get the output from each file as filename_2.php, how can I script that too?
Code:
 
for file_name in *.php
do
---your awk statement $file_name > ${file_name}"_2".php
done

This User Gave Thanks to panyam For This Post:
# 7  
Old 10-04-2010
Small correction to panyam's suggestion:
Code:
awk-statement "$file_name" > "${file_name%.*}_2.php"

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Remove blank lines using cat command

plz help me to figure it out how i remove empty or blank files using cat command. i will be very thankful if u send me this answer... thanks (3 Replies)
Discussion started by: mushfiks1
3 Replies

2. Shell Programming and Scripting

Remove Blank lines in VI

Hi, Which option is used to remove blank lines in VI (AIX). ? Regards, Siva (6 Replies)
Discussion started by: ksgnathan
6 Replies

3. UNIX and Linux Applications

remove all blank lines

When I 'vi' my test file I see some blank lines. However once I do :set list to display hidden characters, I see the empty lines literally like this: ^I$ How do I remove them? I cannot find a regex to match them. (3 Replies)
Discussion started by: alexsuv
3 Replies

4. Shell Programming and Scripting

How to remove blank lines

Hi, I am facing a problem related to removing blank lines from a text document. Input Error 17-05-2011 11:01:15 VisualSVN Server 2.1 1001 The following information was included with the event: line3 line4 Error 17-05-2011 11:00:25 VisualSVN Server 2.1 ... (13 Replies)
Discussion started by: mayursingru
13 Replies

5. Shell Programming and Scripting

remove blank lines and merge lines in shell

Hi, I'm not a expert in shell programming, so i've come here to take help from u gurus. I'm trying to tailor a csv file that i got to make it work for the LOAD FROM command. I've a datatable csv of the below format - --in file format xx,xx,xx ,xx , , , , ,,xx, xxxx,, ,, xxx,... (11 Replies)
Discussion started by: dvah
11 Replies

6. Shell Programming and Scripting

Can't remove blank lines from a file

Hi Guys, I have been trying to remove blank lines from a file with no success. I tried using all the following options on the file: tr -s '\n' < abc.txt grep -v "^$" abc.txt sed '/^$/d' abc.txt sed '/./!d' abc.txt awk '/./' abc.txt The file is a text file. (11 Replies)
Discussion started by: npatwardhan
11 Replies

7. Shell Programming and Scripting

remove blank lines

I have joined 2 files. Join command worked fine. but the result showing extra blank lines. I tried to remove blank spaces by using awk (-- -42 RS= ORS="\n\n" file.txt) and sed (sed '/^ *$/d' file.txt)commands but didn't remove any Any suggestions plz:D 123 tab ....... ......tab .......234... (3 Replies)
Discussion started by: repinementer
3 Replies

8. UNIX for Advanced & Expert Users

How to Remove the unwanted Blank Lines

I have a file with the below data, i would like to remove the end blank lines with no data. I used the below commands but could not able to succeed, could you please shed some light. Commands Used: sed '/^$/d' input.txt > output.txt grep -v '^$' input.txt > output.txt input.txt file... (5 Replies)
Discussion started by: Ariean
5 Replies

9. UNIX for Dummies Questions & Answers

remove blank lines in *.srt file :)

Hi all, I use translate web to get subtitle file in my langues . But in output file have bad blank lines . I need scrip (i use debian ) to remove this blank lines . szintax of my bad *.srt file : ------ number blank1 number:number ---> number:number blank2 text1 . textn blankS... (10 Replies)
Discussion started by: hungbp
10 Replies

10. UNIX for Dummies Questions & Answers

Remove blank lines

¿How can i remove blank lines between all lines in a long text file? Example WrongFile.txt : Line 1 Line 2 Line 3 CorrectFile.txt : Line 1 Line 2 Line 3 Thanks in advance :confused: (4 Replies)
Discussion started by: osymad
4 Replies
Login or Register to Ask a Question