Dynamic search and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamic search and replace
# 1  
Old 03-20-2013
Dynamic search and replace

Hi,

I am trying to search and replace a value in a file. File contains data which is stated below

ruby ./scripts/CreateUsage.rb Cloud_Computing_001

Cloud_Computing_001 is the 3rd column of the file. I want to replace this Cloud_Computing_001 with the runtime value. Next time i want to change the new replaced value with another runtime value. So it should dynamically find the values in the 3rd column of a file and replace that value with runtime value.

For this i used some script like below
Code:
if [ "$1" = "" ]; then        ## No parameters were entered
  echo "Enter value:"
  read mInputValue
  echo "You entered: "$mInputValue
  b=`cat ssss|awk '{print $3}'`
  sed -i.bak "s|${b}|${mInputValue}|g" ssss

This is working if a file contains single line . Suppose if a file contains multiple lines. It should replace all the lines. which is nothappening now , could some one help me out on this.

Last edited by Franklin52; 03-20-2013 at 11:47 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 03-20-2013
try this


Code:
`awk ' NR==1 { print $2}'  filename`

# 3  
Old 03-22-2013
No, the above code is not working

How to dynamically find and replace the value.
Can anyone help me on this.
# 4  
Old 03-22-2013
Your request is very difficult to read and to understand. Meaningful samples of e.g. the file and the desired output had helped a lot, as is also stated in the forum rules.

If I apply some wild guessing to your request, I come to the conclusion that you want to modify file ssss by changing the 3rd field of every row to the value the user entered, either as a command line parameter (which, BTW, is lost in your above code snippet), or as the keyboard input interactively. Would this do the task:
Code:
awk '{$3=mInpVal}' mInpVal=$mInputValue ssss

?
# 5  
Old 03-22-2013
The first step is to explain the problem. Give a few scenarios with input and output. Show what happens in the real world situation. Then the gurus (not me) can help figure it out. Or once you actually write down the examples, the answer might come to you all by your self.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace sql with dynamic values

Hi Guys, I am using a function to replace the values dynamically to frame sql query by reading a file. My file will have column names like file.txt col_1 col_2 expected output: select id,col_1,col_2 from ( select a.id, a.col_1, rank() over (ORDER BY cast(a.col_1 AS double)... (5 Replies)
Discussion started by: Master_Mind
5 Replies

2. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

3. Shell Programming and Scripting

Dynamic number to replace with line

Hi, I have file as below COMMIT # at 34572 # at 23432 COMMIT # at 5674 # at 7856 I want to replace with as below (12 Replies)
Discussion started by: kaushik02018
12 Replies

4. Shell Programming and Scripting

Complex find and replace only 1st instance string with dynamic combination

test.txt is the dynamic file but some of combination are fix like below are the lines ;wonder_off = ;wonder_off = disabled wonder_off = wonder_off = disabled the test.txt can content them in any order #cat test.xt ;wonder_off = ;wonder_off = disabled wonder_off = wonder_off =... (5 Replies)
Discussion started by: SilvesterJ
5 Replies

5. Shell Programming and Scripting

how to make pattern search dynamic in awk

Hi, I have a data in a file like below - andy 22 abc 30000 wallstreet paul 30 xyz 40000 martstreet john 35 abc 50000 martstreet I want to search number of employees working in a particular company. Below query executes perfectly - awk '/abc/{ COUNT ++; }END { print "number of... (3 Replies)
Discussion started by: shell123
3 Replies

6. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

7. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

8. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

9. Shell Programming and Scripting

sed - dynamic search and replace

Hi all, I have a data file formatted as in the following line: Achadd 0:35 1:35 2:35 3:40 4:40 5:40 I need the minutes converted to seconds; I wrote a script, min2sec, to do so for one datapoint. I was hoping to use sed as in the following code to call this script and... (4 Replies)
Discussion started by: x-375HK-x
4 Replies

10. Shell Programming and Scripting

search and replace dynamic data in a shell script

Hi, I have a file that looks something like this: ... 0,6,256,87,0,0,0,1187443420 0,6,438,37,0,0,0,1187443380 0,2,0,0,0,10,0,1197140320 0,3,0,0,0,10,0,1197140875 0,2,0,0,0,23,0,1197140332 0,3,0,0,0,23,0,1197140437 0,2,0,0,0,17,0,1197140447 0,3,0,0,0,17,0,1197140543... (8 Replies)
Discussion started by: csejl
8 Replies
Login or Register to Ask a Question