Tekforums

Chat => Entertainment & Technology => Topic started by: M3ta7h3ad on March 23, 2008, 15:28:47 PM

Title: SED scripting...
Post by: M3ta7h3ad on March 23, 2008, 15:28:47 PM
Need to make a single line of code change to a huge file.

SED is the way to do it but I think im getting lost in the land of escapes and crazyness.

Need to replace the following
if [[ "${EXAMPLE1}" == .* ]]; then

With:

if [ "${EXAMPLE1}" != "" ]; then

Ive tried repeatedly and will carry on trying :) but if one of you lot could throw us a bone itd be much appreciated.
Title: SED scripting...
Post by: M3ta7h3ad on March 23, 2008, 17:14:37 PM
Wussed out in the end as it was the only occurance of it in the entire gazillion lined file, I went for this.

sed -i s/ \=\= \.\* / \!\= \" \" / filename

:) Works... as it just needed to change the test not what it was checking.

Had issues with the {} braces as well ive no idea but I think they are used to group ranges and patterns in regexp.
Title: Re:SED scripting...
Post by: cornet on March 26, 2008, 12:46:57 PM

sed -e s/\[\[ "\${EXAMPLE1}" == \.\* \]\]/[ "${EXAMPLE1}" != "" ]/ filename


that works for me.


{ } are used to define the number of times the previous character occurs

Example:


/[0-9]{1,3}/

Means there must be minium of 1 and max of 3 numeric chars or...


/[0-9]{3}/

Means there must be exactly 3 numeric chars


Title: SED scripting...
Post by: M3ta7h3ad on March 27, 2008, 22:03:43 PM
Doesnt work here as the brackets are part of the string to be checked, not used as a regex.

Its pretty much a straight pattern match.
Title: SED scripting...
Post by: Sam on April 03, 2008, 21:32:11 PM
Quote from: M3ta7h3adDoesnt work here as the brackets are part of the string to be checked, not used as a regex.

Its pretty much a straight pattern match.

But it knows cos its got non digits in.