Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Help to know the various possible uses of awk arrays Post 302506711 by cgkmal on Monday 21st of March 2011 06:05:27 PM
Old 03-21-2011
Help to know the various possible uses of awk arrays

Hi to all,

I write this time to ask for different syntax or ways of arrays within awk and uses.

Since I don't know how actually could work and the real potencial of awk arrays, I look for a proactive reply/help in order to get
more information than it seems I'm trying to get.

I think I have seen before in threads in this forum, solutions with different syntax used to load arrays, obviously
depending on the input file and what you want to do with it, the uses and syntax might vary.

*I show below only examples rescued from what I think I've seen, so some may have incorrect syntax.


Code:
{a[$1]=$2}
{a[k++]=$1}
{a[++i]=$3}
{a[i]=$3++}
{a[i]=++$3}
{++a[i]=$3}
{a[$2]=+$3}
{a[$4]++}

Summary:
1-) It would be fine the person who respond, could say which of those examples above has correct syntax, the meaning of expression
and what others possible syntax exist when we want to load an array from input file.
2-) After that, I'll try to play around/evaluate different syntax you can shared(multi dimensional too, if it possible) and test to see their
output (learning of that) when I apply them to a sample file like I put below.
3-) If you want to share some examples with arrays with the below input file, it would be great and very appreciated
(difference/multiplication between fields, totals, etc)Smilie!!!.
Code:
Product    Entry    Price($)    Purchased    Sold
Laptops    2010-05-20    50    25    10
Desktop Computers    2010-12-07    40    20    7
Printers    2010-09-12    30    37    12
Optical Mouse    2011-02-07    12    71    39
Monitor    2010-12-03    20    44    15
Batteries    2011-01-15    20    80    41
Ac Adapters    2011-03-14    17    31    11
Memories    2010-07-19    15    93    53
Hard Drives    2011-01-13    35    17    5
Totals


PS. It is not about a homework, it's about doubts that I have regarding handling arrays in awkSmilie

Many thanks in advance,

Regards.

Last edited by cgkmal; 03-21-2011 at 07:16 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Two or more arrays in Awk

Hi All, I have been working on awk and arrays. I have this small script: cat maillog*|awk -F: '$2=="SMTP-Accept" && $5~/string/ {lastdate=substr($1,1,8); internaluser=$5; v++} END {for (j in v) {print lastdate, v, j}'| sort>> mail.list This gives me the number of mails users are getting. ... (1 Reply)
Discussion started by: nitin
1 Replies

2. Shell Programming and Scripting

arrays in awk???

Been struggling with a problem, I have been trying to do this in awk, but am unable to figure this out, I think arrays have to be used, but unsure how to accomplish this. I have a input file that looks like this: 141;ny;y;g 789;ct;e;e 23;ny;n;u 45;nj;e;u 216;ny;y;u 7;ny;e;e 1456;ny;e;g... (3 Replies)
Discussion started by: craigsky
3 Replies

3. Shell Programming and Scripting

awk arrays

Guys, OK so i have been trying figure this all all day, i guess its a pretty easy way to do it. Right, so i have to column of data which i have gotten from one huge piece of data. What i would like to do is to put both of these into one array using awk. Is this possible?? If so could... (1 Reply)
Discussion started by: imonthejazz
1 Replies

4. Shell Programming and Scripting

Need Help with awk and arrays

now its owkring - thanks fo rthe help all . (7 Replies)
Discussion started by: fusionX
7 Replies

5. Shell Programming and Scripting

Arrays in awk

Hi, I've written the following code to manipulate the first 40 lines of a data file into my desired order: #!/bin/awk -f { if (NR<=(4)){ a=a$0" "} else { if ((NR >= (5)) && (NR <= (13))) { b=b$0" " } else {if ((NR >= (14)) && (NR <= (25))){ c=c$0" "} ... (5 Replies)
Discussion started by: catwoman
5 Replies

6. Shell Programming and Scripting

awk arrays can do this better - but how?

Hi, I have spent the afternoon trawling Google, Unix.com and Unix in a Nutshell for information on how awk arrays work, and I'm not really getting too far. I ahve a batch of code that I am pretty sure can be better managed using awk, but I'm not sure how to use awk arrays to do what I'm... (1 Reply)
Discussion started by: littleIdiot
1 Replies

7. Shell Programming and Scripting

arrays in awk

Hi, I have the following data in a file for example: Name="Fred","Bob","Peterson","Susan","Weseley" Age="24","30","28","23","45" Study="English","Engineering","Physics","Maths","Psychology" Code="0","0","1","1","0" Name="Fred2","Bob2","Peterson2","Susan2","Weseley2"... (14 Replies)
Discussion started by: james2009
14 Replies

8. UNIX for Dummies Questions & Answers

awk arrays

Hi Can someone please explain the logic of awk arrays. I have been doing some reading but I dont understand this: #!/usr/bin/gawk -f { arr++; } end { for(i in arr) { print arr,i } } As I understand arr refs the arrays index, so while $2 is a string that cant... (2 Replies)
Discussion started by: chronics
2 Replies

9. Shell Programming and Scripting

help in awk arrays!

Hi, buddies I am new to shell scripting and trying to solve a problem. I read about arrays in awk that they are quite powerful and are associative in nature. Awk Gurus Please help! I have a file: Id1 pp1 0t4 pp8 xy2 Id43 009y black Id6 red xy2 Id12 new pp1 black I have... (5 Replies)
Discussion started by: geli21
5 Replies

10. Shell Programming and Scripting

Using arrays with awk

I'm a little stuck and would be grateful of some advice! I have three files, two of which contain reference data that I want to add to a line of output in the third file. I can't seem to get awk to print array contents as I would expect. The input files are: # Input file AAA,OAA,0313... (2 Replies)
Discussion started by: maccas17
2 Replies
set_color(1)							       fish							      set_color(1)

NAME
set_color - set_color - set the terminal color set_color - set the terminal color Synopsis set_color [-v --version] [-h --help] [-b --background COLOR] [COLOR] Description Change the foreground and/or background color of the terminal. COLOR is one of black, red, green, brown, yellow, blue, magenta, purple, cyan, white and normal. o -b, --background Set the background color o -c, --print-colors Prints a list of all valid color names o -h, --help Display help message and exit o -o, --bold Set bold or extra bright mode o -u, --underline Set underlined mode o -v, --version Display version and exit Calling set_color normal will set the terminal color to whatever is the default color of the terminal. Some terminals use the --bold escape sequence to switch to a brighter color set. On such terminals, set_color white will result in a grey font color, while set_color --bold white will result in a white font color. Not all terminal emulators support all these features. This is not a bug in set_color but a missing feature in the terminal emulator. set_color uses the terminfo database to look up how to change terminal colors on whatever terminal is in use. Some systems have old and incomplete terminfo databases, and may lack color information for terminals that support it. Download and install the latest version of ncurses and recompile fish against it in order to fix this issue. Version 1.23.1 Sun Jan 8 2012 set_color(1)
All times are GMT -4. The time now is 03:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy