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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete all matching line except the first line. 1

Status
Not open for further replies.

kHz

MIS
Dec 6, 2004
1,359
US
I am doing a for loop and if a file name already exists I am appending the file contents to the file that already exists, for example a file contains:

Host,Time,% Inodes Used(/),% Inodes Used(/var),% Inodes Used(/opt),% Inodes Used(/opt/app),% Inodes Used(/export/home)
srv1,Wed Aug 17 06:00:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 17 06:05:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 17 06:10:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 17 06:15:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 17 06:20:04 CDT 2005,14.2,2.1,0.9,5.2,0.1

and the next files contains:
Host,Time,% Inodes Used(/),% Inodes Used(/var),% Inodes Used(/opt),% Inodes Used(/opt/app),% Inodes Used(/export/home)
srv1,Wed Aug 19 06:00:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:05:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:10:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:15:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:20:04 CDT 2005,14.2,2.1,0.9,5.2,0.1

so my files ends up like:
Host,Time,% Inodes Used(/),% Inodes Used(/var),% Inodes Used(/opt),% Inodes Used(/opt/app),% Inodes Used(/export/home)
srv1,Wed Aug 17 06:00:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 17 06:05:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 17 06:10:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 17 06:15:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 17 06:20:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
Host,Time,% Inodes Used(/),% Inodes Used(/var),% Inodes Used(/opt),% Inodes Used(/opt/app),% Inodes Used(/export/home)
srv1,Wed Aug 19 06:00:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:05:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:10:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:15:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:20:04 CDT 2005,14.2,2.1,0.9,5.2,0.1

but what I want is:
Host,Time,% Inodes Used(/),% Inodes Used(/var),% Inodes Used(/opt),% Inodes Used(/opt/app),% Inodes Used(/export/home)
srv1,Wed Aug 17 06:00:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 17 06:05:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 17 06:10:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 17 06:15:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 17 06:20:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:00:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:05:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:10:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:15:04 CDT 2005,14.2,2.1,0.9,5.2,0.1
srv1,Wed Aug 19 06:20:04 CDT 2005,14.2,2.1,0.9,5.2,0.1

How do I delete all lines that match "Host,Time,% Inodes Used(/),% Inodes Used(/var),% Inodes Used(/opt),% Inodes Used(/opt/app),% Inodes Used(/export/home)" EXCEPT the first line? I know how to delete all lines that match that with sed, but I want to keep the first line.

Thanks.
 
Something like this ?
awk '/^Host,Time/{if(++n==1)print;next}1' /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 FAQ181-2886
 
hmm,
and if it must not be awk ....

tail -n +2 somefile

will give all lines starting with the 2nd

or

grep -v "^Host" somefile

will give all lines without the ones containing "Host" at the beginning
 
awk '{if(FNR>1) print $0}' somefile

will print your file starting with line 2, regardless what line 1 contains
 
true,
but it may be more difficult to read, if you are not so familiar with awk.

i could live with

awk 'FNR > 1 {print}' somefile

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top