Delete blank lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete blank lines in a file
# 1  
Old 05-07-2012
Delete blank lines in a file

Hi All,

I have a file and I need to delete the lines that are blank and is starting with some characters below. Something like below:

Code:
Regular Ascii File:

Line1:  AGODA1	BUSAN||SK	Lord Beach	4/6/2012	4/7/2012	68060
Line2:  AGODA2	BUSAN||SK	Beach Hotel	4/6/2012	4/7/2012	610200
Line3:  Skahds4
Line4:  AMAN12	CHI	MMa	4/6/2012	4/7/2012	612134
Line5:  Depp
Line6:  ROGER|south|MMa1|4/6/2012|4/7/2012|53106
Line7:  Mango
And so on

I need to delete the lines that has character in the beginning but nothing after that. In my case, I need to delete lines 3, line 5 and line 7(which are shown in bold above).

I will really appreciate any advice.
rkumar28
# 2  
Old 05-07-2012
I'm assuming it doesn't actually have the 'Line:' junk at the front of the lines.

Code:
awk -F'|' 'NF>1' < inputfile > outputfile

# 3  
Old 05-07-2012
Quote:
Originally Posted by Corona688
I'm assuming it doesn't actually have the 'Line:' junk at the front of the lines.

Code:
awk -F'|' 'NF>1' < inputfile > outputfile

Thanks a bunch for replying. This was really helpful. I not very good in awk. I would really appreciate if you can help me understand the awk statement below:

awk -F'|' 'NF>1'
rkumar28
# 4  
Old 05-07-2012
Code:
-F'|'

Use a field-separator of |. So it would consider a line like A|B|C|D|E to have 5 fields.

Code:
'NF>1'

Print all lines with more than one field. So lines with no | in them get ignored.

A bare expression like that is an implied 'if(X) { print }'. A fuller form which does the same thing would look like

Code:
awk -F'|' '{ if(NF>1) print }' filename

# 5  
Old 05-07-2012
the awk statement defines a "field separator" character, in this case the vertical bar character "|", using the argument
Code:
-F'|'

then uses the awk built-in "field count" variable "NF" to output only lines which contain greater than one field.

based on your sample input, I don't think this is precisely what you want, as your "line4" would not be output. The default field separator is any whitespace character, which may be sufficient, so you can try the following variation and compare your results.

Code:
awk 'NF>1' < inputfile > outputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

2. Shell Programming and Scripting

Tried many options but unable to delete blank lines from text file

Hi, I tried the following options but was unable to delete blank lines from file Input file = temp.hash.txt temp.hash.txt content 90 0 89.56 0 0 57575.4544 56.89 (9 Replies)
Discussion started by: uuuunnnn
9 Replies

3. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

4. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

5. Shell Programming and Scripting

Delete blank lines from a file

Hi, I want to use diff to compare two files in a Perl file. But one of the files has some blank lines at the end. So I want to delete the blank lines from the file firstly and then use diff to compare them. But I dont know how to delete the blank lines from the files. Meanwhile, the system is... (5 Replies)
Discussion started by: Damon_Qu
5 Replies

6. Shell Programming and Scripting

Why cant i delete blank lines?

I have a sed pipeline: myVar=$(cat $FILE | sed -n '/regex/,/regex/{/regex/d;p}' | sed -n '/regex/!p' | sed -e s/*:// | sed /regex/,+8d \ ) sed '/^$/d' sed '/./!d' And i've tried to add that in a different order rather then just on the end..Why isnt it deleting all the blank... (2 Replies)
Discussion started by: omgsomuchppl
2 Replies

7. UNIX for Dummies Questions & Answers

delete blank lines from a file

can anyone show me how to delete blank lines from a file. thanks in advance (2 Replies)
Discussion started by: sachin.gangadha
2 Replies

8. Shell Programming and Scripting

Delete blank lines at the end of file

I am attempting to delete blank lines in my file and I've used this command: sed '/^$/d' $file > $file.fixed all this seems to do is copy the file and not delete the blank lines located at the end of the file. Any assistance would be greatly appreciated. (3 Replies)
Discussion started by: TL56
3 Replies

9. Shell Programming and Scripting

regex to delete multiple blank lines in a file?

can't figure out a way to delete multiple empty lines but keep single empty lines in a file, file is like this #cat file 1 2 3 4 5 6 - What I want is 1 2 (6 Replies)
Discussion started by: fedora
6 Replies

10. UNIX for Dummies Questions & Answers

delete blank lines or lines with spaces only

hi there i'm trying to delete blank lines and or lines with spaces only from a series of files in an directory. to do so, i'm using this: for files in `ls /users/myname/pesop* 2>/dev/null` do grep -v ^$ $files > newfile mv newfile $files done now, this works great for blank lines but... (3 Replies)
Discussion started by: vascobrito
3 Replies
Login or Register to Ask a Question