I am downloading an XML file using wget, but sometime the file has text in the first line that I need to get rid off.
It currently has “131” on the first line and “0” on the last line.
I need a way of removing these lines if it contains this information. I can’t do a perl find and replace, in-case it is not there but the proper first line contains “131”.
Does this make sense?
Any ideas?
Thanks
Example, sometimes it is this:
131
<element>
<example>content</example>
<example>content</example>
<example>content</example>
<example>content</example>
</element>
0
It is sometimes like this (correct)
<element>
<example>content</example>
<example>content</example>
<example>content</example>
<example>content</example>
</element>
That’s a job for
sed! You would’nt find quicker or simplier:If you’re sure of the two values, you could simply:
But whith the following command,
sedwhile remove any first and/or last line containing only a number:This could by run with files as param and backup files auto-generation:
Explained:
1and$are address:1for first line and$for last line./^[0-9]\+$/mean *lines that begin with one or more characters between0and9and ending immediately after.dfor delete line.This could be written:
as well.
Edit:
As I hate to write more than one time… approx anything;
There is a way to do some tricky thing, but only on 1st and last line.
First, the same sample could by written:
So this is 1 byte shorter! But especially the operation is written only once:
Explained:
:aand:bare labels where to branch (jump) tobaandbbare branch instruvtion respectively to:aand:b.1and$are address as previously described/.../dis previously described too, mean delete lines matching regexAnd could by written:
Sample of application, using
s/../../instead of onlyd:Modify version info only if present at 1st or last line: