The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Strip one line from 2 blank lines in a file tipsy Shell Programming and Scripting 6 06-23-2008 09:14 AM
strip first 4 and last 2 lines from a file using perl meghana Shell Programming and Scripting 10 02-01-2008 09:01 PM
Append Header and Trailer balzzz UNIX for Dummies Questions & Answers 2 01-06-2008 08:19 AM
How to count lines - ignoring blank lines and commented lines kthatch UNIX for Dummies Questions & Answers 6 05-25-2007 02:21 AM
Total of lines w/out header and footer incude for a file gzs553 Shell Programming and Scripting 1 11-16-2006 07:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-09-2007
ganesh123 ganesh123 is offline
Registered User
  
 

Join Date: Feb 2007
Location: Milwaukee
Posts: 24
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 (permalink)  
Old 03-09-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
sed '1,3 d' f | sed -e :a -e '$d;N;2,4ba' -e 'P;D'
  #3 (permalink)  
Old 03-09-2007
ganesh123 ganesh123 is offline
Registered User
  
 

Join Date: Feb 2007
Location: Milwaukee
Posts: 24
Thanks anbu
  #4 (permalink)  
Old 03-09-2007
ganesh123 ganesh123 is offline
Registered User
  
 

Join Date: Feb 2007
Location: Milwaukee
Posts: 24
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 (permalink)  
Old 03-09-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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 (permalink)  
Old 03-10-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 596
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 (permalink)  
Old 03-10-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0