AWK separating a file into an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK separating a file into an array
# 1  
Old 03-02-2012
AWK separating a file into an array

Is there a way to have awk put successive records into an array in a bash script?

I have files that say things like

name :title :salary
Bob :Instructor :30,000
Joyce :Instructor :30,000
Patrick :Manager :40,000


What I want to do is seperate this file into an array so that the array would look like this:

array[0] = Bob :Instructor :30,000
array[1] = Joyce :Instructor :30,000
array[2] = Patrick :Manager :40,000

I figure there must be a way to make this happen through awk, but I am so new at this that I cannot think of how to do it, and I have not been able to locate an example or even instructions on how to do this.

I want this done in the bash script.

Please, any help that can be given would be greatly appreciated. Smilie
# 2  
Old 03-02-2012
I have a simple solution:

Code:
eval_$(aw_ '{p_int _arra_["X+_"]=\_"$0"_""}'_file_

But it sounds like homework!
# 3  
Old 03-02-2012
It is not homework. I didn't want to give the actual information in my message for security reasons. I am just new at my job and scripting is something that is coming very difficultly for me. I assume the red lines are characters that you just did not post. Is this for testing purposes?
# 4  
Old 03-02-2012
Fair enough Smilie

Code:
$ eval $(awk '{print "array["X++"]=\""$0"\""}' file)
$ echo ${array[0]}                                  
Bob :Instructor :30,000

This User Gave Thanks to Scott For This Post:
# 5  
Old 03-02-2012
Thank you so much Smilie I really appreciate it. The wall I have been hitting my head on appreciates it too.
# 6  
Old 03-02-2012
You can also do it from within a shell without invoking awk
Code:
#!/bin/bash

n=0
while read line
do
     if (( n > 0 ))
     then
         array[$((n - 1))]="$line"
     fi
     (( n++ ))
done < infile

echo ${array[0]}
echo ${array[1]}
echo ${array[2]}

# 7  
Old 03-02-2012
awk and shell variables aren't really related to each other. awk is its own language and has its own variables so, even when processed by awk, you're still faced with the same problem -- getting text output from a stream into variables in the arrangement you want.

An even simpler way is to just tell the shell to split on newlines, so you can cram the text in raw:
Code:
#!/bin/bash

OLDIFS="$IFS"
IFS=$'\n'

ARRAY=( `tail -n +2 file` )

IFS="$OLDIFS"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append awk results into file or array

for a in {1..100} do awk '{ sum+=$a} END {print sum}' a=$a file1 > file2 done I know I will get only one number if following the code above, how can I get 100 sum numbers in file2? (2 Replies)
Discussion started by: wanliushao
2 Replies

2. Shell Programming and Scripting

Not getting array in .awk file and print it

I have test.sh file as below : set -A IDARR $ID echo | awk -f test.awk -v TempArr="${IDARR }" I have test.awk file as below : BEGIN { Flag = 1; } { print "Hello"; for(i in TempArr) { print i; } } (9 Replies)
Discussion started by: nes
9 Replies

3. Shell Programming and Scripting

Help in separating a multilingual file

Hello, I have a text file running into around 100 thousand+ lines which has the following rigid structure: Each field is separated by a comma. Some examples are given below: 23,Chinttaman Pagare,चिंतमण पगारे 24, Chinttaman Pateel,चिंतामण पाटल 25, Chinttaman Rout,चिंतामण राऊत 26,... (3 Replies)
Discussion started by: gimley
3 Replies

4. Shell Programming and Scripting

Print array into a single file - AWK

Hi all, I been looking for a solution to the fact that when I use: for (i=1; i<=NF; i++) print $ifields that are originally in a single line are printed in a single line I have severals files for which the first 7 are the same, but the number of variables after that can vary, for example NF... (5 Replies)
Discussion started by: PaulaL
5 Replies

5. Shell Programming and Scripting

Need help separating file lines into three classes

Hi folks, What I have are config files with lines that: are blank, start with a "!" or start with char's(or a blank space and then char's) I am using ksh I can display each line by doing: for INDEX in {0..$LENGTH} do echo "${data}" done What I need to do requires I can... (12 Replies)
Discussion started by: Marc G
12 Replies

6. Shell Programming and Scripting

Need help separating a file

Hi all, I have a single text file, Contig3.fasta, that looks like this: >NAME1 ACCTGGTA >NAME2 GGTTGGACA >NAME3 ATTTTGGGCCAnd It has about 100 items like this in it. What I would like to do is copy each item into 100 different text files, and have them named a certain way Output... (4 Replies)
Discussion started by: repiv
4 Replies

7. Shell Programming and Scripting

Separating delimited file by pattern with exclusion list

I have a file with the contents below jan_t=jan;feb_t=feb;mar_t=mar;year=2010 jan_t=null;feb_t=feb;mar_t=mar;year=2010 jan_t=jan;feb_t=feb;mar_t=mar;year=2010 I want to extract out all the fields values ending with "_t" , however, i want to exclude feb_t and mar_t from the results In... (6 Replies)
Discussion started by: alienated
6 Replies

8. Shell Programming and Scripting

Put lines of a file in an array with awk

Hello, Is there any way in awk to put every line of a file in an array and so we can like this print the line we want. For example, if we have this file aaa eee bbb fff ccc ggg ddd hhh So we can print to the output the 3rd line only ccc ggg If it is possible, please put the... (7 Replies)
Discussion started by: rany1
7 Replies

9. Shell Programming and Scripting

saving values in file in an array in awk

hi i am trying to save values in a file in an array in awk..the file is as follows: 0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0, so far i have this: awk 'BEGIN {RS="\n";FS=","} { for(i=1;i<=NR;i++) { for(j=1;j<=NF;j++) { a=$j; } } (4 Replies)
Discussion started by: npatwardhan
4 Replies

10. Shell Programming and Scripting

Separating values from a file and putting to a variable

I am writing into a file testfile.txt values like ./XXXXXXCZ1/tprcm10c.bin ./XXXXXXCZ1_HOT/tprcm09c.bin ./XXXXXXCZ_cold/tprcm05c.bin I want to store the values of tprcm*.bin and XXXXXXCZ* in separate variables Can anybody Pls hlp me out with this ... Thanks (2 Replies)
Discussion started by: ultimatix
2 Replies
Login or Register to Ask a Question