Changing a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing a text file
# 1  
Old 07-27-2010
Changing a text file

I have a file as below and want to change it using awk

I want to find the entries such as

Code:
Iteration No.788
Best Value        0.00408152
Next-Worst Value  0.00522935
Worst Value       0.00523487

and change it to

Code:
Iteration No.788
788.   Best Value       = 0.00408152
788.   Next-Worst Value = 0.00522935
788.   Worst Value      = 0.00523487


Code:
***********************************************
TSIMPLEX V1001
Non-linear Local Inversion using Simplex Algorithm
**********************************************

Parameter Reading

Base model = m04.base

Reading data file (.dat): P PHASES/SOURCES/DATA Done
DTau = 0.03
MDAcc = 0.1
Mindist = 0.05
Maxitertp = 25
Sigma0 = 1
Travel Time Data file = m04-npt06-sr40-syn.dat
Number of layers = 1

Parametrization of Layer n. 1
P:
NxP = 16
NzP = 12
NxS = 2
NzS = 2
Ni = 2
IntI = LIN
IntP = LIN
IntS = LIN
VarI = -1
VarP = 0.03
VarS = -1
PDegP = 1
PDegS = 1
PDegI = 1

Inmod = m04-npt06-sr40-syn-dp01-16x12drw.vmod
Out file = m04-npt06-sr40-syn-dv0p03-16x12smp.vmod
Expl file = m04-npt06-sr40-syn-dv0p03-16x12smp.expl
Backup file = m04-npt06-sr40-syn-dv0p03-16x12smp.bck
MaxIter = 2000
Tol = 0

Starting SIMPLEX for minimization
Building Initial 193 Vertex Simplex
Initial Simplex

Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9

Iteration No.785
Best Value        0.00408152
Next-Worst Value  0.00523954
Worst Value       0.00524134
Reflection
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Replace W with R
RTol = 0.248474

Iteration No.786
Best Value        0.00408152
Next-Worst Value  0.00523606
Worst Value       0.00523954
Reflection
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Negative contraction
Replace W with Cn
RTol = 0.24782

Iteration No.787
Best Value        0.00408152
Next-Worst Value  0.00523487
Worst Value       0.00523606
Reflection
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Negative contraction
Replace W with Cn
RTol = 0.247595

Iteration No.788
Best Value        0.00408152
Next-Worst Value  0.00522935
Worst Value       0.00523487
Reflection
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Replace W with R
RTol = 0.246558

# 2  
Old 07-27-2010
Try this:
Code:
awk 'f-->0{printf("%s.\t%s%s\t= %s\n",n,$1,$2,$3);next}
/Iteration/{n=substr($2,4); f=3}1' file

# 3  
Old 07-27-2010
It works thanks.

How can I include this in an awk script?

I understand the "printf" and "substr($2,4)" but can't figure out

"f-->0" and "f=3" thing

Last edited by kristinu; 07-27-2010 at 03:15 PM..
# 4  
Old 07-27-2010
Quote:
Originally Posted by kristinu
It works thanks.

How can I include this in an awk script?
What script? What are trying to achieve?

Quote:
Originally Posted by kristinu
I understand the "printf" and "substr($2,4)" but can't figure out

"f-->0" and "f=3" thing
Code:
f=3

If a line matches with /Iteration/ we set a counter f to format the next 3 lines with the code of the 1st line.

Code:
f-->0 {printf("%s.\t%s%s\t= %s\n",n,$1,$2,$3);next}

if f > 0 substract 1 from f and perform the actions between {..}
# 5  
Old 07-27-2010
I understand now.

Yes I am trying to put the awk commands in a file then call it using

awk -f file.awk file.txt > fout.txt
# 6  
Old 07-27-2010
Quote:
Originally Posted by kristinu
I understand now.

Yes I am trying to put the awk commands in a file then call it using

awk -f file.awk file.txt > fout.txt
file.awk:
Code:
f-->0{printf("%s.\t%s%s\t= %s\n",n,$1,$2,$3);next}
/Iteration/{n=substr($2,4); f=3}1

# 7  
Old 07-27-2010
Just one thing, the 1 at the end, does than mean

{ print }
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

2. Shell Programming and Scripting

Help with changing text file layout

Hi there, I am with this one column input text file to change layout, please help. Thanks. I have awk, sed. $ cat input Median 1.0 2.3 3.0 Median 35.0 26.3 45.7 10.1 63.1 Median 1.2 2.3 (8 Replies)
Discussion started by: cwzkevin
8 Replies

3. UNIX for Dummies Questions & Answers

Changing text in multiple files, but with different text for each file

Hello, I have a situation where I want to change a line of text in multiple files, but the problem is that I want to change the text to something unique for each file. For example, let's say I have five files named bob.txt, joe.txt, john.txt, tom.txt, and zach.txt. Each of these files has a... (5 Replies)
Discussion started by: Scatterbrain26
5 Replies

4. Programming

changing text color in c

I'm writing my own Unix ls command in c and I was wondering if anyone knows how to or can point me to a tutorial that shows me how to change the text color of the filename depending on if it's a directory, regular file, linked file, etc..., like the real ls command does? Thanks. (4 Replies)
Discussion started by: snag49ers
4 Replies

5. Shell Programming and Scripting

CHANGING THE TEXT INSIDE A FILE

Hi All, I need a small help in formating a file. I have a file with word like this; ATGHYJIKOLFHJDHDGDYFGFYGRYGFYRHFYHFUED DHDJFDFSJFLFJSKJFSLKJFGHDKLGLDKGLKDNVNV VNLDVLDVDHFJDKDJVNVHSUFNHJFMVJFMVKJMFV ... .... ....like this 75000 words. I want to convert this words into a single... (5 Replies)
Discussion started by: Lucky Ali
5 Replies

6. Shell Programming and Scripting

Changing the text file format

Hi, I have a shell script to unload all the empname who have salary >50000 from the emp table into a text file(empname.txt) . m_db unload "$dbc_file" -column_delimiter ',' -select "SELECT empname FROM emp where salary > 50000" >> empname.txt Now my text file have data in the following format ... (3 Replies)
Discussion started by: kavithakuttyk
3 Replies

7. Shell Programming and Scripting

changing text

i all, I thought I would restructure my question. Is there a way you can get rid of characters in a line if you know what number they are. eg, if this was a line you could see there were 18 characters including spaces. Could I delete characters 4,5,6 and 7 to remove the word this. ... (2 Replies)
Discussion started by: outthere_3
2 Replies

8. Shell Programming and Scripting

changing last 2 digits of text

I have a text file with a bunch of ethernet port names and host names. I need to just change the last 2 chars on each line to a new number how can I do this quickly? from ethernet5/35 Net01 ethernet5/36 Net01 ethernet5/37 Net01 ethernet5/38 Net01 to ethernet5/35 Net02 ethernet5/36... (4 Replies)
Discussion started by: kingdbag
4 Replies

9. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies

10. UNIX for Dummies Questions & Answers

Changing text with sed?

Hi everyone, Having trouble with sed. I searched the board and found some stuff, but still unclear. I have a file named "userfile" which stores the users info in this form: email:username:password: I want the user to be able to change their password. i tried with sed s/oldpass/newpass/g... (2 Replies)
Discussion started by: primal
2 Replies
Login or Register to Ask a Question