BASH - storing index value from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH - storing index value from another file
# 1  
Old 11-07-2011
Question BASH - storing index value from another file

I'm trying to figure out a way to store the index value from a flat file. The flat file looks like this:

Code:
A1,A2,A4,A6,B3,B6,B7,C1,C2,C5,C7,D2,D5,D7,E2,E3,E4,F1,F5,F6,G2,G5

What I'm trying to achieve is to grab individual index value so as to display it out in a 7x7 matrix with ROW [AB CDE FG] and COL [1234567] and using SED to translate it to a "X" marking.
# 2  
Old 11-07-2011
Can you supply an example of your intended output? Unfortunately, "index" could have multiple meanings, so it's somewhat difficult to imagine what you'd like to see.

Also, are you only willing to use sed to accomplish this, or are other tools also acceptable?
# 3  
Old 11-07-2011
Thanks for your response.

My expected output is

Code:
"\t"AB CDE FG
1   XX    
2      X X   
3
4
5
6
7

where "X" is referring to the data in my flat file (A1,A2 etc).

Currently I'm using the code below to generate the matrix
Code:
    nrows=${1:-8}

    for col in "  " A B "  " C D E "  " F G    # print header row
        do
            printf "%s" "$col"
        done
            printf "\n"

    for (( row=1; row < $nrows; row++ ))    # print rows
        do
            printf "%s\n" "$row"
        done

The data in my flat file represents the "location" in the matrix.

---------- Post updated at 05:14 PM ---------- Previous update was at 12:52 PM ----------

Does anyone has any suggestions for this? Smilie
# 4  
Old 11-07-2011
Try this...
Code:
awk -F, ' 
BEGIN{ for(i=65;i<=71;i++) al[sprintf("%c",i)]=++j }
{
        for(i=1;i<=NF;i++){
                match($i,/([A-Z]*)([0-9]*)/,arr)
                a[arr[1]"-"arr[2]]=$i
        }
}
END{
        printf "\t"
        for(i in al) printf i"\t"
        for(i=1;i<=7;i++){
                printf "\n"i
                for(j in al) {
                        printf "\t"
                        printf a[j"-"i]?"X":""
                }
        }
        printf"\n"
}
' infile

--ahamed

Last edited by ahamed101; 11-07-2011 at 05:02 PM..
# 5  
Old 11-07-2011
Thanks ahamad101!

Thanks for your help!

Sad to say I'm not very familiar with awk. I've tried your codes but I hit some errors and I do not know how to debug it since I'm not very familiar with its syntax.Smilie But i appreciate it! Smilie

I was thinking since my matrix was made out of 2 arrays: "ROW" and "COL"
Each with a value of 7 thus creating a 7x7 matrix.

Which means for ${ROW[0]} and ${COL[0]} actually forms my "A1".

Am I in the right direction?
# 6  
Old 11-08-2011
which is your OS?... If solaris, use nawk...

--ahamed
# 7  
Old 11-08-2011
I'm using Ubuntu 910, currently learning Shell scripting using BASH.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Finding specific string in file and storing in another file

Text in input file is like this <title> <band height="21" isSplitAllowed="true" > <staticText> <reportElement x="1" y="1" width="313" height="20" key="staticText-1"/> <box></box> <textElement> <font fontName="Arial" pdfFontName="Helvetica-Bold"... (4 Replies)
Discussion started by: aankita30
4 Replies

2. Programming

Storing a Temporary File Using C

How would someone save a file such as /etc/vpnc/test.conf locally into a temp file, so it can be queried? So for example if I used rsync to copy this file locally, how would I add that to a temp_file variable and discard it using unlink? #include <stdio.h> #include "error.h" ... (15 Replies)
Discussion started by: metallica1973
15 Replies

3. Shell Programming and Scripting

file index operation in loop

i have one file which contains data like the below DE_CODE|AXXANY|APP_NAME|TELCO|LOC|NY DE_CODE|AXXATX|APP_NAME|TELCO|LOC|TX DE_CODE|AXXABT|APP_NAME|TELCO|LOC|BT DE_CODE|AXXANJ|APP_NAME|TELCO|LOC|NJ DE_CODE|AXXANJwt|APP_NAME|TELCO|LOC|WT am going use a for loop or whole loop which will... (3 Replies)
Discussion started by: mail2sant
3 Replies

4. Shell Programming and Scripting

Search an array and return index (bash)

Hi all, In bash, is there any way of searching an array and returning the index? For example, how could I write a script that would do the following: >> search note_array=(C D E F G A B) for F return the value 3 (or 4) Thanks, R (5 Replies)
Discussion started by: RMontenegro
5 Replies

5. Shell Programming and Scripting

Storing blanks in an array in bash

Hi Guys, I have a file which is as follows: 1 2 4 6 7 I am trying to store these values in an array in bash. I have the following script: FILE=try.txt ARRAY=(`awk '{print}' $FILE`) echo ${ARRAY} (3 Replies)
Discussion started by: npatwardhan
3 Replies

6. Shell Programming and Scripting

Removing the index file

Hi Folks, Under the path /home, I need to remove the index.html/php files from all the users. But the thing is I need to move the index file from the username starting with a-d, then e-g. How can I do this via the shell script. (2 Replies)
Discussion started by: gsiva
2 Replies

7. Shell Programming and Scripting

Sort from start index and end index in line

Hi All, I have a file (FileNames.txt) which contains the following data in it. $ cat FileNames.txt MYFILE17XXX208Sep191307.csv MYFILE19XXX208Sep192124.csv MYFILE20XXX208Sep192418.csv MYFILE22XXX208Sep193234.csv MYFILE21XXX208Sep193018.csv MYFILE24XXX208Sep194053.csv... (5 Replies)
Discussion started by: krish_indus
5 Replies

8. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

9. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies

10. Shell Programming and Scripting

how to delete record in file data with index in another file?

I want to deal with several data, i.e., data.*.txt with following structure MSG|20010102|123 125 4562 409|SEND MSG|20010102|120 230|SEND MSG|20010102|120 204 5071|SEND MSG|20010103|2 11 1098 9810|SEND ...... index file index.txt is 11 201 298 100 ...... What I want to do is: 1)... (0 Replies)
Discussion started by: zhynxn
0 Replies
Login or Register to Ask a Question