Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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


share|improve this question
up vote 3 down vote accepted

This might work for you (GNU sed):

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

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 – iruvar 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

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

Your Answer


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.