Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
#!/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.
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.
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
#!/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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.