LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-25-2007, 03:31 AM   #1
bharatbsharma
Member
 
Registered: Oct 2007
Posts: 33

Rep: Reputation: 15
delete first blank line


A file start with blank line . I want to delete only that line.

i/p
\n
AB BB

CC DD


o/p
AA BB

CC DD
 
Old 10-25-2007, 03:44 AM   #2
ehawk
Senior Member
 
Registered: Jul 2003
Posts: 1,257

Rep: Reputation: 48
http://www.computing.net/unix/wwwboard/forum/7594.html
 
Old 10-25-2007, 06:08 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Code:
perl -i.backup -ne 'if (!($. == 1 && /^\s*$/)) { print; }' input_file
Note: this command will modify input_file, and create a backup in input_file.backup
 
Old 10-25-2007, 07:31 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'NR>1' file > newfile
 
Old 10-25-2007, 07:41 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
There are a lot of posts dealing with blank lines today!

You can also use sed:
sed '1d' file >temp && mv temp file

If you use gnu's sed program:
sed -i '1d' file

The sed program is only 2 characters '1d', and the sed program isn't as large as gawk or perl:
Code:
ls -l /usr/bin/perl /bin/sed /bin/gawk
-rwxr-xr-x 1 root root  295432 Sep 21 14:16 /bin/gawk
-rwxr-xr-x 1 root root   53056 Sep 21 14:15 /bin/sed
-rwxr-xr-x 3 root root 1301600 Sep 21 13:39 /usr/bin/perl
A backup might be a good idea:
sed 'ld' file >temp
mv -b temp file
 
Old 10-25-2007, 08:10 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by jschiwal View Post
The sed program is only 2 characters '1d', and the sed program isn't as large as gawk or perl:
Code:
ls -l /usr/bin/perl /bin/sed /bin/gawk
-rwxr-xr-x 1 root root  295432 Sep 21 14:16 /bin/gawk
-rwxr-xr-x 1 root root   53056 Sep 21 14:15 /bin/sed
-rwxr-xr-x 3 root root 1301600 Sep 21 13:39 /usr/bin/perl
All true, except the two character program doesn't work when the first line is not blank, in which case that line should not be deleted. It's not clear from the OP if that is important or not.

Last edited by matthewg42; 10-25-2007 at 08:11 AM. Reason: typo
 
Old 10-25-2007, 08:37 AM   #7
dgar
Member
 
Registered: Jun 2005
Location: Candia, NH
Distribution: Ubuntu, FC, RHE3, RHE4, CentOS
Posts: 121

Rep: Reputation: 15
You can contort the logic a bit:
awk '!NR==1 || !/^$/ {print;}' file
 
Old 10-25-2007, 02:40 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Code:
sed -i '1{/^$/d}' file
If the problem is to delete the first line from a file if and only if it is a blank line. If you know it is blank, entering "sed -i '1d' filename" is a quick and easy way to remember command to delete the first line on a particular file. Thanks for pointing it out though.

I will use "find ./ -type f -md5sum '{}'\; | sort | uniq -w32 -D >dupes" to locate duplicate files. After handling a duplicate, I'll enter "sed -i '1,2d'" in the console to delete the entries for the original and duplicate. Then I'll look at "head dupes" again. If you find a duplicated directory, you could use
"sed -i '/\/dirname\//d' to delete all entries containing that directory name.

Last edited by jschiwal; 10-25-2007 at 03:03 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM
Blank line - grep. liguorir Linux - Software 1 04-24-2004 09:03 AM
backspace changes line, doesn't delete lugoteehalt Linux - General 2 10-22-2003 04:51 AM
I did the following program and get blank line after pressing <ENTER> purpleburple Programming 5 08-21-2002 12:00 PM
how do I print 'blank space' to standard out in C on same line? purpleburple Programming 7 08-18-2002 11:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration