Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this.

string(Scenario 1)- user::r--,user::ourfrd:r--
String(Scenario 2)- user::r--

File
****

# file: /local/Desktop/myfile
# owner: me
# group: mygroup
user::rwx
user:myfrd:rwx
user:hisfrd:r-x
group::r-x
group:mygroup:rwx
mask::rwx
other::r-x

Required Output :
***************

For Scenario-1 :

# file: /local/Desktop/myfile
# owner: me
# group: mygroup
user::r--
user:myfrd:rwx
user:hisfrd:r-x
user::ourfrd:r--
group::r-x
group:mygroup:rwx
mask::rwx
other::r-x

For Scenario-2 :

# file: /local/Desktop/myfile
# owner: me
# group: mygroup
user::r--
user:myfrd:rwx
user:hisfrd:r-x
group::r-x
group:mygroup:rwx
mask::rwx
other::r-x

Hi. Welcome.

What operating system exactly and what shell are you using?

What code have you written so far to attempt to solve this?

@ Neo : Windows os only and im using Unix Shell/Bash scripting.
I have applied the below logic to eliminate comma if available in the string and then kept each string in array. Then I tried to search it in the file,but replacing the contents in the file is not working as expected.

For Scenario-1 :

#/bin/bash
STRING="user::r--,user:ourfrd:r--"
array=`echo $STRING | awk -F ',' '{ s = $1; for (i = 2; i <= NF; i++) s = s "\n"$i; print s; }'`
for word in ${array}
do
        sed -r -e 's/${array}[^{array}]+//g' file.txt
done

For Scenario-2 :

#/bin/bash
STRING="user::r--"
array=`echo $STRING | awk -F ',' '{ s = $1; for (i = 2; i <= NF; i++) s = s "\n"$i; print s; }'`
for word in ${array}
do
        sed -r -e 's/${array}[^{array}]+//g' file.txt
done

Not sure I understand your request. Does this work for your scenario 1 2?

echo "${STRING//,/$'\n'}" | awk -F: 'FNR==NR {T[$1,$2] = $0; next} ($1,$2) in T {$0 = T[$1,$2]} 1' - file
1 Like

@Reduic : Please find the below explanation on my requirements for scenario-1.

Input parameter:
********************
Scenario-1:

STRING="user::r--,user::ourfrd:r--"

Data available in File:
**************************
# file: /local/Desktop/myfile
# owner: me
# group: mygroup
user::rwx
user:myfrd:rwx
user:hisfrd:r-x
group::r-x
group:mygroup:rwx
mask::rwx
other::r-x

Expected Results - Modified File data
**********************************************
For Scenario-1:
# file: /local/Desktop/myfile
# owner: me
# group: mygroup
user::r--
user:myfrd:rwx
user:hisfrd:r-x
user:ourfrd:r--
group::r-x
group:mygroup:rwx
mask::rwx
other::r-x

Note: In the above result, the string "user:ourfrd:r--" is added newly to the file since it is not available already in file. Only the partially matching strings to be replaced like how we have "user::rwx" exists in file and the first few characters "user::" is getting matched with new input parameter "user::r--". so "user::r--" needs to be updated in file. This is what im trying for, hope now my requirement is clear to you ..

Sorry - I meant scenario 2 in my former post. For your scenario 1, try

echo "${STRING//,/$'\n'}" | awk -F: '
FNR==NR         {T[$1,$2] = $0
                 next
                }
($1,$2) in T    {print T[$1,$2]
                 delete T[$1,$2]
                 next
                }
LAST &&
$1 != LAST      {for (t in T)   {print T[t]
                                 delete T[t]
                                }
                }
                {LAST = $1
                }
1
' - file

@Rudic : I am looking for solution for both the scenario's.

--- Post updated at 12:13 PM ---

@Rudic : For scenario-1, it is not working as expected. Instead of comparing the existing one, it is appending the strings in first line.

For scenario-2 which you have said initially worked partially. i.e. For partially matching pattern it updated with new strings which we passed as Input parameter. If the complete strings is not matched then it has to be added to the file as new record which doesn't work.