[SOLVED] Bash to read text file and replace a line
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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 need a bash script to find and replace a line with the text "//status_id" to "status_id" and then save the modification into the same file.
I searched in the forum some options with "awk" and "sed" to delete the first two characters but i dont know how exactly to do that.
This is the content of the file to find and replace the text.
Code:
// THIS LINES HAVE A SIMPLE TEXT
// THIS LINES HAVE A SIMPLE TEXT
// THIS LINES HAVE A SIMPLE TEXT
// THIS LINES HAVE A SIMPLE TEXT
// THIS LINES HAVE A SIMPLE TEXT
// THIS LINES HAVE A SIMPLE TEXT
// PERFORMANCE PARAMETERS
cl_graph "32"
mp_inco "3.5"
cl_ficas "22"
// CHECK STATUS
//status_id
cl_graph "32"
mp_inco "3.5"
cl_ficas "22"
cl_graph "32"
mp_inco "3.5"
cl_ficas "22"
Last edited by farenheitcx; 11-19-2011 at 01:27 PM.
Many thanks for your quickly reply. I need some help to perform this script for multiple search and replacing words. I try with this but something is wrong..
Code:
#!/bin/bash
ftxt="//status_id cl_ficas" #find words to replace
rtxt="status_id //cl_ficas" #replace words with this
for u in $ftxt
do
for r in $rtxt
do
sed 's/\/\/'"{$u}"'/'"{$r}"'/' file
done
done
sed: -e expression #1, char 19: unknown option to `s'
Code:
#!/bin/bash -x
ftxt="//status_id cl_ficas" #find words to replace
rtxt="status_id //cl_ficas" #replace words with this
for u in $ftxt;
do
for r in $rtxt;
do
sed -i "s/\/\/$u/$r/g" file
done
done
Sorry, I've been talking to someone and writing on LQ. Hence, rather chaotic and incorrect replies. My apologies. Can't concentrate on 2 things at the same time.
Now, do you need to use loops and variables? Your whole script could be replaced with 1 line:
Code:
sed "s@//status_id@status_id@;s@cl_ficas@//cl_ficas@" file1
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.