How to remove header and footer records of a flat file using UNIX shell script?
I am having a flat file as give below. How do I delete the header and footer from the file using UNIX shell script and rewrite the same file.
9 20050427 HE开发者_StackOverflow社区ADER RECORD
0000000 00000 000000000 123456 00 654321 DATARECORD
0000000 00000 000000000 123456 00 654321 DATARECORD
0000000 00000 000000000 123456 00 654321 DATARECORD
0000000 00000 000000000 123456 00 654321 DATARECORD
6 20050427 TRAILER RECORD
Thanks, Arun
... and with sed
:
As @Baramin noted: the least amount to type is sed '1d;$d'
, here is how it works:
By line number
sed -i'' -e '1d' -e '$d' yourfile
1d
deletes first line $d
deletes last line.
Or by pattern
sed -r -i -e '/^[0-9] [0-9]{8} HEADER RECORD$/d' \
-e '/^[0-9] [0-9]{8} TRAILER RECORD$/d' yourfile
-r
is needed for the {8}
extended regular-expression.
Or both
If you are super pedantic, and want to cover your ass in the most thorow way:
sed -r -i.bak -e '1{/^[0-9] [0-9]{8} HEADER RECORD$/d}' \
-e '${/^[0-9] [0-9]{8} TRAILER RECORD$/d}' yourfile
The -i''
will change yourfile
in-place. Drop it if you want to store the output in another file (e.g. with > outputfile
at the end of your line).
tail -n +2 filename | head -n -1
don't have my unix box to test on so those numbers might be 1 or 2, i can't remember if they are bot 1, both 2 or what but i ise this all the time (i just experement right before i run the cmd to see if its 1 or two... the tail ...
should remove the first line and the head ...
should remove the last
Very simple solution for your question
sed -i '1d;$d' yourfile
This is ugly, but it seems to work, at least on your input:
f='test.txt'; g=`wc -l $f`; h=`echo $g | cut -d ' ' -f1`; head -n $((h-1)) $f | tail -n $((h-2))
f is the name of your file. I couldn't work out a quicker way to get rid of the filename from the output of wc. Someone should be able to beat this.
If you want to rewrite the same file, just redirect the output of the command:
f='test.txt'; g=`wc -l $f`; h=`echo $g | cut -d ' ' -f1`; head -n $((h-1)) $f | tail -n $((h-2)) > $f
OTOH, a couple of commands that will clip the first and last line of a file.
head -n -1
will clip the last line of a file.- awk can be used to clip the first line of the file with a command like
awk 'NR>1 {print}'
Alternatively, you can use grep -v
to filter out the first and last lines if they can be reliabily identified by string matches.
Here is another option that is simpler (if your tail supports +
semantics)
tail -n +2 <file> | head -n -1
I got it in this way.. and thanks all for your help.
wc -l fileman |awk '{print $1-0}' |xargs -i head -{} filename | sed -n '2,$p' > filename
Just to throw out a couple more alternatives:
awk -v last=$(wc -l < filename) 'NR == 1 || NR == last {next} {print}' filename
awk 'NR==1 {next} NR==2 {prev = $0; next} {print prev; prev = $0}' filename
To overwrite the current file, if you're not using sed -i
:
( whatever | pipeline ) <filename >filename.tmp &&
ln filename filename.bak &&
mv filename.tmp filename
精彩评论