LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Replace lines in script (https://www.linuxquestions.org/questions/linux-software-2/replace-lines-in-script-123405/)

AMMullan 12-06-2003 10:30 PM

Replace lines in script
 
Hey guys and gals

I've got a difficult one for ya :) I've been trying to work this one out for yonks but not yet been able to do it...

What I want to be able to do is create something like this...

1. Get a file (i.e. html or anything)
2. Grep a line (or more) out of it and replace it with something else
3. Save it in the exact format it was in (apart from changes)

All from a bash script (or something similar)....

example...

Bill
Bob
Jane
Foo

and what i wanna do...

Bill
Johnny
Fred
Foo

Is it even possible?

fr0zen 12-06-2003 10:34 PM

Certainly is. You're looking for this in bash scripting code, right? Not C?



BTW, I really think these questions of yours might fit better in the programming forum :P

AMMullan 12-06-2003 10:40 PM

Either or really, but just not experienced enuf in C yet (wish i was but i got a fair way to go yet)....

fr0zen 12-06-2003 11:35 PM

Try this. It's in bash.

Code:

#!/bin/sh
#
# Replace lines in a file that match the given regex
#
# (c) 2003 Kyle Gibson, free for non-commercial use
# may be redistributed provided this header is left
# in tact. Feel free to modify.
#
# License: GPL
##################################

usage()
{
        echo "Usage: $0 [file] [nfile] [replace] [new]"
        exit 1;
}

# file must be readable
test -r "$1" || usage

# the file we put this into should be specified
test -z "$2" && usage

# if replace is a zero-string
test -z "$3" && usage

# if string is a zero-string
test -z "$4" && usage

FILE="$1"
NFILE="$2"
REGEX="$3"
STR="$4"

# set the new file equal to nothing
echo -n "" > "$NFILE"

cat $FILE | \
while read line; do
        RES="" # reset RES
        RES=`echo -n "$line" | grep $REGEX`# obtain result
       
        if [ -z "$RES" ]; then # if zero-string, then line is not a match; dont replace
                echo "$line" >> "$NFILE" # add to new file
        else # otherwise it is a match, replace this line with the new one
                echo "$STR" >> "$NFILE" #append to new file
        fi
done

Save this to a file, chmod +x the file, and then run:

./script [file to modify] [new file name] [regex to search for] [replace with what]

See the result:

Code:

[frozen@Fr0ZeN frozen]$ cat test
this
is
a simple test
meow
testing
try
[frozen@Fr0ZeN frozen]$ ./script test xtest es "a whole new line"
[frozen@Fr0ZeN frozen]$ cat xtest
this
is
a whole new line
meow
a whole new line
try

Remember it's a regex. If you only provide a string, it'll replace that line if it contains that string. In order to only replace a line if it EQUALS something, do this:

./script test xtest ^es$ "a whole new line"

^ signifies the beginning of a line, $ means the end.

Enjoy.

AMMullan 12-06-2003 11:55 PM

very kewl :)

Thanks heaps

fr0zen 12-07-2003 12:10 AM

I noticed something you should be aware of in using the above code. Don't specify the new file as the original, otherwise ' echo -n "" > "$NFILE" ' ends up erasing all the data inside. I'll see if I can't fix that little problem.

fr0zen 12-07-2003 12:17 AM

Hold on, I'll re-edit this in a minute and post the 'real' code.

Code:

#!/bin/sh
#
# Replace lines in a file that match the given regex
# + new file can be same name as original
#
# (c) 2003 Kyle Gibson, free for non-commercial use
# may be redistributed provided this header is left
# in tact. Feel free to modify.
#
# License: GPL
##################################

usage()
{
        echo "Usage: $0 [file] [nfile] [replace] [new]"
        exit 1;
}

# file must be readable
test -r "$1" || usage

# the file we put this into should be specified
test -z "$2" && usage

# if replace is a zero-string
test -z "$3" && usage

# if string is a zero-string
test -z "$4" && usage

FILE="$1"
NFILE="$2"
REGEX="$3"
STR="$4"

# get the data from file
FDATA=`cat "$FILE"`

# set the new file equal to nothing
echo -n "" > "$NFILE"

echo "$FDATA" | \
while read line; do
        RES="" # reset RES
        RES=`echo -n "$line" | grep $REGEX`# obtain result

        if [ -z "$RES" ]; then # if zero-string, then line is not a match; dont replace
                echo "$line" >> "$NFILE" # add to new file
        else # otherwise it is a match, replace this line with the new one
                echo "$STR" >> "$NFILE" #append to new file
        fi
done


AMMullan 12-07-2003 12:22 AM

How about this...

Code:

#!/bin/sh
#
# Replace lines in a file that match the given regex
#
# (c) 2003 Kyle Gibson, free for non-commercial use
# may be redistributed provided this header is left
# in tact. Feel free to modify.
#
# License: GPL
##################################

usage()
{
        echo "Usage: $0 [file] [nfile] [replace] [new]"
        exit 1;
}

# file must be readable
test -r "$1" || usage

# the file we put this into should be specified
test -z "$2" && usage

# if replace is a zero-string
test -z "$3" && usage

# if string is a zero-string
test -z "$4" && usage

FILE="$1"
NFILE="$2"
REGEX="$3"
STR="$4"
TEMP_STORE=replace_file.$$

cat $FILE | \
while read line; do
        RES="" # reset RES
        RES=`echo -n "$line" | grep $REGEX` # obtain result
       
        if [ -z "$RES" ]; then # if zero-string, then line is not a match; dont replace
                echo "$line" >> "$TEMP_STORE" # add to new file
        else # otherwise it is a match, replace this line with the new one
                echo "$STR" >> "$TEMP_STORE" #append to new file
        fi
done

mv $TEMP_STORE $NFILE

:)

fr0zen 12-07-2003 12:33 AM

You're code looks as though it would work perfectly except for one thing:

mv $TEMP_STORE $NFILE

Would be safer as:

mv "$TEMP_STORE" "$NFILE"

Since it doesn't make any assumptions of the file name.

AMMullan 12-07-2003 12:36 AM

kewl, thanks for the advice :-)


All times are GMT -5. The time now is 12:43 PM.