Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
Hello, I need some help searching through multiple files, finding a line and replacing that line. The line I am searching for is:
password key ******* 1222554
ultimately I want to be able to delete the numbers after the asterisks . my thoughts are to create a script that will search for the line password key ******* and delete it then replace it with password key *******
my files are located in /opt and they are all txt files.
Please let me know the best scripting method to get this accomplished.
It is not clear from your example if the file contains real asterisks or you used them to mask real passwords. Anyway you can accomplish this task with a simple sed command. It might be something like this:
Code:
sed -i -r 's/^(password key .+) [0-9]+$/\1/' file
Please, pay attention to the fact that the -i option edit the file in place, actually changing its content. Better if you test it on a safe copy of the original files.
Then you can repeat the command over a bunch of files either by embedding the sed line in a while loop or by passing all the file names as multiple arguments to the sed command itself. In the first case you can do something like:
Code:
while read file
do
sed -i -r 's/^(password key .+) [0-9]+$/\1/' "$file"
done < <(find /opt <your search criteria here>)
where the loop is fed by the result of a find command through process substitution. Hope this helps.
colucix thanks for your help but that didnt work. The password does contain real asterisks. should i be using a -e instead of -r? i tried the -e but there are problems with that.
Nope. The -r option means "make use of extended regexp" and let the + sign be interpreted as "match one or more occurrences of the previous pattern". In the suggested command line I used anchors to match the beginning ^ and the end $ of the line. Maybe there are extra characters in your lines that prevent the exact matching. What if you try without them?
Code:
sed -r 's/(password key .+) [0-9]+/\1/' file
Please, show us the results (or error messages) of your commands together with the sed version you're running, e.g.
Code:
$ cat testfile
password key ******* 1222554
password key ******* 5362578
$ sed -r 's/(password key .+) [0-9]+/\1/' testfile
password key *******
password key *******
$ sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
$ sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
Earlier you said you wanted to "delete the numbers after the asterisks".....
Do you want to:
---delete everything after the asterisks?
---delete a specific pattern after the asterisks?
---delete everything after the asterisks but before another pattern?
---other?
".*" means any number of any character---if you kept it that way, it would by itself go to the end of the line, and the second ".*" would have no effect.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.