awk command hash array not printing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command hash array not printing
# 1  
Old 11-11-2015
awk command hash array not printing

I'm new to awk command.

The HASH ARRAY is not printing. Merging 2 files togather.

vault_input.txt

Code:
3P04_Dep_Inxml:2230
REM02_Dep_Inxml:2200
REM03_Dep_Inxml:2400
REM05:2200
REM06:2200


tst6.txt
Code:
Nov:10:2115:3P04_Dep_Inxml
Nov:10:2129:REM02_Dep_Inxml
Nov:10:2235:REM03_Dep_Inxml


using following command:
Code:
awk 'NR==FNR{a[$1]=$2;next} $4 in a {print $4 a[$1] $3}' FS=":" vault_input.txt tst6.txt

OUTPUT
3P04_Dep_Inxml2115
Code:
REM02_Dep_Inxml2129
REM03_Dep_Inxml2235

DESIRED RESULT
Code:
3P04_Dep_Inxml 2230  2115
REM02_Dep_Inxml 2200 2129
REM03_Dep_Inxml 2400  2235


Looks like "$4 in a" is extracting correct info but nothing is getting populated
in a[1].

THanks
Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scrutinizer; 11-11-2015 at 11:48 PM.. Reason: Code tags
# 2  
Old 11-11-2015
Code:
awk 'NR==FNR{a[$1]=$2;next} $4 in a {print $4, a[$4], $3}' FS=":" vault_input.txt tst6.txt

or:
Code:
awk 'NR==FNR{a[$1]=$1OFS$2;next} $4 in a {print a[$4], $3}' FS=":" vault_input.txt tst6.txt

# 3  
Old 11-11-2015
Try:
Code:
print $4,a[$4],$3

# 4  
Old 11-12-2015
THankyou aia thankyou...it works

---------- Post updated at 11:06 PM ---------- Previous update was at 10:54 PM ----------

Aia

Thanks couild you expalin how (print $4, a[$4], $3) this works and not {print $4 a[$1] $3}
This will help me understand for future refrences.

Thanks so much..i've been at this for 3 days
# 5  
Old 11-12-2015
$1 from vault_input.txt is the same that $4 from tst6.txt, that value is the index name for that associative array a

Example for the first line of vault_input.txt:

a[$1]=$2 would be a["3P04_Dep_Inxml"] = "2230", therefore to obtain the value back you must use the index name 3P04_Dep_Inxml which is $4 for file tst6.txt

---------- Post updated at 10:23 PM ---------- Previous update was at 10:17 PM ----------

When you get to this point:
Code:
$4 in a {print a[$4], $3}

You are processing lines from file tst6.txt, so $1 is different that $1 from file vault_input.txt
# 6  
Old 11-12-2015
Expanding on what Aia has already said... When you're reading vault_input.txt, using $1 as your index in the array a you are setting:
Code:
a["3P04_Dep_Inxml"] = 2230
a["REM02_Dep_Inxml"] = 2200
a["REM03_Dep_Inxml"] = 2400
a["REM05"] = 2200
a["REM06"] = 2200

and while you are reading tst6.txt, $1 always expands to "Nov" and a["Nov"] has never been set. Therefore, a[$1] expands to an empty string while a[$4] expands to the corresponding value that was set while reading vault_input.txt.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl Array within an hash

Hi All I have been using a curl code to output an hash that looks like this $VAR1 = { 'data'... (5 Replies)
Discussion started by: ab52
5 Replies

2. Shell Programming and Scripting

array of hash - perl

How do I get the unique hashes from an array of hashes? @ar1 = ( {a=>1,b=>2}, {c=>3,d=>4},{a=>1,b=>2});I need : @ar2 = ( {a=>1,b=>2}, {c=>3,d=>4});Thanks. (2 Replies)
Discussion started by: shellwell
2 Replies

3. Shell Programming and Scripting

printing array elements inside AWK

i just want to dump my array and see if it contains the values i am expecting. It should print as follows, ignore=345fht ignore=rthfg56 . . . ignore=49568g Here is the code. Is this even possible to do? please help termReport.pl < $4 | dos2ux | head -2000 | awk ' BEGIN... (0 Replies)
Discussion started by: usustarr
0 Replies

4. Shell Programming and Scripting

Array of hash in perl does not work

Hi , I have an input.txt file that i read node: id= c1, class=nb, cx=100, cy=100, r=10 node: id=c2, class=b, cx=150, cy=130, r=10 node: id=c3, class=nb, cx=50, cy=80, r=10 node: id=c4, class=nb, cx=120, cy=200, r=10 i split over , and = to create a global array and then passed it to a... (6 Replies)
Discussion started by: rsanjay
6 Replies

5. Shell Programming and Scripting

PERL - printing a hash of hashes to screen

Hi there I have a hash of hashes made up of the following data bge0|100|half|10.36.100.21 bge1|1000|full|10.36.100.22 bge2|1000|full|10.36.100.23 which when i turn into a hash, would look like this inside the system bge0 -> nic_speed -> 100 nic_duplex -> half ... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

6. Shell Programming and Scripting

perl array question from going through hash

suppose my @{$data1{$callid}}; cotains one two three three five six one two three of random patterns but each item is separated by white space or tab, Below code extract and get rid of the whitespace perfectly so that it shows now like this onetwothree threefivesix... (2 Replies)
Discussion started by: hankooknara
2 Replies

7. Shell Programming and Scripting

hash,array and perl

Hi,i have a code fragment below. %tag = (); #line 1 $tag{'info'} = $datastring; #line 2 $resp = $ua->request( #$ua is a user agent POST 'http://10.2.3.0' , Content_Type => application/x-www-form-urlencoded Content => #line 3 I am not sure of what the code... (3 Replies)
Discussion started by: new2ss
3 Replies

8. Shell Programming and Scripting

Hash within array, within hash, within array...

I have a little problem. To keep a configuration simple, I've exceeded my perl knowledge. :-) I've worked with multi-dimentional arrays before, but this one has me beat: @info = ( { 'defval' => 'abc' 'stats' = ( { 'name' => 'a', }, { 'name' =>... (1 Reply)
Discussion started by: jsmoriss
1 Replies

9. Shell Programming and Scripting

getting data list into a hash array

Begginer alert!! I have just started learning perl and have got stuck. I need to initialize a hash array with values from a list. I've been told to do this using a split command, but its just not happening. Here is the script and output: SCRIPT: #!/bin/perl system("clear"); ... (5 Replies)
Discussion started by: topcat8
5 Replies

10. UNIX for Advanced & Expert Users

changing Hash printing options for FTP

Hi!!, I usually have to ftp massive files ~200MB. The default hash printing option marks a hash for every 1024 bytes. Any ideas how to change this setting to some other value?? I know no one like to see logfiles of size ~2mb just filled with hashes.. :D (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question