Strip 3 header lines and 4 trailer lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strip 3 header lines and 4 trailer lines
# 1  
Old 03-09-2007
Strip 3 header lines and 4 trailer lines

Hello friends,

I want to remove 3 header lines and 4 trailer lines,

I am using following , is it correct ?

Code:
sed '1,3d';'4,$ d' filename

# 2  
Old 03-09-2007
Code:
sed '1,3 d' f | sed -e :a -e '$d;N;2,4ba' -e 'P;D'

# 3  
Old 03-09-2007
Thanks anbu
# 4  
Old 03-09-2007
Anbu23 can you please explain the code

Code:
sed '1,3 d' f | sed -e :a -e '$d;N;2,4ba' -e 'P;D'

I am confused.
# 5  
Old 03-09-2007
Code:
sed -e :a -e '$d;N;2,4ba' -e 'P;D'

$d;N;2,4ba
N appends second line to pattern space. Since second line is in pattern space 2,4ba makes control to shift to -e :a.
Then N appends third line to pattern space. 2,4ba makes control shift to -e :a.
Then N appends fourth line to pattern space and control is shifted to -e :a.
If the fourth line is the last line then $d deletes all the lines in the pattern space else N appends fifth line to pattern space.
Now 2,4ba is not satisfied then P is executed to print the first line in pattern space and this line is then deleted by D command.
Then control is shifted to start of commands. If fifth line is last line then $d deletes all the lines in the pattern space and continues to till the end of file.
# 6  
Old 03-10-2007
Another one (using awk) ..
(( upper_lim = $(cat $1 | wc -l) - 4 ))
awk -v upp_lim=$upper_lim ' NR>3 && NR<=upp_lim {print $0}' $1
# 7  
Old 03-10-2007
Quote:
Originally Posted by ganesh123
Hello friends,

I want to remove 3 header lines and 4 trailer lines,

I am using following , is it correct ?

Code:
sed '1,3d';'4,$ d' filename

Is it correct? Run it and see.

To remove an arbitrary number of lines from both hte top and bottom of a file, use my topntail command:

Code:
b=
e=
while getopts b:e: opt
do
  case $opt in
      b) b=$OPTARG ;;
      e) e=$OPTARG ;;
  esac
done
shift $(( $OPTIND - 1 ))

case $b$e in
    *[!0-9]*) exit 5 ;;
esac

if [ ${e:-1} -eq 0 ]
then
  sed "1,${b:-1}d" "$@"
else
  awk 'NR > b + e { print buf[ NR % e ] }
                  { buf[ NR % e ] = $0 }' b=${b:-1} e=${e:-1} "$@"
fi

This User Gave Thanks to cfajohnson For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. Shell Programming and Scripting

Sort without Header and Trailer

Hi , My UNIX system is SUN Solaris. I am trying to do a simple thing as described below. I have a PIPE delimited file that has header and trailer. So the file is something like below: Test1.txt looks like something below: field_data1|field_data2|and some more data --Header ... (5 Replies)
Discussion started by: Saanvi1
5 Replies

3. Shell Programming and Scripting

Verify the header and trailer in file

please see my requirement, I hope I am clear. (9 Replies)
Discussion started by: mirwasim
9 Replies

4. UNIX for Dummies Questions & Answers

oneliner for adding header and trailer

for example, i have a file with below content: 123413 866688 816866 818818 i want the output as: This is header 123413 866688 816866 818818 This is trailer i am able to achieve it using a bash script. (2 Replies)
Discussion started by: pandeesh
2 Replies

5. Shell Programming and Scripting

Append transaction header lines to same transaction's detail lines

Hi guys, I was wondering if someone can give me a hand in helping me append transaction header line in a file at the end of the transaction detail lines. Basically, I have a file that looks like this: FHEAD File1 THEAD TRANS1-blah TDETL HI1 TDETL HI2 TDETL HI3 TTAIL TRANS1-blah THEAD... (3 Replies)
Discussion started by: rookie12
3 Replies

6. Shell Programming and Scripting

Header as is.. trailer count

i have .DAT file FILE1.DAT 1200910270040625 2123456789 J123456 ABC 2123456789 K123456 ABC 2222222222 L123456 DEF 2333333333 M12345 GHI 30000004 My outfile FILE2.TXT should have like this, I need the header value as ie (1200910270040625 ) body rows remove the duplicate rows and the... (2 Replies)
Discussion started by: kshuser
2 Replies

7. Shell Programming and Scripting

How to Strip lines off Streamed EDI Output

Attached is a streamed EDI ANSI X12 output where the segment terminator/delimiter is a tilde ~ character. Is it possible to do the following pseudo-code in a unix script (using either sed, awk and/or grep)? Open file StreamedOutput.txt Search for ISA and delete the data up to the tilde ~ char... (7 Replies)
Discussion started by: sapedi
7 Replies

8. Shell Programming and Scripting

Strip one line from 2 blank lines in a file

Hi Is there any command to scan thru a file looking for 2 consecutive blank lines and if any remove one of them. Please let me know. Regards, Tipsy (6 Replies)
Discussion started by: tipsy
6 Replies

9. Shell Programming and Scripting

strip first 4 and last 2 lines from a file using perl

Hi I have a file from which i need to remove the first 4 and the last 2 lines.. i know how to do it with sed but i need to do it in a perl script.. can you please help me how to do that. Thanks (10 Replies)
Discussion started by: meghana
10 Replies

10. UNIX for Dummies Questions & Answers

Append Header and Trailer

Hi everyone, I am new to Unix programming. My inquries is:- a) How to add a Header and Trailer in the set of data b) Include a number count of the data in the trailer The set of data only contained the information of 'Customer's Name' and 'Account Number'. I would like to add the Header... (2 Replies)
Discussion started by: balzzz
2 Replies
Login or Register to Ask a Question