LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   delete lines from files (https://www.linuxquestions.org/questions/programming-9/delete-lines-from-files-594409/)

bharatbsharma 10-25-2007 01:17 AM

delete lines from files
 
How do i delete first 10 lines from any files , preferble using awk.

jschiwal 10-25-2007 01:27 AM

IMHO, sed would be a better tool for this.

Code:

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

bharatbsharma 10-25-2007 01:51 AM

i tried using this but not working

sed -i '1,10d' alarms.txt
sed: illegal option -- i

ghostdog74 10-25-2007 01:59 AM

Quote:

Originally Posted by bharatbsharma (Post 2935997)
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


jschiwal 10-25-2007 02:48 AM

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.

dgar 10-25-2007 08:43 AM

http://www.gnu.org/manual/gawk/


All times are GMT -5. The time now is 04:48 PM.