1

I have the following file format

...
MODE P E
IMP:P 1 19r 0
IMP:E 1 19r 0
...
SDEF POS= 0 0 14.6 AXS= 0 0 1 EXT=d3 RAD= d4 cell=23 ERG=d1 PAR=2
SI1 L  0.020
SP1    1
SI4 0. 3.401                                                                    
SI3 0.9 
...
NPS 20000000 

What I am trying to do is to locate a specific value(in particular the value after the sequence SI1 L) and create a series of files with different values. For instance ST1 L 0.020--->ST1 L 0.050. What I have in mind is to give a start value, an end value and a step so as to generate files with different values after the sequence SI1 L. For instance a for loop would work, but I don't know how to use it outside awk.

I am able to locate the value using

awk '$1=="SI1" {printf "%12s\n", $3}' file

I could also use the following to replace the value

awk '$1=="SI1" {gsub(/0.020/, "0.050"); printf "%12s\n", $3}' file

The thing is that the value won't always be 0.020. That's why I need a way to replace the value after the sequence SI1 L and this replacement should be done for many values.

How can this be acheived?

Thanos
  • 594
  • 2
  • 7
  • 28
  • What should be the content of the files you want to create? How are the start and end value related to the content of the files? – Håkon Hægland Jan 22 '14 at 20:53
  • @HåkonHægland: Thank you very much for your comment! The output file will be exactly the same as the input one, except from the different value after the sequence `SI1 L `. – Thanos Jan 22 '14 at 20:59

2 Answers2

1

You can try:

awk -vval="0.05" '$1=="SI1"{$3=val}1' file

This will replace SI1 L 0.020 by SI1 L 0.05 in the input file.

Then use a bash script to call the awk program in a for loop..

For instance:

#! /bin/bash

vals=(0.02 0.03 0.04 0.05)

i=0
for val in "${vals[@]}"; do
  i=$(($i+1))
  awk -vval="$val" '$1=="SI1"{$3=val}1' file > "file${i}"
done
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • Thank you very much for your answer! How would the output files be generated? I used `awk -vval="$val" '$1=="SI1"{$3=val}1' file >$vals[@]` but it's not working. – Thanos Jan 22 '14 at 21:21
  • @Thanos No problem :) I have updated my answer with a suggestion.. Now the files are named `file1`, `file2`, .. , and so on.. – Håkon Hægland Jan 22 '14 at 21:25
  • Preferably I would like the output file to be named with the name of the value at the position `i`. For instance the output files will be `0.02`, `0.03`, `0.04` and so on. – Thanos Jan 22 '14 at 21:29
  • @Thanos Ok.. Then you could try `awk -vval="$val" '$1=="SI1"{$3=val}1' file > "${val}"` – Håkon Hægland Jan 22 '14 at 21:32
1

If your system has seq command, here is easier script for you.

for val in $(seq 0.02 0.01 0.05)
do 
  awk -vval="$val" '/SI1 L/{$3=val}1' file > "${val}"
  # or Using sed
  # sed: sed "s/SI1 L .*/SI1 L $val/" > "${val}"
done
BMW
  • 42,880
  • 12
  • 99
  • 116