Change a field value in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change a field value in a loop
# 1  
Old 08-02-2012
Change a field value in a loop

Hi guys, i have an executable file that contains several records and fields. One of the records has a variable filed that must be changed each time i want to execute the file. Would it be possible that i can use a loop to change the value of that field? Suppose that the field address is:
Record number 40, field number 10
and the value of that field must be changed between 0.01 to 1.0 by step 0.01
# 2  
Old 08-02-2012
Post an example of your input and your expected output. Use code tags when doing this.
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 08-02-2012
input(executable file):
Code:
 
LOCSEARCH OCT 20 20 10 0.01 10000 5000 0 1
LOCGRID 500 500 40 -300 -300 0 1.0 1.0 1.0 PROB_DENSITY SAVE
LOCMETH EDT_OT_WT 9999.0 4 -1 -1 -1.68 -6 -1.0 1
#LOCMETH GAU_ANALYTIC 9999.0 4 -1 -1 -1.68 6
LOCGAU 0.2 0.0
 
LOCGAU2 0.01 0.01 5.0

I want to change 2nd field in the record which start with "LOCGAU2" (here is the last record) and as i said before with theses ranges: from 0.01 to 1.0 by step 0.01. It means i have to repeat execution in 100 times, but if i can use a loop that can change that value (2nd field of last record) there is no need for long reparations manually and each time that the loop changes that value, the input will be run with new value for "LOCGAU2" parameter (2nd field of that record) .
# 4  
Old 08-02-2012
Still don't know how this incrementing should look like. Do you want as many output files as you have cycles to increment until that value hits 1.0 or do you want just output where the special line is in or... Here is a try:
Code:
$ awk '/^LOCGAU2/ {print; while($2 <= 1){$2+=0.01; $2=sprintf("%01.2f", $2); print}}' infile
LOCGAU2 0.01 0.01 5.0
LOCGAU2 0.02 0.01 5.0
LOCGAU2 0.03 0.01 5.0
LOCGAU2 0.04 0.01 5.0
LOCGAU2 0.05 0.01 5.0
... shortened ...
LOCGAU2 0.94 0.01 5.0
LOCGAU2 0.95 0.01 5.0
LOCGAU2 0.96 0.01 5.0
LOCGAU2 0.97 0.01 5.0
LOCGAU2 0.98 0.01 5.0
LOCGAU2 0.99 0.01 5.0
LOCGAU2 1.00 0.01 5.0

This User Gave Thanks to zaxxon For This Post:
# 5  
Old 08-02-2012
Actually i wrote a script just like below:

Code:
#!/bin/bash
rm -r ./gmt/*
rm -r ./loc/*
rm -r ./results/*
rm -r ./time/*
Vel2Grid run_7VM_p
Grid2Time run_7VM_p
Grid2Time run_7VM_s
Time2EQ run_7VM_p
for i in {1..7}
do
rm -r ./model/*.*
rm -r ./time/*.*
Vel2Grid run_${i}p
Grid2Time run_${i}p
Grid2Time run_${i}s
NLLoc run_${i}p
done
./result
./statics
#./ploteq
#./ps2jpg

Each time I run this file the script read some files such as “[/SIZE][/FONT][COLOR=black][FONT=Verdana]run_7VM_p” which contains inputs that I show in last session, I want to run this script for 100 times and each time it must run with different “run_7VM_p” file. The change in this file means the change in a record of that file which starts with “LOCGAU2” and then change 2nd field of that record from 0.01 to 1 by step 0.01. so each time the script run, the value of the 2nd field of that record sake of the “run_7VM_p” file will be changed and then run.

Last edited by Scrutinizer; 08-02-2012 at 11:28 AM.. Reason: Cleaned up spurious formatting
# 6  
Old 08-02-2012
Where do you get these run_7VM_p filenames? It can be looped, for sure, but it has to get the names from somewhere. Show us instead of telling us 'record x'.

*.* is a DOS thing incidentally. In UNIX that just means "any files or folders with . in their name somewhere".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to change value in field according to another

I am trying to use awk to check if each $2 in file1 falls between $2 and $3 of the matching $4 line of file2. If it does then in $5 of file2, exon if it does not intron. I think the awk below will do that, but I am struggling trying to is add a calculation that if the difference is less than 10,... (27 Replies)
Discussion started by: cmccabe
27 Replies

2. Shell Programming and Scripting

awk to change value of field using multiple conditions

In the below awk in the first step I default Classification NF-1 to VUS. Next, I am trying to change the value of Classification (NF) to whatever CLINSIG (NF-1) is. If there is only one condition everything works great, but if there are two conditions it does not work. Is the syntax used... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

How to change variable length field?

Hello, I have a file with a date field with various lengths. For example: m/d/yyyy hh:mm or h:mm mm/dd/yyyy hh:mm or h:mm Is there a way using sed or awk to change the field to m/d/y ? I don't need the hours and minutes in that field, just the date in the proper format. Thanks in... (6 Replies)
Discussion started by: sonnyo916
6 Replies

4. UNIX for Dummies Questions & Answers

change field separator only from nth field until NF

Hi ! input: 111|222|333|aaa|bbb|ccc 999|888|777|nnn|kkk 444|666|555|eee|ttt|ooo|ppp With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records). In order to get: 111|222|333|aaa; bbb; ccc 999|888|777|nnn; kkk... (1 Reply)
Discussion started by: beca123456
1 Replies

5. UNIX for Advanced & Expert Users

change field 2 date format

from this input WEBELSOLAR,29122009,1:1 WIPRO,15062010,2:3 ZANDUREALT,18012007,1:3 i want output as WEBELSOLAR,20091229,1:1 WIPRO,20100615,2:3 ZANDUREALT,20070118,1:3 basically input is in ddmmyyyy format and i was to convert it to yyyymmdd format (1 Reply)
Discussion started by: manishma71
1 Replies

6. Shell Programming and Scripting

change a field in a file

Hi, All I want to change a field in the middle of a file such as below 123 456 789 987 654 321 147 258 369 for example i extract 654 in line 2 with following code and sum with 20 EX=`cut -f2 -d " " FILENAME` let EX=$EX+2 if we have too many fields with too many lines, how... (12 Replies)
Discussion started by: mjelecom
12 Replies

7. Shell Programming and Scripting

change field content awk

I have a line like this: I want to move HTTP/1.1 200 OK to the next line and put a blank line between the two lines i.e. How can i get it using awk? Thanks in advance (2 Replies)
Discussion started by: littleboyblu
2 Replies

8. Shell Programming and Scripting

insert EOD if value in Field 1 change

How to create simple awk program data such as below: input: 1 23 1 34 1 12 2 10 2 11 2 12 3 11 3 12 3 13 expected output: 1 23 1 34 1 12 EOD 2 10 2 11 2 12 EOD (2 Replies)
Discussion started by: jmaskar
2 Replies

9. Shell Programming and Scripting

How to change field seperator

Hi Please help me out with this problem: I want to have a script that would change the nth field seperator in a line into something else. like a,d,4,2,97,8,9 into a,d,4,2,97/8/9 Thanks (2 Replies)
Discussion started by: onthetopo
2 Replies

10. Shell Programming and Scripting

sed command to change 2nd field

Hi I am a beginner to sed command, here I have a question about using sed to add a few characters into a token of a string. For example, I have a file, sqw:qqq:123124:uiqe dfd:ccc:12390:dfjis cde:aaa:21311:dfjsid and, I want the output to be, sqw:qqq:123124:uiqe... (4 Replies)
Discussion started by: Julius
4 Replies
Login or Register to Ask a Question