script help
Cameron Hutchison
lists at xdna.net
Mon Aug 23 12:31:39 UTC 2010
Oliver Marshall <Oliver.Marshall at g2support.com> writes:
>Hi chaps,
>Can someone help me with a script/grep syntax please? I have a string within an HTML file downloaded by wget.
>The string is "Antivirus Pattern - Win vx.yyy.z"
>I need to find that string and then pass "xyyyz" to the console pass to the next command in the chain (note the lack of v and the ".").
>Any ideas how I can do this ?
sed is the right tool for this job (not grep).
sed -n 's/^.*Antivirus Pattern - Win v\(.\)\.\(...\)\.\(.\).*/\1\2\3/p' wget_file.html
where wget_file.html is the HTML file you downloaded with wget.
To break this down:
sed -n : this runs sed, telling it not to output lines unless told to do so
's/XXX/YYY/p' : substitute XXX for YYY and print the line (overrides sed -n)
\(XXX\) : The part between \( and \) is matched and assigned to the
next "group" by number
\1 : substitute the value of group 1. Can be from 1 to 9
The sed pattern matches three groups because that is the data we want to
extract. I placed dots (.) in the groups to say "match any character"
for each dot. You can make this more specific by using [0-9] for
instance if you know that x, yyy and z are only numbers. (Note that I
"escaped" the actual dots in the string (\.), because a dot on its own
matches any character - thats why there are a lot of backslashes there).
The replacement string consists of the three matched groups only
(\1\2\3). All the other text on the line is discarded.
Any lines that do not match the pattern are not printed (sed -n).
The next step is passing this to the next command, either with a pipe if
it is to go to stdin of the next command, or in the parens in $() if
they are to go on the command line of the next command.
More information about the ubuntu-users
mailing list