2

I have a huge text file called helo.lst in which every line starts with the format:

#=GS N4TFW2_FUSC1/96-161      AC N4TFW21

I'm only interested in the N4TFW2_FUSC1 part. I need to extract this part from the rest of the text and apply the same for the whole text file.

I found out that the following script works but I couldn't get how.

cat helo.lst | sed 's/^#=GS\s\([A-Z0-9_]*\)\/.*/\1/1' > helo_id.lst

Can someone explain what's going on after s/

AFAIK, for example sed 's/Mark/Euro/g' de_currency.txtchanges every occurrences of Mark with Euro in the de_currency.txt file. But in my example the script looks more complicated than this.

1
  • 's[search]/[what to search for]/[what to replace it with]/g'[global -- if you want it to replace every occurrence per line or only the first -- without the g]. The slashes are delimiters and can just as easily be replaced with other characters such as @. Nov 27 '15 at 2:32
-1

As per sed syntax:

/original/substitution/1 time


breaking down original

^ beginning of line

#=GS string to search

\s space

( ) \ to take meaning from ( as is used in shell ; () to denote variable to be substituted in the substitution part (grouping)

[A-Z0-9_] [] is for one char, that can be from A to Z, 0 to 9, and _

* means 0 or more times

\/ as \ is to take meaning of it from the shell, it represents the string ending with "/"

.* consume/match everything else until the end of line


breaking down substitution

\1 first string regexp match (variable) in the original string (back reference [to grouping])


Warning: this is GNU sed syntax. If in OS/X or *BSD, you have to use gsed

Link to sed article about advanced examples:

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-advanced-sed-substitution-examples/

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.