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
# 8  
Old 02-07-2018
I' afraid the code you posted doesn't give the desired output in post#1 as it suppresses the Mrv16a3102061815532D line. Plus, it is a bit overcomplicated.

Try also
Code:
awk -v find_name=$name_field -v pre=$pre '
                {OUT[++CNT] = $0
                }

$0 ~ find_name  {getline OUT[++CNT]
                 gsub (/ /, "_", OUT[CNT])
                 OUT[0] = pre OUT[CNT]
                }

$0 == "$$$$"    {for(i=0; i<=CNT; i++) print OUT[i]
                }
' file

# 9  
Old 02-08-2018
Quote:
Originally Posted by RudiC
I' afraid the code you posted doesn't give the desired output in post#1 as it suppresses the Mrv16a3102061815532D line. Plus, it is a bit over complicated.
Was this addressed to me? I have run this version of the code on many files and it seems to work fine.
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

I like your method of changing the value stored in the output array instead of storing the modified value in a separate variable.
Code:
awk -v find_name=$name_field -v pre=$pre ' { OUT[++CNT] = $0 }
                           $0 ~ find_name  { getline OUT[++CNT]
                                             gsub (/ /, "_", OUT[CNT])
                                             OUT[0] = pre OUT[CNT] }
                           $0 == "$$$$"    { for(i=0; i<=CNT; i++) print OUT[i] }
                                         ' file

If I read your code correctly, each line is read and stored in an array. When a line containing find_name is found ($0 ~ find_name), the next line is read by getline() using the incremented counter as in index. The character substitution is done and the modified line is assigned to the array by index value. When the end of record is reached ($0 == "$$$$"), the record is output.

It seems like the getline OUT[++CNT] instruction would cause the counter to be off by one. Is the changed value of ++CNT only in scope inside the {}?

In my bash, it seems like the array runs from 1 to n and not from 0 to n. If I run for(i=1; i<=CNT; i++) print OUT[i] I get the entire record printed. That is why in my version I output the first line from a variable and then start the rest of the output at i=2.

LMHmedchem
# 10  
Old 02-08-2018
Quote:
Originally Posted by LMHmedchem
Was this addressed to me?
Yes.
Quote:
I have run this version of the code on many files and it seems to work fine.
Do you need the line Mrv16a3102061815532D or not? The output as posted has it, the script doesn't provide it. Or, is there an empty line at the begin of the input not shown in the input sample?

Quote:
I like your method of changing the value stored in the output array instead of storing the modified value in a separate variable. . . .
Your analysis is correct.

Quote:
It seems like the getline OUT[++CNT] instruction would cause the counter to be off by one. Is the changed value of ++CNT only in scope inside the {}?
No. The getline reads a new line that needs to be inserted exactly one count above the last line read. Your desired header ("ID_" + name value) will be inserted BEFORE all the other lines at the array's zero element. If the Mrv16a3102061815532D line in NOT wanted, assign to OUT[1] and run the loop starting from i=1.
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