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

shell script --> to copy lines between 2 fixed lines 1

Status
Not open for further replies.

RVSachin

Technical User
Nov 12, 2001
77
IN
Hi,

How do I write a korn-shell script to copy all the lines between following lines of a file into a different file:

Starting Group ----> line 1
------------------------ ----> line 2




------------------------ ----> line n

The no. of lines between line 2 and n varies.

Thanks,

RV
 
Try this...

awk 'NR>2 {if ($0 ~ "------------------") exit; print}' file1
 
#!/bin/ksh

file=txt
n=0
while read line; do
typeset -L1 fch=$line
if [[ $fch = '-' ]]; then
if [[ n -eq 1 ]]; then
exit 0;
else
((n+=1))
fi
else
print $line
fi
done < $file
 
Or if there are multiple lines between -----'s

#!/bin/ksh
file=txt
n=0
while read line; do
typeset -L1 fch=$line
if [[ $fch = '-' ]]; then
if [[ n -eq 1 ]]; then
print ""
n=0
else
((n+=1))
fi
else
if [[ n -eq 1 ]]; then
print $line
fi
fi
done < $file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top