How to remove blank lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove blank lines
# 8  
Old 05-26-2011
Hi,
Thanks for your replies. I will definitely try the solutions. It never came to my mind the file is a text file converted from ".evtx" format. So it definitely has some junk in it.


Regards,
Mayur

---------- Post updated 05-26-11 at 11:31 AM ---------- Previous update was 05-25-11 at 07:38 PM ----------

Hi,
Thanks a lot bakunin and zaxxon. I understood bakunin's solution but was not able to comprehend zaxxon's solution will take some awk reading and get through it. Meanwhile, i came up with this temporary solution
Code:
sed '/^[[:space:]]*$/d' filename

Regards,
Mayur
# 9  
Old 05-26-2011
Hi,

try this out --
Code:
cat filename|sed "/^[ ]*$/d" ;

it should work.

thankx,
Arpit
# 10  
Old 05-26-2011
sed '/^$/d'

The above command delete the blank line
# 11  
Old 05-26-2011
hello anil,

i think same is required here.

Last edited by thearpit; 05-26-2011 at 05:22 AM..
# 12  
Old 05-26-2011
@mayursingru

/^Error/ {print l?l RS RS $0: RS $0; l=x; next} --> If the line starts with the string "Error" then print l, but when it has a value, use l and two times the record separator (RS) which is a newline, and additionally the current line ($0). If l has no value at the moment, kust print one record separator and the current line. This will make sure, that you have an empty line as separator for the next block starting with "Error". Afterwards we set the value of l to nothing (l=x) and skip the rest of the awk script for this input line (next).

/^line/ {l=sprintf("%s%s", l, $0); next} --> If a line starts with "line", we store into the variable l the current line and if there have been former lines starting with "line", they will be already stored in l and just being appended. With this, you will not have every "line..." on its own row but concatenated them all into one, as your example output shows. We then skip the rest of the awk script for that line again (next).

/^[[:space:]]*$/ {next} --> If a line is "empty", but we recognized that in your example there are spaces in it which can be blanks or tabs or mixed at any number, we just skip them. [[:space:]] is a POSIX bracket expansions (see this Regex Tutorial - POSIX Bracket Expressions). The rest of the awk script is skipped again.

{print} --> If none of the before 3 conditions matched, ie. a line does not start with Error, line or contains spaces (or none of them), we just print the rest of the lines, which in your example is the one starting with "The following ...".

END{ if(l){print l} }When the whole file has been worked through, we just check, if l still has a value in it (this should be a line starting with "line"), we have to print it now, since we formerly had a line starting with "Error" triggering this print which will not happen anymore.
# 13  
Old 05-26-2011
Hi
can you please try this
Code:
grep -v "^$" input_file > out_file

# 14  
Old 05-26-2011
Hi zaxxon,
Thanks a ton.


Regards,
Mayur
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

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

5. Shell Programming and Scripting

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... (9 Replies)
Discussion started by: KidCactus
9 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