How to delete all leading empty lines in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete all leading empty lines in a file?
# 8  
Old 10-20-2010
are the { } necessary ?

(couldn't this X=1 assignment be done outside the brace ?)

does X=1 set the value 1 into the first element of an array ?

I mean since X is not previously declared so how does awk determine a suitable type for it? or are they just (void)-like by default ?
# 9  
Old 10-20-2010
pseudo code:
Code:
if number of fields in line is non-zero then X=1
if X is non zero then print line

In awk expresions that result in non-zero equate to true for more clarity we could rewrite as follows:

Code:
(NF != 0) {X=1}  (X != 0)

Also the default action in awk is to print the current line, the following is also the same:

Code:
(NF != 0) {X=1}  (X != 0) {print}

Edit:

Took a while to type my response so above probably repeats what was already said. To answer you question yes the {} are necesary awk consists of a number of

expression { action }

pairs. Expression can be null in which case action is always executed, otherwise expression needs to equate to true before the action(s) are performed.

Special expressions BEGIN and END equate to true before and after the file is processed.

Variables are null (or zero) before being assigned, no arrays are used here (arrays have [])

Last edited by Chubler_XL; 10-20-2010 at 03:22 PM..
# 10  
Old 10-20-2010
Quote:
Originally Posted by ctsgnb
are the { } necessary ?

(couldn't this X=1 assignment be done outside the brace ?)

does X=1 set the value 1 into the first element of an array ?

I mean since X is not previously declared so how does awk determine a suitable type for it? or are they just (void)-like by default ?
Yes, it's necessary to put it in brackets, otherwise it will be set without condition.

awk will type the variable by whatever you set it as first time, so yes it's "void-type". X is a scalar, not an array (X[1]=1 would make X an array).

Last edited by Scott; 10-20-2010 at 03:26 PM..
This User Gave Thanks to Scott For This Post:
# 11  
Old 10-20-2010
Code:
awk 'p+=NF' infile

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 12  
Old 10-21-2010
Quote:
Originally Posted by Scrutinizer
Code:
awk 'p+=NF' infile

@Scru1Linizer

Does "p" has a special meaning or is it a random choice of variable name?
Could you please comment your code ?

default NF is 1 ?
so p=p+1 ?
what is the default value of p ? p=1 ?

so that would make start to print$0 from line 2 ?

NF refer to the number of field of the line (which is 1 by default unless another separator is set ) or does it refer to the field number/index (the default field is a line )?

Could this also be written like this (is it equivalent)?
Code:
awk 'p+=NF 1' infile

I am a little confuse between NF and NR can someone clarify a little (ok a record is supposed to be composed of field so NF should refer to something inside NR...)

By default, a line is a field ? or a record ? or both ? (1 record of 1 field ?)

Thanks in advance for your help
# 13  
Old 10-21-2010
It is a random variable name. I could also have used:
Code:
awk x+=NF infile

The value for NF varies per line. When there is an empty line then NF (number of fields) equals zero, hence the variable stays 0 for the first few lines that are empty. The value of the variable determines if the record gets printed. The default record is a line. NR is something else, that is the line number.

Last edited by Scrutinizer; 10-21-2010 at 05:52 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 14  
Old 10-21-2010
Ok i got it
wasn't aware x would be kept between 2 line read (i though the condition was evaluated for each line so that x would be reset every time a line was read), so i wasn't understanding the use of += instead of = but i got it now (otherwise the empty line that are not the first one wouldn't have been printed...

Many Thanks !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete everything after empty line in file

Hello, I am trying to remove everything coming after empty line under ubuntu14.04. How may I do it? file: aaa 020390 bb00id3 c03wisi 209dl x092sia 0290s I expect: aaa 020390 bb00id3 c03wisi (3 Replies)
Discussion started by: baris35
3 Replies

2. Shell Programming and Scripting

Delete the last empty/blank line of the text file

Hi All, I have a file.txt which seems like having three lines. wc -l file.txt 3 file.txt In fact, once it is open in text editor, this file has four lines where the last line is empty. how can i delete this last empty line of the file.txt? I tried the codes below so far but they... (6 Replies)
Discussion started by: senayasma
6 Replies

3. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

4. Shell Programming and Scripting

Sed delete certain number of empty lines

Hi What I'm trying to do is delete every blank line upto a certain number, so for instance I only want to delete the first 4 empty lines within a file, I know how to delete every line with: sed '/^$/d' But I can't figure out how to limit it to only the first 4 occourances I though it... (5 Replies)
Discussion started by: duonut
5 Replies

5. Shell Programming and Scripting

delete lines with empty second column

hi, I'd like to ask you if you can help me with such surely easy thing - I have some data and need to plot them. I have txt of data in two columns, tab separated, but some second columns are empty. like 1```` 2 1.2`` 3```` 2 4.44` 5````` 6.1```1 how can I erase all the lines... (2 Replies)
Discussion started by: bsco
2 Replies

6. UNIX for Dummies Questions & Answers

want to remove " in a file and delete empty spaces

I have to remove character " in file which occurs at every line and have to delete empty spaces. Please help (2 Replies)
Discussion started by: vikram2008
2 Replies

7. UNIX for Advanced & Expert Users

How to delete empty lines

abc# abc#this is a test abc#this is a test to delete abc# xyz# xyz#this is a test two xyz# In the above example '#' is common. How to do delete the emply lines. In specific to observe the output as: abc#this is a test abc#this is a test to delete xyz#this is a test two . . . . (5 Replies)
Discussion started by: Aejaz
5 Replies

8. Shell Programming and Scripting

Delete an empty file from dir.

Hi, I have an dir which has 50K file, some of them are empty. I want to delete the empty file from the dir. I am planning to use the following command. ls -l | grep " 0 " | awk '{print $9}' I can copy this to an file and then how do I use this file to delete the empty files.... Any help... (2 Replies)
Discussion started by: Script40
2 Replies

9. Shell Programming and Scripting

delete multiple empty lines

Hi, I need to delete the lines with empty name. What is the best way to do it? Thanks a lot for your help! EMID MMDDYY HOURS JOB EMNAME 0241 051605 11.40 52062 someone 0520 051605 10.63 52062 0520 051605 10.66 52062 0520 051605 10.65 52062 (3 Replies)
Discussion started by: whatisthis
3 Replies

10. UNIX for Dummies Questions & Answers

How will you list only the empty lines in a file (using grep)

How will you list only the empty lines in a file (using grep) (1 Reply)
Discussion started by: JosephGerard
1 Replies
Login or Register to Ask a Question