Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete blank lines at start of file 3

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
GB
This must be so straightforward but ...

What's the best way to delete a few (in this case it's always 2) lines from the start of a file (the length of the file will vary).

The usual thanks for all suggestions

Chris
 
One way:
sed -n '3,$p' /path/to/input > output
Another way:
awk 'NR>2' /path/to/input > output
Yet another way:
tail +3l /path/to/input > output
....

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Delete blank lines at start of file.
Code:
awk 'NF || x { print; x=1 }' infile >outfile


If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. For an introduction to Awk, see faq271-5564.
Let me know whether or not this helps.

 

From Eric Pement, this works no matter how many lines:

sed '/./,$!d' myfile
 
This works no matter how many lines:
Code:
awk '$1,0' myfile
 
Thanks everyone for the replies. All helpful but futurelet gets the star for brevity.

Could you explain why it works? In particular why it only does blank lines at the start of the file.

Chris
 
Long form:
[tt]
$1, 0 { print $0 }
[/tt]

An awk program consists of test [tt]{[/tt] actions [tt]}[/tt] pairs. The actions are performed if the test succeeds for the line just read. When the test is of the form test1[tt],[/tt]test2 , it means "start performing the actions when test1 is true and continue until test2 is true". So we're telling Awk to start printing the line read ($0) when field 1 ($1) exists (i.e., when it's not an empty string) and to stop printing when [tt]0[/tt] is true. Well, 0 is always considered false, so Awk never stops printing; it prints the rest of the file.

For an introduction to Awk, see faq271-5564.
 
Quite impressive fururelet.
You deserve a star.

But what if the first field of the first non empty line is the string '0' ?

I would (and will) use a mix of your two examples:
Code:
awk 'NF,0'

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

Denis
 
A star for Denis.

Good point. If the $1 of first non-blank line is "0", $1 is treated as a number, not a string, so it will be considered false and the line won't be printed.

Your version will work in all cases (I think).

An illustration of the truism "Never trust a program that hasn't been thoroughly tested.
 
"Never trust a program that hasn't been thoroughly tested."
Variation on the theme:
A working program is a not enough tested program.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top