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

SED one-liner?

Status
Not open for further replies.

antman03

Programmer
Feb 5, 2003
8
US
Hi all

I have a file which contains a header, trailer and lines of data, I would like to append a count of the data lines in the file to the end of the trailer.

For the moment I have managed to generate the line count on a seperate line at the bottom of the file. For now it seems that I have to delete the carraige return at the end of the pen-ultimate line to have my trailer with line count at the end. The end of the pen-ultimate line has fixed text so I was thinking of using the following type script

sed -e 's/fixed text$/fixed text/g' file >> other file

but it doesn´t seem to work any ideas help greatly appreciated.

TIA

Anthony
 
Is this your requirement ? :
Header
text
text
text
Trailer00229

If so - pass wc -l minus 2 through an awk script
and if current line = Trailer
do $0 = $0 passed_wc
eg
#!/bin/ksh
WC=`wc -l<yourfile`
((WC = $WC - 2))
awk -v WRC = &quot;$WC&quot; '{
/Trailer/ print $0WRC else print
}' youfile > outfile

Dickie Bird (:)-)))
 
Dickiebird,

yep that is the right spec, I couldn´t get the code typed in bold to work though I´m not really a unix expert.

I was getting the line count using grep of data lines and wc -l, (isn´t there any way of this total being written directly to the end of the last line instead of starting a new line at the end of the file?)

I was getting

/b WC=wc -l < ant.tmp -2: bad number

msg when I tried to execute it, any pointers?

Thanks

Anthony
 
Hi,

I think I got it now, I am using sed for a file with the following type of contents

header
data
data
data
trailer:
229

sed '$N;s/trailer:\n/trailer: /' file

Gives me what I was looking for

Thanks anyway
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top