[SOLVED] Replace string in several files at position
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
It is also important that you get to the point where you can build or extend regular expressions on your own. One, it will save you effort. Two, you encounter them everywhere these days.
When you have the working solution, which I think you will in the next few minutes, try looking at the manual page already mentioned and working through this tutorial: http://www.regular-expressions.info/quickstart.html to analyze the components of the pattern used.
MADEINGERMANY, Thank you so much! the solution you provided worked like a charm! yes, there are other expressions with less characters in the string that I might need to also modify so I will play with the solution you provided to change those expressions.
Since I have thousands of files I need to modify and they are located in multiple sub folders within a folder, should I run the code as shown below?
Code:
sed -i -r 's/^\([[:space:]]*ACCT:[[:space:]]*\)[0-9]\{12\}/\1XXXXXXXXXXXX/' /home/user/folder/*
... and, to illustrate just how subtle regular-expressions ("regexes") can be, it is the "^" character which says, "anchor to start-of-line." (Similarly, "$" anchors to end-of-line.)
There are many regex tester web-sites out there which are frankly worth their weight in gold. They let you type in a regex, and a test string, and they will actually show you the logic that is applied to obtain a "match" or "doesn't match" result. They will also point out any syntax-errors in your regex. "Don't leave home without it.™"
Since I have thousands of files I need to modify and they are located in multiple sub folders within a folder, should I run the code as shown below?
Personally, I use the find command to produce a list of the files that it considers to be "matches." Then, I e-y-e-b-a-l-l that list ... several times. (I'm only gonna get one chance at this ...)
Finally, I apply that list, e.g.:
cat listfile | xargs -I{} mycommand "{}"
(Notice the use of "quotes" in the substituted command, to handle the case of a path-string that contains spaces.)
Sometimes I'll start by executing [font=courier]echo commands that echo the command-strings I am about to execute. Sometimes, I'll capture that echo-output into a file, give it one... last... look..., then execute that file.
That degree of caution has saved my left foot many times.
Be sure to look up what options -i and -r do. The parenthesis and braces have to be written normally if you use -r. And -i usually takes some string with it.
Code:
sed -r 's/^([[:space:]]*ACCT:[[:space:]]*)[0-9]{12}/\1XXXXXXXXXXXX/' /home/user/folder/*
Also check the references mentioned for explanations of all the components in the regular expression:
Thank you! I was able to modify the command to be able to change other strings in the same file which contain less/more characters and they all worked okay. I will def read up on 'sed' and the references mentioned above. I will also do more testing before changing all files. Thank you again!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.