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.
If I have a line like AA=<value> in the file, and the <value> is not a constant. For example, it could be AA=BB or AA=CC. How can I replace the <value> with a specified string such as 'DD'? Thus, AA=BB can become AA=DD and AA=CC can also become DD. Don't want the whole line replaced, just the <value> part.
Yes you can do that with both sed and awk. If you know the length of the value; if that's static (always two characters) then you could use sed like this:
Code:
sed -i 's/=[A-Z][A-Z]/=DD/g' yourfile
The above will change all instances of '=' followed by two capital characters from the alphabet to =DD. Of course if the value is outside this scope; for example including numbers or being larger then two characters, you'll have to adapt the regex.
Sed will execute the changes inside de document without output on the screen. If you want to view the results on screen remove the -i. If you want the result in another file remove the -i and redirect output to another file.
Sed can do this for sure. Is AA=value unique within the file? Is AA=value the only line in the file?
You would need something like
Code:
cat file | sed 's/^\(AA=\)\(.*\)/\1Insert Your New Value/' > file.tmp
mv file.tmp file
The above expression applies only to lines where the line starts with AA=. Change "Insert Your New Value" with whatever you want the new value of AA= to be.
Look up "regex sed" or "sed regex" for more information on how to form accurate expressions to suit your purpose.
@sag47:
- No need to 'cat' and pipe to 'sed'
- You should use "-i" (in-place) instead of redirect output to a file and move it back
I prefer backups for my backups. It's not like my command wouldn't work (there's a 1000 ways to build a house). Whatever the case may be, as Eric puts it, the OP can easily modify all the information we've given him/her to their liking. Thanks for the tips.
# cat file
AA=BB dog cat AA=CC dog cat
mouse rat AA=xxxxxAA=yyyyy
# ruby -e 's=readlines.join.split("AA=").each{|x| x.sub!(/^.[^\s]*/,"AA=DD")};print s.join' < file
AA=DD dog cat AA=DD dog cat
mouse rat AA=DDAA=DD
sed is greedy.
Code:
# sed 's/^\(AA=\)\(.*\)/\1DD/' file
AA=DD
mouse rat AA=xxxxxAA=yyyyy
Shouldn't matter if it is a settings/config file where usually it is one setting per line in a setting=value format. I designed the regex to be greedy for simplicity's sake. It can be refined to be less or more greedy you just have to modify the regex.
Using ruby is an interesting solution for it though.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.