LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Replacing multiple string in multiple files with awk (https://www.linuxquestions.org/questions/linux-newbie-8/replacing-multiple-string-in-multiple-files-with-awk-4175455111/)

jnorbert 03-22-2013 05:23 AM

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.


Any help will be appreciated,
jnorbert

RaviTezu 03-22-2013 06:59 AM

Here's the script:
Quote:

#!/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.

RaviTezu 03-22-2013 07:01 AM

1.You can copy the script right away into a file.
2.Make it executable(Use chmod).
3.Execute it.

jnorbert 03-22-2013 07:26 AM

Hello RaviTezu,

it works! Thank you for help.

best wishes

grail 03-22-2013 12:23 PM

Before offering a solution, please advise 2 things:

1. Will your string always be comma and space separated?

2. What is the format of the files being changed? ie how do we now which is to be the first file, second file, ...

jnorbert 03-22-2013 01:16 PM

Hi grail,

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.

best wishes

grail 03-22-2013 02:02 PM

Well my suggestion would be something like:
Code:

#!/bin/bash

set -- ${1//, / }

for (( i = 1; i <= $#; i++ ))
do
    sed "/ref_t = x x/s/x/$i/g" file_${i}.txt
done

Call it as follows from within the directory with the files:
Code:

./script.sh '298.00, 299.01, 300.02, 301.03, 302.05, 303.06, 304.09'
Just another way to look at the problem.

ntubski 03-22-2013 02:31 PM

@grail: you meant
Code:

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


grail 03-23-2013 08:04 AM

Sorry ... wasn't getting late ... and yes what he said ;)

David the H. 03-26-2013 12:39 PM

Quote:

Originally Posted by jnorbert (Post 4916650)
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.

Do the numbers stop at 9? Otherwise unless the numbers are zero-padded to the same length they won't naturally sort in numerical order.


All times are GMT -5. The time now is 12:37 PM.