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

Cuting specified line from a text file 3

Status
Not open for further replies.

FlashGorden

IS-IT--Management
Nov 8, 2001
22
0
0
EU
Hi

I have managed to take a text file with fields delimited with a | and add a unique number. My only problem is that the file has a header and footer.

I need a command to remove the header and footer and then insert then after I've updated the primary key.

Thanks
 
Not easily done with a command, but this shell script should get you started.

#!/bin/ksh
# topntail.sh - remove 1st & last line of a file, do stuff and
# stick it back together again

# check that filename supplied
[ $# != 1 ] && {
echo "Need a filename"
exit 1
}

FILE=$1
LINES=`cat $FILE|wc -l`
TAIL=`expr $LINES - 2`

# save header & footer
head -1 $FILE > header.txt
tail -1 $FILE > footer.txt

# get the main section of the file
tail +2 $FILE | head -$TAIL > mainfile.txt

# do the primary key stuff here
#
#

# put the file back together again
cat header.txt mainfile.txt footer.txt > newfile.txt

Post back it there are any probs.

Greg.
 
This may be easier ...
Using piped tail commands ...
This assumes your file has single line headers and footers

tail -r filename | tail +2 | tail -r | tail +2

Read the tail man page for an explanation ....

"The power of Unix"
 
This assumes your file has single line headers and footers

awk -v ll="`cat Yourfile.txt|wc -l`" '{
if( NR > 1 && NR < ll) print
}' Yourfile.txt Gregor.Weertman@mailcity.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top