Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ONLY Second line with SED

Tags:

linux

unix

sed

How can I get second line in a file using SED

@SRR005108.1 :3:1:643:216
GATTTCTGGCCCGCCGCTCGATAATACAGTAATTCC
+
IIIIII/III*IIIIIIIIII+IIIII;IIAIII%>

With the data that looks like above I want only to get

 GATTTCTGGCCCGCCGCTCGATAATACAGTAATTCC
like image 492
neversaint Avatar asked Aug 18 '11 07:08

neversaint


People also ask

How do you sed a specific line?

Just add the line number before: sed '<line number>s/<search pattern>/<replacement string>/ . Note I use . bak after the -i flag. This will perform the change in file itself but also will create a file.

How do I print the second line in Unix?

head -2 creates a file of two lines. tail -1 prints out the last line in the file.

How do we display the last second line of a file?

Use the tail command to write the file specified by the File parameter to standard output beginning at a specified point. This displays the last 10 lines of the accounts file. The tail command continues to display lines as they are added to the accounts file.


3 Answers

You don't really need Sed, but if the pourpose is to learn... you can use -n

n read the next input line and starts processing the newline with the command rather than the first command

sed -n 2p somefile.txt

Edit: You can also improve the performance using the tip that manatwork mentions in his comment:

sed -n '2{p;q}' somefile.txt
like image 181
Jonathan Avatar answered Oct 17 '22 03:10

Jonathan


You always want the second line of a file? No need for SED:

head -2 file | tail -1
like image 22
Jacob Avatar answered Oct 17 '22 05:10

Jacob


This will print the second line of every file:

awk 'FNR==2'

and this one only the second line of the first file:

awk 'NR==2'
like image 16
Karoly Horvath Avatar answered Oct 17 '22 03:10

Karoly Horvath