Problem with awk array when loading from shell variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with awk array when loading from shell variable
# 1  
Old 06-18-2013
Problem with awk array when loading from shell variable

Hi, I have a problem with awk array when iam trying to use awk in solaris box as below..Iam unable to figure out the problem..
Need your help. is there any alternative to make it in arrays from variable values

Code:
nawk 'BEGIN {SUBSEP=" ";
split("101880|110045 101887|110045 101896|110045 101903|110045 101910|110045 101919|110045 101926|110045 101933|110045 101941|110045 101948|110045 101957|110046 101966|110045 101975|110046 101992|110045 102002|110046 102009|110046 102016|110046 102024|110046 102033|110046 102048|110046 102063|110046 102078|110046 102093|110047 102103|110046 102112|110047 102128|110047 102137|110047 102144|110047 102151|110047 102160|110047 102168|110047",Val," ");
} 
END {
for (i in Val) print i,Val[i]
}
' $FILE

O/p--Success

Code:
nawk 'BEGIN {SUBSEP=" ";
split('${LINENUMBER_JOBID}',Val," ");
} 
END {
for (i in Val) print i,Val[i]
}
' $FILE

O/p Error:---
nawk: syntax error at source line 2
 context is
         >>> split(101880|110045 <<<
nawk: illegal statement at source line 2
        missing }
        missing )

Shell variable value:
-------------------
Code:
 echo "--${LINENUMBER_JOBID}==="--101880|110045
101887|110045
101896|110045
101903|110045
101910|110045
101919|110045
101926|110045
101933|110045
101941|110045
101948|110045
101957|110046
101966|110045
101975|110046
101992|110045
102002|110046
102009|110046
102016|110046
102024|110046
102033|110046
102048|110046
102063|110046
102078|110046
102093|110047
102103|110046
102112|110047
102128|110047
102137|110047
102144|110047
102151|110047
102160|110047
102168|110047===


Last edited by zaxxon; 06-18-2013 at 10:04 AM..
# 2  
Old 06-18-2013
The split looks ugly, it may be another way to divide your string.
What is your input file/data?

With 70 post you should also now how to use code tags
# 3  
Old 06-18-2013
Yes, why don't you use code tags?

Looks like your shell variable has a <newline> char as a separator, to which awk responds with utter lack of comprehension.
# 4  
Old 06-18-2013
The input file and my logic

Hi Jotne,Thanks for the info..will use code tags from here on.What iam trying to achieve is..I have the below blocks of data which i want to find the max Job-id and start printing only the blocks and omitting the rest
My logic goes in this way

> find the pattern [0-9]{4,} [A-Z][a-z]{2,} [0-9]{2,} i.,e 2013 Jan 23 09
and get the job-id and line number of the file
> Take it in a variable and find the max jobid in the file.
> Now traverse the jobid in the same variable and find the startpt and the endpt.
> send this to the basic awk command
awk 'NR==STPT,NR=={ENDPT}-1' and print only the blocks.

Here i got struck at the last point when i try to split the values through " ",take it in an array and send as startpoint and endpoint.
Need your help.
I/p
Code:
2013 Jan 23 09:12:02:716 IST +0 BW.TEST-2-Process_Archive Debug [BW-User] DEBUG Job-107544 [UtilityService
s/BWFLIB_Logging/BWFLIB_getLogger.process/GA_WL_LOGDEBUG]: Wed, Jan 23, 2013, 09:12:02 698[1111732318]::
LogMessage::CallSYs1AndSystem2SubProcess:: CallSystem1AndSystem2
TrackingInfo:: System2ServicevCustomerRequest-382_1358932322623
CustomId:: 192.168.248.250:6961937c:13a2b960ad6:-7ffd

