Home > Enterprise >  Edit a particular string in a file based on another file
Edit a particular string in a file based on another file

Time:03-23

Hello I have a file called users. In that file i have a list of users for example

user1 user2 user3

Now i have another file called searches where there is a specific string called owner = user for example

owner = user1
random text 
random text
owner = user15
random text
random text
owner = user2

so is it possible to find all the users based on the users file and rename those users to [email protected] ? for example

owner = [email protected]
random text 
random text
owner = user15 
random text
random text
owner = [email protected]

Currently what i am doing is the long manual process like below

awk '/owner = user1|owner = user2|owner = user3/{print $0 "@domain.com"; next}1' file

and it actually does work but for large files i have to spend a long time creating this command.

CodePudding user response:

Given:

$ head users owners
==> users <==
user1 user2 user3

==> owners <==
owner = user1
random text 
random text
owner = user15
random text
random text
owner = user2

You can use this awk:

awk 'BEGIN{FS="[ \t]*=[ \t]*|[ \t] "}
FNR==NR{for (i=1;i<=NF;i  ) seen[$i]; next}
/^owner[ \t]*=/ && $2 in seen{sub($2, $2 "@domain.com") } 1' users owners

Prints:

owner = [email protected]
random text 
random text
owner = user15
random text
random text
owner = [email protected]
  • Related