awk :quick question removing empty line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk :quick question removing empty line.
# 1  
Old 05-09-2013
awk :quick question removing empty line.

How to write in awk to remove lines starting with "#" and then process the file:

This is not working:
Code:
cat file|awk '{if ($0 ~ /^#/) $0="";print NF>0}'

When I just give
Code:
cat file|awk '{if ($0 ~ /^#/) $0="";print }'

it prints the blank lines . I don't wnat the blank lines along with the data lines.

Thanks,
# 2  
Old 05-09-2013
Why don't you simply do:
Code:
awk '$0 !~ /^#/ && NF' file

If you want to perform any action:
Code:
awk '$0 !~ /^#/ && NF{ action here }' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-09-2013
Try:
Code:
awk '!/^#/' file

Code:
grep -v '^#' file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-09-2013
Quote:
I don't wnat the blank lines along with the data lines.
To suppress printing of blank lines and remove lines starting with #
Code:
awk '!/^#|^$/' file

PS: no need to cat data to awk
This User Gave Thanks to Jotne For This Post:
# 5  
Old 05-09-2013
Thanks Yoda,
I was looking for something like this
Code:
awk '$0 !~ /^#/ && NF{ action here }' file

, it worked great, but it is also removing exiting empty lines, but that is fine in this case,

- && NF : worked and seems it removed empty lines.


Scrutinizer ,
I dont wanted to use grep -v, as wanted to pass in one awk statement. Thanks again...

Jotne, This worked great as well,
Code:
awk '!/^#|^$/' file

,
But surprised it leaves only one blank line on the data file, after it removed all starting hash and blank lines.
Thanks all. .
# 6  
Old 05-09-2013
You may have blanks or tabs in the remaining line.

Try this:
Code:
awk 'NF && !/#/ {action}' file


Last edited by Jotne; 05-09-2013 at 04:13 PM..
This User Gave Thanks to Jotne For This Post:
# 7  
Old 05-09-2013
Quote:
Originally Posted by rveri
but it is also removing exiting empty lines, but that is fine in this case
If you don't want to remove existing empty lines, then I guess what Scrutinizer posted is the right approach.

Just add your action part to it:
Code:
awk '!/^#/ { action here } ' file

This User Gave Thanks to Yoda For This Post:
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