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
# 15  
Old 11-08-2011
It still hits the same error. Currently I'm doing trial and error to see which part of the syntax actually causes this error.
# 16  
Old 11-08-2011
Try this...
Code:
#!/bin/bash

ret_int()
{
  arg=$1
  num=$(echo $arg | sed 's/[^0-9]//g')
  alp=$(echo $arg | sed 's/[^A-Z]//g')
  alphnum=$(echo $alp | sed 'y/ABCDEFG/1234567/')
  return ${num}${alphnum}
}

read val < infile
val=$(echo $val | sed 's/,/ /g')

for i in $val
do
   ret_int $i
   a[$?]=$i
done

printf "\tA\tB\tC\tD\tE\tF\tG\n"
for i in $(seq 1 7)
do
  printf $i
  for j in $(seq 1 7)
  do
    test ! -z "${a[$i$j]}" && val="X" || val=""
    printf "\t$val"
  done
  echo
done

Still there is an awk statement in this, if it throws error let me know, we will replace the awk...
--ahamed

---------- Post updated at 10:58 PM ---------- Previous update was at 10:54 PM ----------

Updated the code, replaced the AWK with SED...
Try now...

--ahamed
# 17  
Old 11-08-2011
It works perfectly fine!

I'll try to understand the codes you provided.

Thanks alot ahamed!
Really appreciate itSmilie
# 18  
Old 11-08-2011
Optimized the code...
Code:
#!/bin/bash
read val < infile
val=$(echo $val | sed 's/,/ /g')

for i in $val
do
   co=$(echo $i | sed 'y/ABCDEFG/1234567/')   
   a[$co]=$i
done
    
printf "\tA\tB\tC\tD\tE\tF\tG\n"
for i in $(seq 1 7) 
do  
  printf $i
  for j in $(seq 1 7)
  do
    test ! -z "${a[$j$i]}" && val="X" || val=""
    printf "\t$val"
  done
  printf "\n"
done

--ahamed
# 19  
Old 11-08-2011
Ahamed, I noticed you've changed
Code:
ret_int $i
a[$?]=$i

to
Code:
co=$(echo $i | sed 'y/ABCDEFG/1234567/')
a[$co]=$i

Both provide same output, so I'm wondering what does this change does?
# 20  
Old 11-08-2011
Removed a function with 3 SED statements and made it into just one SED statement... ain't that good?... Smilie

--ahamed
# 21  
Old 11-08-2011
My understanding of the codes

Code:
#!/bin/bash

ret_int() #creating of function
{
 
  #creating a variable with value of the First argument parse in Shell
  #In this case, what will be value of $1?
  arg=$1
 
  #creating an array (What's this array for?)
  num=$(echo $arg | sed 's/[^0-9]//g')
 
  #creating an array (What's this array for?)
  alp=$(echo $arg | sed 's/[^A-Z]//g')
 
  #creating an array (What's this array for?)
  alphnum=$(echo $alp | sed 'y/ABCDEFG/1234567/')
  return ${num}${alphnum}
}

#creating a variable that read file "infile"
read val < infile
 
#creating an array to store the data in "infile" and convert all "," to " "
val=$(echo $val | sed 's/,/ /g')

#I don't understand this part
for i in $val
do
   ret_int $i
   a[$?]=$i
done

#printing A-G with a tab spacing
printf "\tA\tB\tC\tD\tE\tF\tG\n"

#I don't understand this part
for i in $(seq 1 7)
do
  printf $i
  for j in $(seq 1 7)
  do
    test ! -z "${a[$i$j]}" && val="X" || val=""
    printf "\t$val"
  done
  echo
done

Ahamed, this is the my understanding of your codes. However there are a few parts of it that I don't really get it. Do you mind provide some explanation?
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