how to use sed to get last 6 lines

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to use sed to get last 6 lines
# 1  
Old 10-31-2007
how to use sed to get last 6 lines

dear experts,

Sorry im a newbie. i have a file called INA.txt and it contains

INA 2007-08-28 1631102 1027114 384472 196639 22877 594194 34412
INA 2007-08-29 1633962 1030338 385999 198903 18722 589318 36156
INA 2007-08-30 1635325 1029710 385471 201558 18586 586097 35836
INA 2007-08-31 1637176 1047182 371050 201992 16952 582365 34574
INA 2007-09-01 1638491 1050806 364819 203030 19836 578627 35150
INA 2007-09-02 1639918 1052110 362672 205293 19843 574704 35709
INA 2007-09-03 1640868 1045316 367341 206709 21502 571422 35607
INA 2007-09-04 1641938 1045747 367866 208576 19749 567991 35853
INA 2007-09-05 1642445 1043900 372356 209103 17086 564587 36145
How do i get the 6 lines before "INA 2007-09-05" to appear? I tried using grep -A6, it doesnt work. Pls help.
# 2  
Old 10-31-2007
Use the B option: grep -B6
# 3  
Old 10-31-2007
radulov, i cant seem to use those options.

root@ckpgpaywebs03core # grep -B6 "INA 2007-09-26" INA.txt
grep: illegal option -- B
grep: illegal option -- 6
Usage: grep -hblcnsviw pattern file . . .
# 4  
Old 10-31-2007
awk '{ x[FNR] = $0 }
/^INA 2007-09-05/ { 
	for ( i = FNR-6; i <= FNR; i++ ) 
	print x[i]
}' filename

Use nawk on /usr/xpg4/bin/awk on Solaris.

P.S. You can add delete x (for multiple occurrences)
or exit (for a single one) for efficiency Smilie
In that case you have to use xpg awk on Solaris.
# 5  
Old 10-31-2007
Thanks radoulov, it works with nawk.
Didnt work with awk though Smilie

Thanks again.
# 6  
Old 10-31-2007
Originally Posted by aismann
Thanks radoulov, it works with nawk.
Didnt work with awk though Smilie
It works with nawk and /usr/xpg4/bin/awk on Solaris.
You don't have to use [/usr]/bin/awk on Solaris,
it's too old and broken.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

2. Shell Programming and Scripting

Use sed to print first n lines and last n lines of an output.

Use sed to print first n lines and last n lines of an output. For example: n=10 Can it be done? Thanks. (7 Replies)
Discussion started by: carloszhang
7 Replies

3. Shell Programming and Scripting

sed print all lines between second and third identical lines

I am trying to extract a table of data (mysql query output) from a log file. I need to print everything below the header and not past the end of the table. I have spent many hours searching with little progress. I am matching the regexp +-\{99\} with no problem. I just can't figure out how to print... (5 Replies)
Discussion started by: godfreydanials
5 Replies

4. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

5. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

6. Shell Programming and Scripting

sed show lines text between 2 blank lines

I have a file like blah blah blah blah this is the text I need, which might be between 1-4 lines, but always has a blank line above and below it, and is at the end of the text file the code tags don't show the trailing blank line. I started by deleting the last blank line with: ... (2 Replies)
Discussion started by: unclecameron
2 Replies

7. Shell Programming and Scripting

Merge two lines using sed

Hi, I am trying to merge two lines, first line starts with a particular pattern and second line ends with a particular pattern in a file. Something like: First line starts with say ABC Second line ends with say XYZ After a merge, the line should become ABC.......XYZ I tried... (14 Replies)
Discussion started by: Sunny Arora
14 Replies

8. Shell Programming and Scripting

using sed to remove lines

Can somebody explain why my sed command is not working. I do the folloinwg: Generates a binary file to /tmp/x1.out /usr/lib/sa/sa2 -s 4:00 -e 8:00 -i 3600 -A -o /tmp/x1.out decodes the file (no problem so far) sar -f /tmp/x1.out When I do this it does not appear to delete the... (4 Replies)
Discussion started by: BeefStu
4 Replies

9. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies

10. Shell Programming and Scripting

sed and blank lines

hello, i have tried to remove blank lines from a file using korn shell script file.. it doesn't seem to work! i tried sed '/^\s*$/d' infile > outfile but that didn't work i tried sed 's/ *$//;/^$/d' infile > outfile and that didn't work i tried sed '/^s./d' infile > outfile and that... (6 Replies)
Discussion started by: alrinno
6 Replies
Login or Register to Ask a Question