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

awk loop to delete lines ina file

Status
Not open for further replies.

pb0y

Technical User
Feb 27, 2002
16
US
Hi

I need assistance with a loop routine that counts the number of lines in a file then extracts the last line into a new file. The problem I am running into is the number of lines in file A will be dynamic.

I need a way to subtract the lines in File A one at a time and output them into File B. File B can never have more than one line since I am using the line as a variable for another function. I would prefer to just delete each line from file A and output them to file B but cut is not able to do this and I haven't had much luck with sed.


for i in $fileA
do
x=`cat $i | wc -l`
y=`expr $x - 1`
z=`expr $x - $y`
nawk 'NR == 1, NR == '"$z"' {print}' $i > $FileB
 
Hi,

I think that you can do all work without AWK.

If you want to extract last part of a file use TAIL :
tail -1 file # Extract last line of file
tail -5 file # Extract last 5 lines of file
tail +10 file # Extract last part of file begining at line 10.

If you want to extract first part of a file use HEAD :
head -5 file # Extract first 5 lines of file

And if you want to extract moddle part of a file :
head -5 file | tail +10 # Extract lines from line 5 to line 10 of file. Jean Pierre.
 
To process fileA one line at a time in reverse order, you can do

for i in $(tail -r fileA)
do
do your stuff here
done

Greg.
 
thanks

i got it to work like this

while [ $X != 1 ]

do
X=`expr $X - 1`
head -$X $FileA |tail +$X > $FileB
done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top