LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
 
Search this Thread
Old 03-22-2013, 05:23 AM   #1
jnorbert
LQ Newbie
 
Registered: Mar 2013
Posts: 5

Rep: Reputation: Disabled
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
 
Old 03-22-2013, 06:59 AM   #2
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
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.

Last edited by RaviTezu; 03-22-2013 at 10:32 AM.
 
1 members found this post helpful.
Old 03-22-2013, 07:01 AM   #3
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
1.You can copy the script right away into a file.
2.Make it executable(Use chmod).
3.Execute it.
 
1 members found this post helpful.
Old 03-22-2013, 07:26 AM   #4
jnorbert
LQ Newbie
 
Registered: Mar 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hello RaviTezu,

it works! Thank you for help.

best wishes
 
Old 03-22-2013, 12:23 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,900

Rep: Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096
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, ...
 
Old 03-22-2013, 01:16 PM   #6
jnorbert
LQ Newbie
 
Registered: Mar 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-22-2013, 02:02 PM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,900

Rep: Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096
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.
 
Old 03-22-2013, 02:31 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,681

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
@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
 
Old 03-23-2013, 08:04 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,900

Rep: Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096
Sorry ... wasn't getting late ... and yes what he said
 
Old 03-26-2013, 12:39 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951
Quote:
Originally Posted by jnorbert View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Substitute string in multiple files learning01 Linux - Newbie 7 09-11-2012 10:43 AM
Find a String in Multiple Files raihan26 Linux - Newbie 3 07-28-2012 03:04 AM
[SOLVED] Awk on multiple .gz files BarataPT Programming 3 03-22-2011 05:13 AM
Replacing text in multiple files ? centosfan Linux - General 10 09-20-2008 07:25 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM


All times are GMT -5. The time now is 01:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration