Can -v option in awk be used to store an array of variables?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can -v option in awk be used to store an array of variables?
# 1  
Old 10-02-2012
Can -v option in awk be used to store an array of variables?

I want to pass an array of variables to be inserted by awk in the 2nd column of a file.

 
Empl No. Employee Age
1000000 22
1100000 24
1200000 26
Now, I want to pass an array having three different ages which need to replace the ages in the above table. Can I pass them to awk for it to replace the second column with new ages from an array?
# 2  
Old 10-02-2012
Usually you'd pass big tables of data into awk as a file. You can use the NR==FNR trick to process only the first file, and let the rest of the code handle everything else.

Code:
awk 'NR==FNR { A[++L]=$0 ; next } { rest of stuff }' tablefile otherfiles

# 3  
Old 10-03-2012
Thanks Corona. But it looks like I wasn't quite clear about what I wanted.

I have an array that I want to pass to awk as an argument, maybe to go in the -v variable. All I know is the variable used with -v is a scalar variable. If I wished to pass an array instead, would that be possible?

I could be using the wrong approach to my problem, wherein I want to replace the 2nd column of a file with new values contained in my array. But I guess that is unsafe as I'd rather do a key mapping as opposed to try and randomly fit in data from elsewhere. Thanks anyway!
# 4  
Old 10-03-2012
If you really need entire huge blocks of data stored in arrays, perhaps the entire thing could be rewritten in awk. It's good at that sort of thing, and doesn't have limits except the main memory of the computer.

To answer your question: No. There's no generic way to shove an array from shell into awk, or anything else for that matter. You have to convert it back into a block of text, then split that block back apart on the other side manually. That's trivial to do with files, which awk is designed to process.

Forgive me, though, but I've seen a lot of new shell programmers shell shove absolutely everything into arrays because that's the only thing they know. If they don't have arrays, they simply shove everything into single variables instead. Then they post about what they managed to accomplish despite the difficulty, and people think that's how it's supposed to be done, and the habit spreads. What they're missing out on is easy redirection of files and streams -- which are the whole advantage of using a shell as opposed to, say, perl.

But doing so causes a whole new set of problems. How is that text stored, exactly? Line one as element one, line two as element two, and so forth? Or token 1 as the first element, token 2 as the second, and so forth? Do you need to worry about the size of a shell variable? -- that's quite small, on some systems! Will you need to feed awk its dimensions? Do you actually know its dimensions? Will you need to do all kinds of splitting specially instead of the stuff awk can do for you all by itself if used correctly? Might it have been simpler to not use an array at all?

Quite often, it really is simpler, to use things as they were intended.

Last edited by Corona688; 10-03-2012 at 03:41 PM..
# 5  
Old 10-03-2012
Yeah you're right. I'm actually trying to complicate something by using an array which can otherwise be done using awk alone (if I could, in the correct way). But I intend to abandon that method as I also believe using the array couldn't land me with accurate data and I'd be beating around the bush to even get those array values before I "shove" them into my awk variable Smilie

So I'll explore the other way of using awk in the right way to get exactly what I want. Again, many thanks.
# 6  
Old 10-03-2012
Maybe:

Code:
set -A arr aaa bbb ccc ddd
 
awk -v arr="${arr[*]}" 'NR>1 {$2=arr} 1' infile

# 7  
Old 10-03-2012
Wow! Looks so workable. Now I'm tempted into using it (after almost pledging to Corona that i'll not go that way!) Smilie

Thanks RDRTX1!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

2. Shell Programming and Scripting

ksh : need to store the output of a awk command to a array

I have awk command : awk -F ' ' '{ print $NF }' log filename And it gives the output as below: 06:00:00 parameters: SDS (2) no no no no doc=4000000000). information: (6 Replies)
Discussion started by: ramprabhum
6 Replies

3. Shell Programming and Scripting

Store value in array with awk

Hi everybody I wanna store some values that r in a .txt file in some arrays for example I have: 32782 28 32783 02 32784 01 32785 29 32786 25 32787 25 32788 00 32789 25 32790 02 32791 29 32792 23 32793 01 32794 28 and I need to save the first... (4 Replies)
Discussion started by: Behrouzx77
4 Replies

4. Shell Programming and Scripting

Assign zero to strings that don't appear in block, store result in AWK array

Hi to all, I have this input: <group> <x "2">Group D</x> <x "3">Group B</x> <x "1">Group A</x> </group> <group> <x "1">Group E</x> <x "0">Group B</x> <x "1">Group C</x> </group> <group> ... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

5. Shell Programming and Scripting

Store all the passed arguments in an array and display the array

Hi I want to write a script which store all the parameters passed to the script into an array. Once it is stored I want scan through the array and and delete those files for last month present inside the directory. The files in directory is appneded with YYYY_MM_DD. I want to know how can I... (3 Replies)
Discussion started by: dgmm
3 Replies

6. Shell Programming and Scripting

store multiple variables in one go

Guys anyone know how i can store fields into multiple variables in one go? I'm wanting to grab the disk id's from format into disk1 and disk2 Below is what i want to work but i know it doesnt :- : | format | awk '/^(\ +)/ {print $2}' | read disk1 disk2 The below does work...but i don't... (5 Replies)
Discussion started by: lavascript
5 Replies

7. Shell Programming and Scripting

comparing variables in awk array

Hi, I am trying to sort and display the below(like) input using awk command: Input: ------ 0;A 4;A 5;A 33;A 45;A 0;B 4;B 5;B 33;B 45;B Output (desired): (5 Replies)
Discussion started by: pvamsikr
5 Replies

8. UNIX for Dummies Questions & Answers

trying to store variables in an array

im looping through an array setting three variables each time (one of the variables gives me the position in the array and is incremented each loop) im trying to then set the variables to that position in the array without much luck. any ideas? anArray=`$VAR1+$VAR2+"("$pos")"` (1 Reply)
Discussion started by: magnia
1 Replies

9. Shell Programming and Scripting

Store values in an Array

Hi all. Well, I have the next code: I need to make an array with the values I have in the bucle, but just don't get it... Question is, how can I store in an array that values, and how can I display them with echo? (8 Replies)
Discussion started by: crcbad
8 Replies

10. Shell Programming and Scripting

How to store contents of a command in array of variables in shell script?

I want to store contents of command dir in array of variables For eg: dir contents are command d2 demovi~ file inven java new untitled folder d1 demovi er1 filename inven~ myfiles ubuntu desktop xmms ----------------------------------- I... (3 Replies)
Discussion started by: netresearch
3 Replies
Login or Register to Ask a Question