2013 Jan 23 09:12:02:746 IST +0 BW.TEST-2-Process_Archive Debug [BW-User] DEBUG Job-107543 [UtilityService
Adapter.Request._Null_.vCustomer.1
ReplySubject:: _INBOX
2013 Jan 23 09:12:02:716 IST +0 BW.TEST-2-Process_Archive Debug [BW-User] DEBUG Job-107544 [UtilityService
s/BWFLIB_Logging/BWFLIB_getLogger.process/GA_WL_LOGDEBUG]: Wed, Jan 23, 2013, 09:12:02 698[1111732318]::
LogMessage::CallSYs1AndSystem2SubProcess:: CallSystem1AndSystem2
TrackingInfo:: System2ServicevCustomerRequest-382_1358932322623
CustomId:: 192.168.248.250:6961937c:13a2b960ad6:-7ffd

2013 Jan 23 09:12:02:746 IST +0 BW.TEST-2-Process_Archive Debug [BW-User] DEBUG Job-107544 [UtilityService
Adapter.Request._Null_.vCustomer.1
ReplySubject:: _INBOX

2013 Jan 23 09:12:02:716 IST +0 BW.TEST-2-Process_Archive Debug [BW-User] DEBUG Job-107543 [UtilityService
s/BWFLIB_Logging/BWFLIB_getLogger.process/GA_WL_LOGDEBUG]: Wed, Jan 23, 2013, 09:12:02 698[1111732318]::
LogMessage::CallSYs1AndSystem2SubProcess:: CallSystem1AndSystem2
TrackingInfo:: System2ServicevCustomerRequest-382_1358932322623
CustomId:: 192.168.248.250:6961937c:13a2b960ad6:-7ffd

---------- Post updated at 07:16 AM ---------- Previous update was at 07:07 AM ----------

Sorry i didnt mention the o/p..The expected o/p should be the blocks with Job-107544.

o/p
Code:
2013 Jan 23 09:12:02:716 IST +0 BW.TEST-2-Process_Archive Debug [BW-User] DEBUG Job-107544 [UtilityService
s/BWFLIB_Logging/BWFLIB_getLogger.process/GA_WL_LOGDEBUG]: Wed, Jan 23, 2013, 09:12:02 698[1111732318]::
LogMessage::CallSYs1AndSystem2SubProcess:: CallSystem1AndSystem2
TrackingInfo:: System2ServicevCustomerRequest-382_1358932322623
CustomId:: 192.168.248.250:6961937c:13a2b960ad6:-7ffd

2013 Jan 23 09:12:02:716 IST +0 BW.TEST-2-Process_Archive Debug [BW-User] DEBUG Job-107544 [UtilityService
s/BWFLIB_Logging/BWFLIB_getLogger.process/GA_WL_LOGDEBUG]: Wed, Jan 23, 2013, 09:12:02 698[1111732318]::
LogMessage::CallSYs1AndSystem2SubProcess:: CallSystem1AndSystem2
TrackingInfo:: System2ServicevCustomerRequest-382_1358932322623
CustomId:: 192.168.248.250:6961937c:13a2b960ad6:-7ffd

2013 Jan 23 09:12:02:746 IST +0 BW.TEST-2-Process_Archive Debug [BW-User] DEBUG Job-107544 [UtilityService
Adapter.Request._Null_.vCustomer.1
ReplySubject:: _INBOX


Last edited by cskumar; 06-19-2013 at 02:23 AM.. Reason: Please use code tags only for code, not just the whole text. Thats not what they are for.
# 5  
Old 06-18-2013
You are using code tags wrong. I am not able to read this.
Mark group of text, not all and select co-de button.
And you can use edit button to edit your post.
# 6  
Old 06-18-2013
Again: did you check for the <newline> chars in your variable? Replace by space when creating it, and your split will fly!
# 7  
Old 06-19-2013
Edited code tags

@Jotne, I have done with editing part and @RudiC, will give a try as well..

Thanks,
cskumar.

---------- Post updated at 01:08 AM ---------- Previous update was at 12:46 AM ----------

@RudiC,
I have translated the "\n" to " ",but still not working..please see the below output..Still iam getting the same error.

Code:
eaits001z$ echo "-------LINENUMBER_JOBID--------${LINENUMBER_JOBID}---"
-------LINENUMBER_JOBID--------101880|110045 101887|110045 101896|110045 101903|110045 101910|110045 101919|110045 101926|110045 101933|110045 101941|110045 101948|110045 101957|110046 101966|110045 101975|110046 101992|110045 102002|110046 102009|110046 102016|110046 102024|110046 102033|110046 102048|110046 102063|110046 102078|110046 102093|110047 102103|110046 102112|110047 102128|110047 102137|110047 102144|110047 102151|110047 102160|110047 102168|110047 ---

Code:
eaits001z$ nawk -v LINENO=$LINENUMBER_JOBID 'BEGIN {SUBSEP=" ";
> split(LINENO,Val," ");
> }
> END {
> for (i in Val) print i,Val[i]
> }
> ' $FILE

nawk: syntax error at source line 1
context is
>>> 101887|110045 <<<
nawk: bailing out at source line 1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

2. Shell Programming and Scripting

Pass awk array variable to shell

Hi, all suppose I have following myfile (delimited by tab) aa bb cc dd ee ffand I have following awk command: awk 'BEGIN{FS="\t"}{AwkArrayVar_1=$1;AwkArrayVar_2=$2};END{for(i=0; i<NR; i++) print i, AwkArrayVar_1, AwkArrayVar_2,}' myfileMy question is: how can I assign the awk array... (7 Replies)
Discussion started by: littlewenwen
7 Replies

3. 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

4. Shell Programming and Scripting

Problem using shell variable in awk if condition

Hi friends, I'm having a bit of a problem using shell variable in an awk if statement. Please note that i'm using -v option as listed in many forums but I still don't get it working. Here's my code. Kindly help as I've gone crazy trying to work this out :wall: #!/bin/bash -xv ... (4 Replies)
Discussion started by: vishwas.s
4 Replies

5. Shell Programming and Scripting

AWK help: how to compare array elements against a variable

i have an array call ignore. it is set up ignore=34th56 ignore=re45ty ignore=rt45yu . . ignore=rthg34 n is a variable. I have another variable that i read from a different file. It is $2 and it is working the way i expect. array ignore read and print correct values. in the below if... (2 Replies)
Discussion started by: usustarr
2 Replies

6. Shell Programming and Scripting

AWK help. how to compare a variable with a data array in AWK?

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=errdjt3235 so number or elements in the array can varies depending on how big the data input is. now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1). ... (9 Replies)
Discussion started by: usustarr
9 Replies

7. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies

8. Shell Programming and Scripting

Parameter Problem With an Array Variable

Hi, I have two bash shell scripts: test: rrdhcp78-120:test_data msb65$ cat ~/bin/test #!/bin/sh array1=() echo 'elements in array1:' ${#array1} ~/bin/test2 $array1 test2: rrdhcp78-120:test_data msb65$ cat ~/bin/test2 #!/bin/sh array2=${1} (5 Replies)
Discussion started by: msb65
5 Replies

9. Shell Programming and Scripting

Function loading in a shell scripting like class loading in java

Like class loader in java, can we make a function loader in shell script, for this can someone throw some light on how internally bash runs a shell script , what happenes in runtime ... thanks in advance.. (1 Reply)
Discussion started by: mpsc_sela
1 Replies

10. Shell Programming and Scripting

AWK program with array variable

Hi, I made a small awk program just to test array variables. I couldn't find anything wrong with it. But it doesn't give out valid numbers.( just 0.00 ) Do you see any problem that I didn't see? Thanks in advance! Here is the program: ################################## BEGIN { FS =... (4 Replies)
Discussion started by: whatisthis
4 Replies
Login or Register to Ask a Question