Make change to variable value inside of awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Make change to variable value inside of awk script
# 1  
Old 02-06-2018
Make change to variable value inside of awk script

Hello,

I have text data that looks like this,
Code:
  Mrv16a3102061815532D          

  6  6  0  0  0  0            999 V2000
   -0.4018    1.9634    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.1163    1.5509    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.1163    0.7259    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.4018    0.3134    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
    0.3127    0.7259    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.3127    1.5509    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  2  3  2  0  0  0  0
  3  4  1  0  0  0  0
  4  5  2  0  0  0  0
  5  6  1  0  0  0  0
  1  6  2  0  0  0  0
M  END
>  <id>
1
>  <name>
pyridine
>  <mw>
79.102
$$$$

I have the following awk code that looks for the > <name> tag, stores the value on the next line, and then writes it to the first line of the file.

Code:
# name field tag to look for
name_field='<name>'
# value to add to beginning of name string
pre='ID_'
awk -v find_name=$name_field -v pre=$pre ' { OUT[++CNT] = $0 }
                                      F==1 { NAME = pre$0; F = 0 }
                            $0 ~ find_name { F = 1 }
                              $0 == "$$$$" { print NAME; for(i=2; i<=CNT; i++) print OUT[i]; delete OUT; CNT = 0 }
                                         ' > $output_file_name

The results look like this,
Code:
ID_pyridine
  Mrv16a3102061815532D          

  6  6  0  0  0  0            999 V2000
   -0.4018    1.9634    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.1163    1.5509    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.1163    0.7259    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.4018    0.3134    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
    0.3127    0.7259    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.3127    1.5509    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  2  3  2  0  0  0  0
  3  4  1  0  0  0  0
  4  5  2  0  0  0  0
  5  6  1  0  0  0  0
  1  6  2  0  0  0  0
M  END
>  <id>
1
>  <name>
pyridine
>  <mw>
79.102
$$$$

I have a problem with later code in cases where there are spaces in the value of <name> and I would like to substitute underscore for space in the value of NAME in the above awk code before it is written. Is there a way to do this?

Thanks,

LMHmedchem
# 2  
Old 02-06-2018
Code:
F==1 { gsub(" ", "_"); NAME = pre$0; F = 0 }

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 02-06-2018
Because the gsub() runs on $0 here (no 3rd argument --> $0), you can alter the other output as well, by placing the { OUT[++CNT] = $0 } after it.
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 02-06-2018
Thanks, that worked well.

This is the revised code,
Code:
# name field tag to look for
name_field='<name>'
# value to add to beginning of name string
pre='ID_'
awk -v find_name=$name_field -v pre=$pre ' { OUT[++CNT] = $0 }
                                      F==1 { gsub(" ", "_"); NAME = pre$0; F = 0 }
                            $0 ~ find_name { F = 1 }
                              $0 == "$$$$" { print NAME; for(i=2; i<=CNT; i++) print OUT[i]; delete OUT; CNT = 0 }
                                         ' > $output_file_name

It's nice to know how to do that as I'm sure it won't be the last time it comes up. Is gsub() part of awk or a call to a different tool?

Quote:
Originally Posted by MadeInGermany
Because the gsub() runs on $0 here (no 3rd argument --> $0), you can alter the other output as well, by placing the { OUT[++CNT] = $0 } after it.
Thanks for the tip, in this case, the name line is the only one that I need to modify.

---------- Post updated at 07:31 PM ---------- Previous update was at 05:07 PM ----------

Perhaps I spoke too soon about not needing to make space replacements in other places in the code. What I need to do is use the space replaced version of NAME on the first line as the original code does, and also use it for the line following the <name> tag.

I was thinking something like this,
Code:
awk -v find_name=$name_field -v pre=$pre ' { if(F == 1) { gsub(" ", "_"); NAME = pre$0; F = 0; OUT[++CNT] = NAME }
                                             else { OUT[++CNT] = $0 }
                                           }
                            $0 ~ find_name { F = 1 }
                              $0 == "$$$$" { print NAME; for(i=2; i<=CNT; i++) print OUT[i]; delete OUT; CNT = 0 }
                                         ' > $output_file_name

