![]() |
Search and replace line in multiple files
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. Thank you, |
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/' fileThen 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 |
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.
Thank you, |
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/' fileCode:
$ cat testfile |
If you use "*" in a regex, it has to be escaped. (Otherwise, it means "any number of the preceding expression or character")
Untested (some pseudo-code): Code:
for fil in ls -R; do |
cat me.txt
password key ******** 225541A222221111F8 $ sed -r 's/(password key .+) [0-9]+/\1/' me.txt password key ********A222221111F8 $ 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. |
That is working as expected.....
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? |
i would like to delete everything after the asterisks on that line. I have multiple lines under there but do not wish to delete any of those.
example blah blah blah text password key ******** 225541A222221111F8 blah blah blah i want this to be: blah blah blah text password key ******** 225541A222221111F8 blah blah blah |
Delete everything after the asterisks:
sed -r 's/(password key \*+).*/\1/' me.txt ".*" 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. |
pixellany, great thank you so much, this is what i needed.
|
OK, another question. I have over 100 files that i need to run through. Can you also help me create a while loop for this?
|
We already gave some examples of how to do the loops.....
The easiest case---if the files are all in one directory, and there are no sub-directories: Code:
for fil in *; doCode:
for fil in *; do |
this seems to search multiple files:
sed -i -r 's/(password key \*+).*/\1/' *.txt looks like i dont need a while loop |
"If it works, it's OK"
|
| All times are GMT -5. The time now is 06:12 PM. |