awk line editing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk line editing
# 1  
Old 02-02-2009
awk line editing

Hello I am trying to make an awk script that always replaces the same characters in a file, with white space in between. Can anyone write a script that prompts for the 3 numbers and then replaces them in the file?
eg.
enter a b c:
'it then puts a b c in the file.

The file format is always

npts 40 40 40 # num.grid points in xyz
gridfld rec_flex.maps.fld # grid_data_file
spacing 0.375 # spacing(A)
receptor_types A C HD N OA # receptor atom types
ligand_types C HD N OA # ligand atom types
receptor rec_flex.pdbqt # macromolecule
gridcenter 9.9 -16.8 -7.3 # xyz-coordinates or auto
smooth 0.5 # store minimum energy w/in rad(A)
map rec_flex.C.map # atom-specific affinity map
map rec_flex.HD.map # atom-specific affinity map
map rec_flex.N.map # atom-specific affinity map
map rec_flex.OA.map # atom-specific affinity map
elecmap rec_flex.e.map # electrostatic potential map
dsolvmap rec_flex.d.map # desolvation potential map
dielectric -0.1465 # <0, AD4 distance-dep.diel;>0, constant

it is these 3 numbers that need to be changed:
gridcenter 9.9 -16.8 -7.3
the resulting file is ref.gpf
# 2  
Old 02-02-2009
With awk you can do something like:

Code:
awk 'BEGIN{
  printf "Enter the values separated by a space : "
  getline var < "-"
  split(var,a)
}
/gridcenter/{$2=a[1]; $3=a[2]; $4=a[3]}
{print}' file

Regards
# 3  
Old 02-02-2009
Thanks, this works great but I need to update the original rather than to std output.
Thank you.
# 4  
Old 02-02-2009
Redirect the output of the awk command to a temporary file and replace the original file with it:

Code:
awk '...' file > tempfile
mv tempfile file

These are basic shell commands, have a read of one of the tutorials here:

https://www.unix.com/answers-frequent...tutorials.html

Regards
# 5  
Old 02-02-2009
yes that was the 1st thing I tried but the script failed. I will pursue this.
gav
# 6  
Old 02-04-2009
Hi when I use > or | the script fails. Why's this?
The link didn't.
gs
# 7  
Old 02-04-2009
May be you don't have a permission to create a file in that directory.
Basically the elimentary nature of > should write to a file as franklin has given its fine.
You can also "touch" a file and then append the out put to that file.
You will come to know while "touch" when u can create the files in that dir.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Editing in vi - adding words to a line

i have lines in a file similar to this: results=$(echo Total: ${res} | command | command) now I need to add text before the word "results", but i dont know how to do it. here's what i tried: %s~results=.*echo Total:.* ${7}~PROCESS \; results=.*echo Total:.* ${7}~g the problem is,... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. Shell Programming and Scripting

Editing the next line of the pattern

Hi All, I am trying to edit the next line when there is a success for a pattern. For example: <nvname>abc</nvname> <nvvalue>1</nvvalue> <nvname>def</nvname> <nvvalue>2</nvvalue> once there is a hit for the string "abc" the value of the nvvalue should be replaced with the value we... (5 Replies)
Discussion started by: pulgupta
5 Replies

3. Shell Programming and Scripting

Editing the first line of file

I have a sample file as below :- RECORD_COUNT|2660|28606946.86|20110701122349694| adkad|wwsgesg|mkmk|FFFF|FAFAF|FFAF|FAFFFFA|5894858| I have to replace the second coulmn in the first row which is the count of no of lines in the file from a new variable . Also I have to delete the 3rd... (2 Replies)
Discussion started by: Sanjeev Yadav
2 Replies

4. UNIX for Dummies Questions & Answers

Help with editing 2nd line after match

Hi All I would like to break down each next line that matches SK1.chr* in this case NNNNNN.... into 100 characters each after SK1.chr*... (3 Replies)
Discussion started by: pawannoel
3 Replies

5. Shell Programming and Scripting

Line/Variable Editing for Awk sed Cut

Hello, i have a file, i open the file and read the line, i want to get the first item in the csv file and also teh third+6 item and wirte it to a new csv file. only problem is that using echo it takes TOO LONG: please help a newbie. below is my code: WorkingDir=$1 FileName=`cut -d ',' -f... (2 Replies)
Discussion started by: limamichelle
2 Replies

6. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

7. UNIX for Dummies Questions & Answers

Line editing

I have some trouble editing a line. My aim is to edit the first line of a file.It will look the code and if there are dummy characters at the begininng of line before < it will erase. It is a php code.For instance: for instance if the first line is like %^<?php>sadsa</php>, it will change it to... (6 Replies)
Discussion started by: king87
6 Replies

8. Shell Programming and Scripting

Single line file editing command?

Hello everyone. I have been reading a lot about the various different text editors at my disposal through Unix, but I just can't seem to close the deal for what I am trying to do. Is there a way to issue a single line command to edit a file where pattern=x, and do it non-destructively AND in-place?... (1 Reply)
Discussion started by: gator76
1 Replies

9. Shell Programming and Scripting

Editing last line of a file

Hi All, Could you please help me out with this problem? I need to edit the last line of my file. Ex: The last line in my file will be say 000056000045 8 I need to subtract some number from the number 45 and replace the new number in its place. ... (1 Reply)
Discussion started by: Anitha Chandran
1 Replies

10. UNIX for Dummies Questions & Answers

editing bash command line with vi

Is there a way using bash that I can edit a command line using vi. I.e. if I have a long command line and I want to edit it.....by typing vi and then having the command open in an editing window.... I beleive this can be done in k shell by pressing v....however can find out how this can be... (3 Replies)
Discussion started by: peter.herlihy
3 Replies
Login or Register to Ask a Question