I think this will work but perhaps a more generalized solution would be better to allow for substitution on any requested line but not the entire input.

LMHmedchem
# 5  
Old 02-06-2018
In original post, swap lines:
Code:
F==1 { gsub(" ", "_"); NAME = pre$0; F = 0 }
     { OUT[++CNT] = $0 }

This User Gave Thanks to rdrtx1 For This Post:
# 6  
Old 02-07-2018
Quote:
Originally Posted by rdrtx1
In original post, swap lines:
Code:
F==1 { gsub(" ", "_"); NAME = pre$0; F = 0 }
     { OUT[++CNT] = $0 }

This works, as does the suggestion I posted above. I'm not sure which is preferable except that the suggestion of rdrtx1 does not require the conditional.

I assume that in the above case gsub() is changing the value of $0?

LMHmedchem
# 7  
Old 02-07-2018
Quote:
Originally Posted by LMHmedchem
This works, as does the suggestion I posted above. I'm not sure which is preferable except that the suggestion of rdrtx1 does not require the conditional.

I assume that in the above case gsub() is changing the value of $0?

LMHmedchem
Yes, the gsub() call modifies $0 if no third argument is specified. MadeInGermany already said this in post #3 in this thread. (And, you quoted it and thanked him for that tip in post #4.)
This User Gave Thanks to Don Cragun 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

Variable and awk inside for loop

Thanks all for taking time out and reading this thread and big Thanks to all who have come forward for rescue. Background: I have a variable "nbrofcols" that has number of columns from a data file. Now, using this count in for loop, I am trying to get the maximum length of each column present... (7 Replies)
Discussion started by: svks1985
7 Replies

2. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Tracking change inside the script

we have more then 10 jobs scheduled in cronjob.. but we can see some of the script has been changed without any notification.. can we write any script which captures any changes inside the scripts with time of change and user name like .. or any other option apart from this ?? Plz help .. (4 Replies)
Discussion started by: netdbaind
4 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

Using variable inside awk

Hi, Please help me how to use variables inside awk in code below: ll | awk -v "yr=`date '+%Y'`" -v "mnth=`date '+%m'`" -v Jan=1 -v Feb=2 -v Mar=3 -v Apr=4 -v May=5 -v Jun=6 -v Jul=7 -v Aug=8 ' !/^d/ { if(NR>1) {printf "%-29s\t\t%s\t%5s\t\t%s %s,", $9,$1,$5,$`$6`,$7} }' Thanks. (10 Replies)
Discussion started by: manubatham20
10 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. Shell Programming and Scripting

How to use same variable value inside as well as outside of the awk command?

Hi Jim, The following script is in working state. But i m having one more problem with awk cmd. Could you tell me how to use any variable inside awk or how to take any variable value outside awk. My problem is i want to maintain one property file in which i am declaring variable value into that... (12 Replies)
Discussion started by: Ganesh Khandare
12 Replies

8. Programming

using dbx: I can't make it watch a variable change?! Help, please!

(I have mentioned about this situation and arisen problems in another thread: Is there a limit for a code line length in C?, but those questions are OffTop for that subject; so I open a new topic, here.) The main problem is that I have some situation in my program where memory has been changed... (2 Replies)
Discussion started by: alex_5161
2 Replies

9. Shell Programming and Scripting

Using variable inside awk

I am trying to print the lines with pattern and my pattern is set to a variable express awk '/$express/{where=NR;print}' test2.log I am not getting any data even though i have the data with the pattern. Can seomeone correct me with the awk command above? (20 Replies)
Discussion started by: rdhanek
20 Replies

10. Shell Programming and Scripting

getting variable inside awk

Hi All, I have awk script for replacing the nth ocurance of a string in an xml file... My code is like this FILETYPE=xml TAGNAME=type OCCURANCE=$1 TAGVALUE=valueur echo OCCURANCE:$OCCURANCE echo TAGNAME:$TAGNAME echo TAGVALUE:$TAGVALUE awk -v n=$OCCURANCE -v... (1 Reply)
Discussion started by: subin_bala
1 Replies
Login or Register to Ask a Question