hi guys,
i'm writing a script that looks for a unquie id in a file and replaces a string between two square brackets on the same line as the unquie id:
.......
.......
0001 zz 43242 [replace this text] name
0002 sd 65466 [UK] country
.......
.......
how can i find line with id 0001 and then replace only the text between the brackets?
# echo "0001 zz 43242 [replace this text] name" | sed '/^0001/s/\[.*\]/anything/'
0001 zz 43242 anything name
# cat input
0001 zz 43242 [replace this text] name
0002 sd 65466 [UK] country
0001 zz 43242 [replace this text] name
0001 zz 43242 [replace this text] name
0002 sd 65466 [UK] country
0002 sd 65466 [UK] country
0001 zz 43242 [replace this text] name
0002 sd 65466 [UK] country
0002 sd 65466 [UK] country
0002 sd 65466 [UK] country
# sed '/^0001/s/\[.*\]/anything/' input
0001 zz 43242 anything name
0002 sd 65466 [UK] country
0001 zz 43242 anything name
0001 zz 43242 anything name
0002 sd 65466 [UK] country
0002 sd 65466 [UK] country
0001 zz 43242 anything name
0002 sd 65466 [UK] country
0002 sd 65466 [UK] country
0002 sd 65466 [UK] country
#
...for further clarity:
what i'm trying to do is replace the string between the brackets with its MD5 value. I'm basically anonymising the data. I can only search the file using the id, as i have no idea what the string-to-be-replaced is.
Regards,
Zaff
---------- Post updated at 04:58 PM ---------- Previous update was at 04:55 PM ----------
Thanks for the quick response!
.......and how about if i wanted to keep the square brackets?
Thanks,
Zaff
---------- Post updated at 05:01 PM ---------- Previous update was at 04:58 PM ----------
...sorry, one more thing:
how can i assign the value between the brackets to a variable?
Please give more clue :
what is your input ?
what is your expected output ?
What do you want to do with the MD5 (log it into a file ? sometyhing else ?)
command similar to one u provided, but assigning the string in the brackets to a variable. i will then use this variable to generate an md5 (this i can do).
replace the content of the brackets with the md5. This command u have already kindly provided:
echo "0001 zz 43242 [replace this text] name" |sed '/^0001/s/\[.*\]/my_md5/'
........only thing is, i need the brackets to remain as they are and not be removed along with the original string.