HJS
HJS

Reputation: 33

How to Replace a string taking an input from another file in Linux Shell script

i have an existing users ID and wanted to change those IDs to users name for e.g.

UserID.txt

Group1 = 1234,1002,2004
group2 = 3214,0032,6632
1234 = Read
6632 = Write

Input_file.csv

search for column1 and replace with column2 from Input_file.csv

1234 onetofour
1002 ten2
2004 tennyfour
3214 threefouteen
0032 thirtytwo
6632 Sixtytwo

Using the Input_file.csv records, i want to replace IDs to names inside UsersID.txt

Expected Output:

Group1 = onetofour,ten2,tennyfour
group2 = threefouteen,thirtytwo,Sixtytwo
onetofour= Read
Sixtytwo = Write

I prefer shell scripting here, kindly suggest for the same.

Thanks

Upvotes: 0

Views: 325

Answers (1)

ushangman
ushangman

Reputation: 46

I love bash and I had some free time.

bash-4.1$ cat UserID.txt
Group1 = 1234,1002,2004
group2 = 3214,0032,6632
1234 = Read
6632 = Write
bash-4.1$ data=$(cat Input_file.csv); awk '{print $1"/"$2}' <<<"$data" | xargs -I {} sed -i 's/{}/g' UserID.txt
bash-4.1$ cat UserID.txt
Group1 = onetofour,ten2,tennyfour
group2 = threefouteen,thirtytwo,Sixtytwo
onetofour = Read
Sixtytwo = Write
bash-4.1$

Upvotes: 1

Related Questions