awk :quick question removing empty line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk :quick question removing empty line.
# 8  
Old 05-10-2013
Code:
grep '^[^#]' file

Code:
awk '/^[^#]/ {print}' file

removes empty lines, but leaves lines with a blank character.

Last edited by MadeInGermany; 05-10-2013 at 11:41 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 9  
Old 05-11-2013
MadeInGermany ,
Thanks this is also a nice code, seems inside [ ] the caret is doing negetion for #.
But couldn''t get how the empty lines are getting removed.

Code:
awk '/^[^#]/ {print}' file

Is that means print lines starting with something , but not hash and empty lines.

Thanks for the post.
# 10  
Old 05-11-2013
It greps the lines with a first character that is not a #
# 11  
Old 05-11-2013
But a blank line does not start with #, so why does it remove it?
# 12  
Old 05-11-2013
Code:
awk '/^[^#]/ {print}' file

Quote:
jotne - But couldn''t get how the empty lines are getting removed.
rveri - But a blank line does not start with # , so why does it remove it?
It prints lines that start with a character that is not #. A blank line does not start with a character. So a blank line is not printed, so the blank line is removed (as well as a line starting with #).

-------------------------------

An alternative way, maybe easier to understand, using grep:
Code:
grep -v -e '^#' -e '^$' file

This means "print all lines except those that start with # or that are blank".
# 13  
Old 05-12-2013
Ok, now I understand, thanks.
So ii there are lines that do contain spaces or tabs it will not work. awk '/^[^#]/ {print}' file
This would. awk 'NF && !/#/ {action}' file
# 14  
Old 05-12-2013
Code:
awk 'NF && !/#/ {action}' file

That would not work. It has two problems.

First, it would need to be !/^#/ to mean "do not act on lines that start with #".

Second, the idea was to act on lines that have a space character. But NF is 0 if just a space character, just as NF is 0 if the line is totally blank. So it would not act on the lines with spaces.

-----------------------

Look at the test input below, where the expected output is to NOT print first two lines (empty, start with #), and do print last three lines, and compare the results from the three ways:
Code:
$ cat -e input
# X$
$
X #$
 $
string 1-99-string 2$

Code:
$ awk 'NF && !/#/ {print}' input
string 1-99-string 2

Code:
$ awk '/^[^#]/ {print}' input | cat -e
X #$
 $
string 1-99-string 2$

Code:
$ grep -v -e '^#' -e '^$' input | cat -e
X #$
 $
string 1-99-string 2$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to identify empty fields in line

I am trying to use awk to identify and print out records in fields that are empty along with which line they are in. I hope the awk below is close, it runs but nothing results. Thank you :). awk awk -F'\t' 'FNR==NR ~ /^*$/ { print "NR is empty" }' file file 123 GOOD ID 45... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. UNIX for Dummies Questions & Answers

Using awk to remove duplicate line if field is empty

Hi all, I've got a file that has 12 fields. I've merged 2 files and there will be some duplicates in the following: FILE: 1. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, 100 2. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, (EMPTY) 3. CDC, 54321, TEST3,... (4 Replies)
Discussion started by: tugar
4 Replies

3. Shell Programming and Scripting

Quick awk question

gawk 'BEGIN{count=0} /^Jan 5 04:33/,0 && /fail/ && /09x83377/ { count++ } END { print count }' /var/log/syslog what is wrong with this code? i want to search the strings "fail" and "09x83377" from all entries. im grabbing all entries in the log starting from Jan 5 04:33 to the end of the... (3 Replies)
Discussion started by: SkySmart
3 Replies

4. Shell Programming and Scripting

Quick sed/awk question

Hi fellow linux-ers, I have a quick question for you. I have the following text, which I would like to modify: 10 121E(121) 16 Jan 34S 132E 24 Feb 42 176E(176) 18 Sep 21S 164E 25 May 15 171W(-171) 09 Jul How can I do the following 2 modifications using sed and/or awk? 1. in 1st column,... (1 Reply)
Discussion started by: lucshi09
1 Replies

5. Shell Programming and Scripting

awk script creates empty line

I have the following awk script to which I pass the file > 10 0 0 10 0 0 > 12.997 0 5.71132 12.9098 0.0687626 5.48855 12.7506 0.174324 5.13225 12.5913 0.262662 4.80643 12.4316 0.335652 4.50283 12.2717 0.394598 4.21542 12.1113 0.440399 3.93957 11.9506 0.473646 3.67148 11.7894... (2 Replies)
Discussion started by: kristinu
2 Replies

6. Shell Programming and Scripting

awk print last line returns empty string

hello I have a file with lines of info separated with "|" I want to amend the second field of the last line, using AWK my problem is with geting awk to return the last line this is what I am using awk 'END{ print $0 }' myFile but I get an empty result I tried the... (13 Replies)
Discussion started by: TasosARISFC
13 Replies

7. Shell Programming and Scripting

awk - if field is empty, move line to new file

I have a script with this statement: /usr/xpg4/bin/awk -F"" 'NR==FNR{s=$2;next}{printf "%s\"%s\"\n", $0, s}' LOOKUP.TXT finallistnew.txt >test.txt I want to include logic or an additional step that says if there is no data in field 3, move the whole line out of test.txt into an additional... (9 Replies)
Discussion started by: scriptr2be
9 Replies

8. Shell Programming and Scripting

Sed question (Removing a line of text)

I am working with bash on HP-UX server at school. As practice for scripting, I am trying to make a pretend server admin script that adds a user to the system, deletes a user from the system, and lists all users of the pretend system. I have accomplished this with a select loop. Adding users, and... (2 Replies)
Discussion started by: masterscout1977
2 Replies

9. Shell Programming and Scripting

Help with AWK -- quick question

Ok. I'm just starting to use AWK and I have a question. Here's what I'm trying to do: uname -n returns the following on my box: ftsdt-svsi20.si.sandbox.com I want to pipe this to an AWK statement and make it only print: svsi20 I tried: uname -n | awk '{ FS = "." ; print $1 }' ... (5 Replies)
Discussion started by: Probos
5 Replies

10. Shell Programming and Scripting

perl question - removing line from input file

In perl I want to do remove the top line of my input file then process the next line. I want to do something like head -1 inputfile > temp grep -v temp inputfile > newinputfile cp newinputfile inputfle is this possible in perl? (3 Replies)
Discussion started by: reggiej
3 Replies
Login or Register to Ask a Question