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?
# 1  
Old 10-20-2010
How to delete all leading empty lines in a file?

How to delete all leading empty lines in a file?


Code:
$>cat input.txt

test

Test
$>

output file should be
Code:
test

Test
$>


Last edited by Scott; 10-20-2010 at 01:57 PM.. Reason: Code tags
# 2  
Old 10-20-2010
Hi.

I suppose there are other ways, but here's one:

Code:
$ awk 'NF { X=1 } X' input.txt
test

Test
$>

These 2 Users Gave Thanks to Scott For This Post:
# 3  
Old 10-20-2010
thank you
# 4  
Old 10-20-2010
Code:
$
$ cat -n input.txt
     1
     2
     3
     4  test
     5
     6  Test
$
$
$ perl -lne 'if ($x||/./){$x=1; print}' input.txt
test

Test
$
$

tyler_durden
# 5  
Old 10-20-2010
Quote:
Originally Posted by durden_tyler
Code:
$
$ cat -n input.txt
     1
     2
     3
     4  test
     5
     6  Test
$
$
$ perl -lne 'if ($x||/./){$x=1; print} else{$x=0}' input.txt
test
 
Test
$
$

tyler_durden
Thanks - I was waiting for you Smilie

Perl is the better choice here, as it can write back to the same file with no need for a temporary file.
# 6  
Old 10-20-2010
Quote:
Originally Posted by scottn
Hi.

I suppose there are other ways, but here's one:

Code:
$ awk 'NF { X=1 } X' input.txt
test

Test
$>

As I am a willing-to-learn-awk-noobzor
...could you please explain the code ? thx
# 7  
Old 10-20-2010
Quote:
Originally Posted by ctsgnb
As I am a willing-to-learn-awk-noobzor
...could you please explain the code ? thx
Code:
$ awk '
NF { X=1 }   # Whenever a line has some fields (NF > 0), set X
X            # When X is set, print the line (the default action if none is specified (equivalent to X { print })
' input.txt

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