Reputation: 11
I am trying to adjust header lines in a file using sed and I have looked at many previous posts but nothing I've tried based on those so far have helped. So here it goes:
I am using terminal on mac OS,
I have a file with header lines that look like this: starting with >
>KeepThis_text_VariableNumbersHere /LotsOFText/Here to get rid of."
I want to have:
>KeepThis_text_VariableNumbersHere
All of the header lines end in ." but there are quotes within the header line as well.
I have tried many variations using sed, here are a few:
sed -e 's/^\/*.*//' input.file > output.file #this removed everything
or
sed -e 's/^\/*.*"//' input.file > output.file #this kept non-header
lines, which is good, but got rid of entire header line
sed -e 's/>KeepThis_Text_*.* *.*”/>KeepThis_Text_*.*\//' input.file > output.file #This did not change anything
Thank you for any suggestions!
Upvotes: 0
Views: 1171
Reputation: 67497
awk to the rescue!
awk -F/ 'NR==1{print $1;next}1' file
modifies first line only, prints everything up to /
.
Upvotes: 0
Reputation: 158060
I would use capture everything starting from >
including everything before space followed by a /
in a capturing group. The keep only the capturing group and throw away the remaining text, like this:
sed 's/\(>[^\/]\+\) .*/\1/' input.txt
It looks more clean using extended regular expression, using the -r
option:
sed -r 's/(>[^\/]+) .*/\1/' input.txt
I'm not sure if header line means for you the first line. If you really mean the first line, limit the s
command to the first line like this:
sed -r '1s/(>[^/]+) .*/\1/' input.txt
Upvotes: 0
Reputation: 70852
If you only want to change 1st line, consider addressing your command (s///
):
sed -re '1s/\/\w+//g'
or addressing all lines beginning with >
:
sed -re '/^>/s/\/\w+//g'
or if you want to drop everything from /
to end of line:
sed -re '/^>/s/\/.*//g'
Upvotes: 1
Reputation: 70452
If you want to only keep the part that precedes the forward slash, then the following will do:
sed -e 's-/.*--'
The separators for the command are not limited to /
, it is just a convention. Here, I used -
as command separators. The command is then: replace anything following a slash until the end of line with empty text.
Upvotes: 1