LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 01:17 AM   #1
bharatbsharma
Member
 
Registered: Oct 2007
Posts: 33

Rep: Reputation: 15
Question delete lines from files


How do i delete first 10 lines from any files , preferble using awk.
 
Old 10-25-2007, 01:27 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
IMHO, sed would be a better tool for this.

Code:
sed -i '1,10d' file1 file2 file3

Last edited by jschiwal; 10-25-2007 at 01:28 AM.
 
Old 10-25-2007, 01:51 AM   #3
bharatbsharma
Member
 
Registered: Oct 2007
Posts: 33

Original Poster
Rep: Reputation: 15
i tried using this but not working

sed -i '1,10d' alarms.txt
sed: illegal option -- i
 
Old 10-25-2007, 01:59 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by bharatbsharma View Post
How do i delete first 10 lines from any files , preferble using awk.
its just as easy as sed, however without the added convenience of -i switch. you have to rename the file in a few steps
Code:
awk 'NR>10' file > newfile
mv newfile file
 
Old 10-25-2007, 02:48 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
You must not be using the gnu sed program. Sed is lighter weight than gawk,
ls -l /bin/gawk /bin/sed
-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

Since you don't have the "-i" option, you will need to redirect the output to a temporary file as ghostdog74 mentioned.
Code:
for file in file1 file2 file3 file4
do
   sed '1,10d' $file >temp
   mv temp $file
done
The "1,10" part is the line range. You can also use patterns (using sed or awk)
Code:
 /sbin/lspci -v | sed -n '/Broadcom/,/^$/p'
02:02.0 Network controller: Broadcom Corporation BCM4306 802.11b/g Wireless LAN Controller (rev 03)
        Subsystem: Hewlett-Packard Company NX9500 Built-in Wireless
        Flags: bus master, fast devsel, latency 64, IRQ 11
        Memory at e0104000 (32-bit, non-prefetchable) [size=8K]
There is an O'Reilly book that covers both Sed & Awk that you might be interested. The first edition is on the web if you want to google for it. The second edition is in the bookstores.

Also, you might consider downloading the source for gawk. It includes the .texi source for the "Gawk: Effective Awk Programming" book, which is excellent. I don't remember the exact make target, maybe "make pdf" or "make doc". You can examine the Makefile yourself to find out.
 
Old 10-25-2007, 08:43 AM   #6
dgar
Member
 
Registered: Jun 2005
Location: Candia, NH
Distribution: Ubuntu, FC, RHE3, RHE4, CentOS
Posts: 121

Rep: Reputation: 15
http://www.gnu.org/manual/gawk/
 
  


Reply



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 first and last lines of a file ChainsawPenguin Programming 5 09-28-2007 07:28 AM
Delete lines using awk kkjegan Programming 13 09-11-2007 07:36 PM
how do I delete *certain* lines in vi editor? zim64half Linux - General 3 04-23-2007 07:39 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
delete some lines from a file freelinuxcpp Linux - Software 4 01-17-2004 10:28 AM

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

All times are GMT -5. The time now is 11:20 PM.

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