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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use sed to print first n lines and last n lines of an output.
# 1  
Old 07-30-2013
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.
# 2  
Old 07-30-2013
Check "selective printing of certain lines" section on this page

http://www.catonmat.net/blog/wp-cont...9/sed1line.txt
This User Gave Thanks to rajamadhavan For This Post:
# 3  
Old 07-30-2013
Quote:
Originally Posted by rajamadhavan
Check "selective printing of certain lines" section on this page

http://www.catonmat.net/blog/wp-cont...9/sed1line.txt
Hi rajamadhavan, thanks for the reply.
I had seen the page before posting,
but below did not work. Smilie
Code:
$ cat a.txt
1
2
3
4
5
6
7
$ sed 3q -e :a -e '$q;N;4,$D;ba' a.txt
/tools/oss/packages/x86_64-rhel5/sed/default/bin/sed: can't read 3q: No such file or directory
5
6
7

# 4  
Old 07-31-2013
Is your need to print them at one go using a single sed command ?

Can't you go with something like this ?

Code:
sed '3q' filename &&  sed -e :a -e '$q;N;4,$D;ba' filename

This User Gave Thanks to rajamadhavan For This Post:
# 5  
Old 07-31-2013
I know that you asked for a sed solution, but ed is well suited to something like this and only needs to be invoked once to do it. This was tested using a Korn shell, but will work with any shell that recognizes basic Bourne shell syntax:
Code:
#!/bin/ksh
# Usage: tester file count
ed -s "$1" <<-EOF
        1,$2p
        \$-$2+,\$p
        q
EOF

If you save this in a file named tester, make it executable with:
Code:
chmod +x tester

and run it with:
Code:
./tester a.txt 1

where a.txt contains the text shown in the 3rd message in this thread, the output will be:
Code:
1
7

and the output from the command:
Code:
./tester a.txt 5

will be:
Code:
1
2
3
4
5
3
4
5
6
7

These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 07-31-2013
Quote:
Originally Posted by rajamadhavan
Is your need to print them at one go using a single sed command ?

Can't you go with something like this ?

Code:
sed '3q' filename &&  sed -e :a -e '$q;N;4,$D;ba' filename

Hi rajamadhavan,
Actually it's a command output instead of a file,
so your above code seems not applicable.
Thanks.

---------- Post updated at 02:50 PM ---------- Previous update was at 02:45 PM ----------

I googled and got some alternative solutions.
Code:
(head -3; tail -3) < a.txt

but how to apply it to a command output?
I tried below but failed.
Code:
cat a.txt | (head -3; tail -3)

Thanks.
# 7  
Old 07-31-2013
Just for the fun of it, using awk
Code:
awk '{a[NR]=$0} NR<=n {print} END{for (i=NR-n+1;i<=NR;i++) print a[i]}' n=3 file

This prints the first 3 and last 3 records of file

Or if you like it to the output
cat file | awk '{a[NR]=$0} NR<=n {print} END{for (i=NR-n+1;i<=NR;i++) print a[i]}' n=3
This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. AIX

Print output of grep command in multuple lines

HI All, I am using grep command to serach a pattern in a list of files and storing the output in a variable. Then i am applying some logic on that variable to get the required output. But Thing is that when the pattern is present mutiple times in a file, i am getting the output of grep in a... (3 Replies)
Discussion started by: goutam sahoo
3 Replies

3. Shell Programming and Scripting

sed command : print lines having no # at the begining

I am trying to print those line which has no # in the begining of the line. The sed I used for this purpose as shown below is not giving the required output. echo 'PDE 5600' | sed -n 's/^\!#/&/p' Where lies the problem:confused: (3 Replies)
Discussion started by: hiten.r.chauhan
3 Replies

4. UNIX for Dummies Questions & Answers

sed print certain lines matching variable

I have a very large results file, and a list of filters grep -wf filterlist.txt datafile.txt > outfile.txt The above line works but is very slow and I'm wondering how to make it faster. The items in my filterlist are only relevant to the first column. I don't care if any item in the... (1 Reply)
Discussion started by: sayb
1 Replies

5. Shell Programming and Scripting

Print duplicate only lines as normal output - Awk

input output a1 100 200 XYZ_X a1 98 188 ABC (2 Replies)
Discussion started by: quincyjones
2 Replies

6. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies

7. Solaris

sed command to print lines after expression

Hi guys. I need a sed command to print like 10 lines after a regular expression is found in the log. Can anyone help me out. Thanks ---------- Post updated at 10:52 AM ---------- Previous update was at 10:34 AM ---------- never mind. I just did the search bewteen two expressions. (1 Reply)
Discussion started by: jamie_collins
1 Replies

8. Shell Programming and Scripting

read file-print lines in sed

Hello! Im trying to read file contents. Then, print out every line that has "/bens/here" in the file that was read. cat /my/file.now | sed '/bens/here/p' I keep getting the error asking if I need to predeclare sed? What does predeclaring sed mean? Thanks! Ben (2 Replies)
Discussion started by: bigben1220
2 Replies

9. Shell Programming and Scripting

sed print all lines after pattern match

HiCan someone show me how to print all lines from a file after a line matching a pattern using sed?Thanks (13 Replies)
Discussion started by: steadyonabix
13 Replies

10. Shell Programming and Scripting

Sed one-liner to print specific lines?

I need to print specific lines from a file, say 2-5, 8, 12-15, 17, 19, 21-27. How do I achieve this? (2 Replies)
Discussion started by: Ilja
2 Replies
Login or Register to Ask a Question