3

I want to remove all spaces from a file, except from every line beginning with the same pattern (pattern is "ORGANISM").

Input:

Cat; Dog; Squirrel
ORGANISM Animalus terrus
Sequence: ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGT

Output:

Cat;Dog;Squirrel
ORGANISM Animalus terrus
Sequence:ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGT

No more spaces in any line except the line starting with the characters "ORGANISM".

7
sed '/^ORGANISM/!s/ //g' /path/to/input

This will remove all spaces on all lines that do not start with ORGANISM.

1
sed -e '/^ORGANISM/n' -e 's/ //g'  < input > output

The first expression jumps to the next line if this one starts with ORGANISM, and if that didn't happen, substitute all spaces with nothing (globally)

1

Here's a perl version using negative lookahead (?!ORGANISM) to figure out if we need to perform substitution. -p flags allows automatic printing

$ perl -pe 's/[[:blank:]]//g if /^(?!ORGANISM).*/' input.txt                                                             
Cat;Dog;Squirrel
ORGANISM Animalus terrus
Sequence:ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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