Convert values in an array using a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert values in an array using a loop
# 1  
Old 02-20-2018
Question Convert values in an array using a loop

I have a headerless array of 1000 columns x 100000 rows. The array only contains 4 values; 0/0, 0/1, 1/1, ./.

Here I am showing the 1st 3 rows and columns of the input array

Code:
0/0    0/0    1/1
0/1    0/1    0/1
0/0    ./.    0/0
0/0    0/0    0/0


I would like to convert the values in the array as follows and print them to a new array:

Code:
0/0 --> 0
0/1 --> 1
1/1 --> 2
./. --> 9

What would be an efficient way to do this using AWK. I am guessing a loop that loops through all columns and changes the values as shown above.

The output array should look like:

Code:
0    0    2
1    1    1
0    9    0
0    0    0

Thanks in advance.
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments. Without them, all sequences of <space>s and <tab>s in data that you're showing us are displayed as a single <space>.

Last edited by Don Cragun; 02-20-2018 at 02:22 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 02-20-2018
As with any thread posted in this forum, there is information that we need to be able to suggest how you might want to proceed:
  1. What operating system are you using?
  2. What shell are you using?
  3. In what form is this leaderless array that you have? (Is it in a shell array variable? Is it hard-coded into an awk BEGIN clause? Is it in a file?)
  4. What is the name of this shell array variable, awk array variable, file, or whatever?
  5. In what form do you want the output array to be produced and what name should it have?
  6. And, most importantly, what have you tried to solve this problem on your own?
# 3  
Old 02-20-2018
Some python3?

Code:
conversion_table = { '0/0': 0, '0/1': 1, '1/1': 2, './.': 9 }
with open('columns_by_rows.file') as read_f:
    for line in read_f:
        columns = [conversion_table[f] for f in line.split()]
        print(*columns, sep='    ')

Save as conv.py
Run as python3 conv.py
This User Gave Thanks to Aia For This Post:
# 4  
Old 02-20-2018
Quote:
Originally Posted by Aia
Some python3?

Code:
conversion_table = { '0/0': 0, '0/1': 1, '1/1': 2, './.': 9 }
with open('columns_by_rows.file') as read_f:
    for line in read_f:
        columns = [conversion_table[f] for f in line.split()]
        print(*columns, sep='    ')

Save as conv.py
Run as python3 conv.py
Works great. How would you save the output to a text file, Output.txt instead of printing to screen since this is a huge array?

I think I figured it out by changing the last line in your code to:

Code:
print(*columns, sep='    ', file=open("output.txt", "a"))

Any problems with doing this?

---------- Post updated at 05:41 AM ---------- Previous update was at 05:38 AM ----------








Quote:
Originally Posted by Don Cragun
As with any thread posted in this forum, there is information that we need to be able to suggest how you might want to proceed:
  1. What operating system are you using?
  2. What shell are you using?
  3. In what form is this leaderless array that you have? (Is it in a shell array variable? Is it hard-coded into an awk BEGIN clause? Is it in a file?)
  4. What is the name of this shell array variable, awk array variable, file, or whatever?
  5. In what form do you want the output array to be produced and what name should it have?
  6. And, most importantly, what have you tried to solve this problem on your own?
Sorry Don. Here is the info:

1. Ubuntu 16.04 LTS
2. Unix
3. Input is a text file, Input.txt
5. Output should be a text file, Output.txt
6. I have tried If Else statments in AWK

Last edited by Geneanalyst; 02-20-2018 at 07:00 AM..
# 5  
Old 02-20-2018
Try also
Code:
awk '
BEGIN   {for (n=split("0/0 0/1 1/1 \\./.", TMP); n; n--) CAT[TMP[n]] = n-1
        }
        {for (c in CAT) gsub (c, CAT[c])
         gsub (3, 9)
        }
1 
' file
0    0    2
1    1    1
0    9    0
0    0    0

Redirect stdout to Output.Txt if happy with the result.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 02-20-2018
Code:
awk -v values="0/0:0,0/1:1,1/1:2,./.:9" '
BEGIN {
   n=split(values,arr,"[:,]");
}
{  for (i=1; i<=n; i+=2) gsub(arr[i], arr[i+1]);
   print $0;
}
' infile

This User Gave Thanks to rdrtx1 For This Post:
# 7  
Old 02-20-2018
Thanks to everyone. All 3 scripts work great !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array not printing values if used in a loop

Hello! I'm making an English to Morse Code translator and I was able to mostly get it all working by looking through older posts here; however, I have one small problem. When I run it it's just printing spaces for where the characters should be. It runs the right amount of times, and if I try... (3 Replies)
Discussion started by: arcoleman10
3 Replies

2. Shell Programming and Scripting

How to swap the values in array using for loop?

array=( 8 5 6 2 3 4 7 1 9 0 ) for i in "${array}" do echo $i done # i need the output like this by swapping of array values 0 9 1 7 4 3 2 6 5 8 (7 Replies)
Discussion started by: Meeran Rizvi
7 Replies

3. 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

4. Shell Programming and Scripting

Convert Column Values to a Range of Values

I have a list of columns with values that I need to transform into a row containing the range of each column. For example: "Column A" 1 2 3 4 10 12 14 15 16 17 18 "Column B" 1 4 5 6 (4 Replies)
Discussion started by: newbio
4 Replies

5. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

6. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

7. Shell Programming and Scripting

how to convert array into the string

Hi I have a line/string as follows: A=" 3498 NORDEA - INDX LINKED NORY" which was converted into an array of characters: p321$ echo "${ARR}" 3 4 9 8 N O R D E A - I N D X L I N K E D N O R Y When I am trying print this array there are blank... (4 Replies)
Discussion started by: aoussenko
4 Replies

8. Shell Programming and Scripting

Assigning values to an array via for/while loop

I need to do something like this: for i in 1 2 3 4 5; do arr=$(awk 'NR="$i" { print $2 }' file_with_5_records) done That is, parse a file and assign values to an array in an ascending order relative to the number of record in the file that is being processed on each loop. Is my... (2 Replies)
Discussion started by: fiori_musicali
2 Replies

9. Shell Programming and Scripting

convert variable content to array

Hi All, I have a variable in a shell script which holds let say n paarmeteres with space separate them : $var = par1 par2 par3 par4 parn; so if I print this variable this is what I'll see: par1 par2 par3 par4 parn I need to insert each parameter to an array , so I can go over on each... (3 Replies)
Discussion started by: Alalush
3 Replies

10. Shell Programming and Scripting

How to use array values after the loop.

- I m retreving values from database and wish to use those values later in my shell script. I m placing these values in an array da_data but outside loop array is empty.Problem is its treating array as local inside loop hence array is empty outside loop. Plz go through the script and suggest how... (1 Reply)
Discussion started by: Devesh5683
1 Replies
Login or Register to Ask a Question