Leaving last 30 lines

If we want to display lines from file leaving last 30 lines. i dont know the count of lines in file

 
tail -r temp | nawk 'NR>30'|tail -r

You can also try:

awk 'NR==FNR{n=NR;next}FNR<=n-c-1' c=30 file file

Or:

awk 'NR>c{print A[NR%c]} {A[NR%c]=$0}' c=30 file

can you pls give me a sample output and explain me what command does..will be very helpful. thank you

Sed and awk are both fast method.

lines=$(cat x.txt | wc -l)  # numberOfLines

echo "--------------------------"
# delete lines between 1-$line = rest is output
sed  "1,${line}d" x.txt

echo "--------------------------"
((line=line-1))  
# print (p) starting from line $line to end
sed -n "$line,\$p" x.txt

Thanks kshji

But I want output using one command, not a script

---------- Post updated at 10:12 PM ---------- Previous update was at 10:09 PM ----------

kshji

tail -r temp | nawk 'NR>30'|tail -r

is this command working?

tail -r temp | nawk 'NR>30'|tail -r

This is three commands, nearly a script :slight_smile:

But jotne we can execute it in one go
correct..

tail -r temp | nawk 'NR>30'|tail -r

But I didnt try yet is it working or not. I am not sure what

tail -r

does

try also:

tail -30 file

If you don't know what tail -r does, or any other command, read the manual page for it.

man tail

That will display the lines the user doesn't want.

scott and rdrtx1, I want to leave last 30 line, so

tail -30 filename

is not helpful

try also:

tail -30 filename > f.tmp
mv f.tmp filename
sed -n "1,$(( `wc -l < filename` - 30 ))p" filename

if sed supports -i option, try:

sed -i -e :a -e '$q;N;31,$D;ba' infile