Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have header starting with '>' and I want fix the header by keeping first word and removing other shown as in output.txt and print it

input.txt

>AGAJ01065549.1 scaffold:Xipmac4.4.2:AGAJ01065549.1:1:500:1 REF
CGCCAGGTGTCTGGCGTAATAGCGCCAGCGCCAGGTGTCATATACGTAATAGCGCCAGGT
>RGAMMT01065456.1 scaffold:Xipmac4.4.2:AGAJ01065595.1:1:500:1 REF
GACTAGTTTTTACATATAGTAATGGTTATTCGGAAGTGTACAGACGTTTTCAGGTTTTTT
TTTGGTAGGGGTTGAGGTGTTGAGGTGAGGGGACTATGTGGAGGGAACTTTCCATAGAGG

output.txt

>AGAJ01065549.1 
CGCCAGGTGTCTGGCGTAATAGCGCCAGCGCCAGGTGTCATATACGTAATAGCGCCAGGT
>RGAMMT01065456.1 
GACTAGTTTTTACATATAGTAATGGTTATTCGGAAGTGTACAGACGTTTTCAGGTTTTTT
TTTGGTAGGGGTTGAGGTGTTGAGGTGAGGGGACTATGTGGAGGGAACTTTCCATAGAGG
share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

This might work for you (GNU sed):

sed -i '/^>/s/\s.*//' file
share|improve this answer
add comment

You can do this by piping the text through awk

awk '{print $1}' input.txt

This prints out the first entry of every line (entries are separated with spaces).

share|improve this answer
    
pass the file to awk directly, no need to cat and pipe it. Secondly your solution would break if non-header lines contain embedded spaces –  1_CR Nov 2 '12 at 21:28
    
True, but it addresses the example given and shows a solution that would work as long as the sample doesn't change. –  Mark Cohen Nov 2 '12 at 21:34
add comment

Similar to the answer using awk is cut:

cut -d' ' -f 1 input.txt > output.txt

The -d option sets the delimiter to one space and -f selects the first field.

However you can also use sed:

sed 's,^\([^ ]\+\) .*,\1,' input.txt > output.txt

This command substitutes an expression. It looks the beginning of a line and copies every character into a buffer which is not white space. Furthermore it matches a white space and any other character. sed replaces this line with the buffer content.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.