LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-06-2003, 10:30 PM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Rep: Reputation: 30
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?
 
Old 12-06-2003, 10:34 PM   #2
fr0zen
Member
 
Registered: Nov 2003
Location: 127.0.0.1
Distribution: xubuntu
Posts: 217

Rep: Reputation: 30
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
 
Old 12-06-2003, 10:40 PM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
Either or really, but just not experienced enuf in C yet (wish i was but i got a fair way to go yet)....
 
Old 12-06-2003, 11:35 PM   #4
fr0zen
Member
 
Registered: Nov 2003
Location: 127.0.0.1
Distribution: xubuntu
Posts: 217

Rep: Reputation: 30
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.
 
Old 12-06-2003, 11:55 PM   #5
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
very kewl

Thanks heaps
 
Old 12-07-2003, 12:10 AM   #6
fr0zen
Member
 
Registered: Nov 2003
Location: 127.0.0.1
Distribution: xubuntu
Posts: 217

Rep: Reputation: 30
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.
 
Old 12-07-2003, 12:17 AM   #7
fr0zen
Member
 
Registered: Nov 2003
Location: 127.0.0.1
Distribution: xubuntu
Posts: 217

Rep: Reputation: 30
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

Last edited by fr0zen; 12-07-2003 at 12:30 AM.
 
Old 12-07-2003, 12:22 AM   #8
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
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

Last edited by AMMullan; 12-07-2003 at 12:34 AM.
 
Old 12-07-2003, 12:33 AM   #9
fr0zen
Member
 
Registered: Nov 2003
Location: 127.0.0.1
Distribution: xubuntu
Posts: 217

Rep: Reputation: 30
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.
 
Old 12-07-2003, 12:36 AM   #10
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
kewl, thanks for the advice :-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 08:40 AM
bash script to replace char in variable gmartin Programming 5 11-08-2004 02:46 PM
removing lines from file script iluvatar Programming 9 08-20-2004 06:49 AM
Can't get lines of a file with a Bash script.. barisdemiray Programming 2 08-11-2004 01:42 PM
Replace blank/almost blank lines in file Wynd Linux - General 3 01-27-2004 05:49 PM


All times are GMT -5. The time now is 08:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration