[SOLVED] Replacing multiple string in multiple files with awk
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Replacing multiple string in multiple files with awk
Dear All,
I have scripting problem that I cannot solve. I have one long comma-separated string, like this: 298.00, 299.01, 300.02, 301.03, 302.05, 303.06, 304.09, and so on.
I have multiple text files, that contain a line, similiar like this:
ref_t = x x
I would like to replace x x in the first file with the first value of the string, so the result will be:
ref_t = 298.00 298.00
Then the second file with second string, then the third, fourth, etc.
Replacing single line in multipile files with a single string is working with sed, but I could not solve this issue so far.
#!/bin/bash
echo -e "Please paste the string you have:\n[Note: Make sure you dont have comma/white space at the end]: "
read string
n=`echo $string| grep -o ","|wc -l` #### Counts the number of fields you have in the string ###
n=$(( $n + 1 ))
echo "You have $n fields in the entered string"
m=1
while [ $m -le $n ]
do
y=`echo $string | cut -d"," -f$m | sed 's/ //'`
echo "Please enter the file name in which you'd like to insert $y:\n[Note: Make sure you have the file in the current working directory]"
read fn
sed -i "s:ref_t = x x:ref_t = $y $y:" "$fn" ### You can change the pattern here if you want ###
m=$(( $m + 1 ))
done
I wrote this depending on what i have understand from your post.. I'm posting it here,assuming it may help you in writing your own script.
Yes, the string is always comma and space separated.
The order of the files is depending on their name, they share same name, but are numbered starting from 0, like this:
file_0.txt, file_1.mdp, file_2.txt, etc.
The number of files is always same of the length of the string. With a small modificiation of RaviTezu's script (where it prompts for the file's name, that I want to modify), I think I can automatize it fairly easily.
for (( i = 1; i <= $#; i++ ))
do
sed "/ref_t = x x/s/x/${!i}/g" "file_${!i}.txt"
done
# or maybe
for num
do
sed "/ref_t = x x/s/x/$num/g" "file_$num.txt"
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.