Need to replace a number from string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to replace a number from string
# 1  
Old 02-25-2011
Need to replace a number from string

Dear all,
I need your help with some shell scripting which I have just started. I have a file whose content look something like:

Pressure 1 1 1e5
Temperature 1 1 300
Volume 1 1 2e-20

And there are other lines too, but these are the lines i wish to play with. These files are kept in different folders and I wish to change say temperature in each of these folders in systematic fashion say a step of 5. The columns with numbers 1 1 need not change.

sed command is not useful here because 300 is just an example. Temperature may not be 300 in each file. I need a way to do this using shell script. What I do currently is to use vim and change these values every time. But its very cumbersome. Is there a way to change a particular number(in this case 300) from a line containing a particular word(in this case Temperature) without altering other contents of line?
# 2  
Old 02-25-2011
Code:
awk '/Temperature/{$NF=$temp}1' file

# 3  
Old 02-25-2011
Please give more clue about the name and the PATH of the files containing such kind of data
Then we may help to automate making change in it.

---------- Post updated at 07:13 PM ---------- Previous update was at 07:11 PM ----------

Code:
nawk -v STEP="$1" '/Temperature/{$NF+=STEP}1' infile

Code:
# cat tst
Pressure 1 1 1e5
Temperature 1 1 300
Volume 1 1 2e-20

Code:
# nawk -v STEP="-5" '/Temperature/{$NF+=STEP}1' tst
Pressure 1 1 1e5
Temperature 1 1 295
Volume 1 1 2e-20


Last edited by ctsgnb; 02-25-2011 at 02:20 PM..
This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 02-25-2011
Name of the file is Input
I have directories named 190,195,...300
Each one has one Input file in which one of the line is Temperature
I want to replace random value of Temperature in each of those Input files by its directory name.

---------- Post updated at 11:52 PM ---------- Previous update was at 11:44 PM ----------

I am sorry, I should have mentioned this before. The problem here with awk is that it doesn't write the modified line in the file. I want the line in file to be changed.
# 5  
Old 02-25-2011
Should the directory name have the same value than the temperature ?
# 6  
Old 02-25-2011
Yes it should have same value.
# 7  
Old 02-25-2011
See if this code can help you out

Code:
 find ./ -type d -maxdepth 1|sed 's|./||'|while read line
do
cd $line
sed -i -r "/Temperature/s/(.*1) (.*)/\1 $line/" *
cd ..
done

This User Gave Thanks to yinyuemi For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Replace all string matches in file with unique random number

Hello Take this file... Test01 Ref test Version 01 Test02 Ref test Version 02 Test66 Ref test Version 66 Test99 Ref test Version 99 I want to substitute every occurrence of Test{2} with a unique random number, so for example, if I was using sed, substitution would be something... (1 Reply)
Discussion started by: funkman
1 Replies

3. Shell Programming and Scripting

Replace first number of each line in a file with another number

Hi, I have a set of files in a directory that I have to read and replace the first occurrence of a number with another dummy number. This is what I have so far but it does not seem to work. The files have lot of other data in each row and each data element is separated by ,@, for file in... (13 Replies)
Discussion started by: scorpioraghu
13 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

6. Shell Programming and Scripting

Replace x Number of String Occurrence with Sed

Ok, So I have a huge file that has over 12000 lines in it. in this file, there are 589 occurrences of the string "use five-minute-interval" spread in various areas in the file. How can i replace the the last 250 of the occurrences of "use five-minute-interval" with "use... (10 Replies)
Discussion started by: SkySmart
10 Replies

7. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

8. UNIX for Dummies Questions & Answers

Sed to replace second number in a random string

I need a sed line that will take STDM111 and change it to STDM161 the STDM will always be constant but the 3 numbers after will be random, I just need it to always replace the middle number with 6 regardless of what the numbers are. (8 Replies)
Discussion started by: glev2005
8 Replies

9. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

10. 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
Login or Register to Ask a Question