Combine two lists of variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Combine two lists of variables
# 1  
Old 06-16-2015
Combine two lists of variables

Thanks in advance for any advice and help.

I have two lists of variables that I want to put into nested for loops.

Code:
for x in 1 2 3 
do
for y in a b c 
do

The output I want is:
Code:
filepath/1/ command modified by a 
filepath/2/ command modified by b
filepath/3/ command modified by c

To be clear, I do NOT want 1a, 1b, 1c, 2a, 2b, 2c, etc. I want 1 to match with a, 2 with b and 3 with c.

Thanks you and sorry if anything is not clear.
# 2  
Old 06-16-2015
You don't want a nested loop, then. Perhaps something like:

Code:
set -- a b c # Set $1=a, $2=b, $3=c

for X in 1 2 3
do
        Y="$1"
        shift 1 # $1=a, $2=b, $3=c becomes $1=b, $2=c

        echo "filepath/$X/ command modified by $Y"
done

# 3  
Old 06-16-2015
Quote:
Originally Posted by Corona688
You don't want a nested loop, then. Perhaps something like:

Code:
set -- a b c # Set $1=a, $2=b, $3=c

for X in 1 2 3
do
        Y="$1"
        shift 1 # $1=a, $2=b, $3=c becomes $1=b, $2=c

        echo "filepath/$X/ command modified by $Y"
done

Hi, thanks for your response. I copy and pasted your code, ran it and get many errors:

test.sh: line 5: *: command not found
test.sh: line 6: *: command not found
test.sh: line 8: *: command not found
test.sh: line 5: *: command not found
test.sh: line 6: *: command not found
test.sh: line 8: *: command not found
test.sh: line 5: *: command not found
test.sh: line 6: *: command not found
test.sh: line 8: *: command not found

---------- Post updated at 10:55 AM ---------- Previous update was at 10:52 AM ----------

This also requires manually pairing the two sets of variables, no? Assume that the list of variables is more like 100x100 (strings x numeric), of something like {a, b, c...} x {1, 2, 3...}.

Thanks.
# 4  
Old 06-16-2015
The code I gave works when pasted word for word, letter for letter. It definitely has no * in line 6. Show me what you actually did, and I can tell you why it didn't work.

Also, show your actual input and I can show you how to use it.
# 5  
Old 06-16-2015
Quote:
Originally Posted by Corona688
The code I gave works when pasted word for word, letter for letter. It definitely has no * in line 6. Show me what you actually did, and I can tell you why it didn't work.

Also, show your actual input and I can show you how to use it.
Code:
for s in amygSMA_R
do
for n in 
ANUP ANVA ANYA ANZH ARMU BELA CAWI CHAZ CHJO CHSC CHSH CRSW DAHA DIWE DOWI ELBU GLGO HOCH JASH JEAM JERE JOWR JYAK KABL KAGI KARO KRAR LAFR LATE LICR MAGE MEWE MILE MIWA NALU NYSH NYWE PAMA SATI SHCO SHME STAL SWPA TAGR TIKI UZOM

for x in 1712 1922 2001 1700 1743 1860 2017 1838 2190 1750 1973 1909 2075 1843 1906 2103 1957 1813 1888 1961 1893 1772 2038 1957 1959 2055 2036 1903 1775 1839 2144 2322 1844 1855 1722 1731 1722 2038 1788 1856 1949 1837 1864 1847 1831
do
fslmaths /filepath/$s/$n/fdt_paths -thr $x -bin fdt_paths_thr

echo $s
done
done
done

***
The above would do ANUP with every number but I just want it to do ANUP with 1712, ANVA with 1922, etc.

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags for code, data, terminal output and the like, but especially for CODE! That is what the tag is for. Thank you.
# 6  
Old 06-16-2015
That has no * in line 6 either. That can't be what you did.

Without seeing your actual input, I'll extrapolate from your broken program instead:

Code:
set -- ANUP ANVA ANYA ANZH ARMU BELA CAWI CHAZ CHJO CHSC CHSH CRSW DAHA DIWE DOWI ELBU GLGO HOCH JASH JEAM JERE JOWR JYAK KABL KAGI KARO KRAR LAFR LATE LICR MAGE MEWE MILE MIWA NALU NYSH NYWE PAMA SATI SHCO SHME STAL SWPA TAGR TIKI UZOM

for x in 1712 1922 2001 1700 1743 1860 2017 1838 2190 1750 1973 1909 2075 1843 1906 2103 1957 1813 1888 1961 1893 1772 2038 1957 1959 2055 2036 1903 1775 1839 2144 2322 1844 1855 1722 1731 1722 2038 1788 1856 1949 1837 1864 1847 1831
do
        echo "$1" "$x"
        shift 1
done

Feel free to post your actual input, so I can do what you actually want.
These 2 Users Gave Thanks to Corona688 For This Post:
# 7  
Old 06-16-2015
My suggestion is to use a file to do the pairing, like this:

Code:
BLA=123
BLUB=234
FOO=345
...

You could read from the file in a while-loop easily and it would be a lot easier to maintain than any for-loop:

Code:
while IFS="=" read VAL1 VAL2 ; do
     echo "VAL1 is $VAL1    and paired with     VAL2 is $VAL2"
done < /path/to/your.file

You can even have tuples of any size (triplets, quadruplets, ...) this way:

Code:
BLA=123=XYZ=000
BLUB=234=ABC=111
FOO=345=CDE=222
...

Code:
while IFS="=" read VAL1 VAL2 VAL3 VAL4 ; do
     echo "VAL1 is $VAL1 VAL2 is $VAL3 VAL3 is $VAL3 VAL4 is $VAL4"
done < /path/to/your.modified.file


I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

guide needed to combine 2 variables

readNOW=$(date +"%Y%m%d%H%M") read readTerminal #input B terminalc=$readTerminal echo eqpt_list_QC_$terminalcT_$readNOW how to get display result it: eqpt_list_QC_BT_201209121530 (2 Replies)
Discussion started by: ment0smintz
2 Replies

2. Shell Programming and Scripting

get the lists

I expert, I may cross post something similar but I dirtyed my quesion somehow to be clear in the thread #cat file1 88dee gcc: Grok for callconvention-hard to enable hard float a2ad2 eglibc: package mtrace separately 61487 python: bump PR of packages after update of distutils.bbclass... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

3. Shell Programming and Scripting

Using foreach with two lists

Hi everybody, I'm trying to use a foreach command with two lists. The file.txt looks like this: var1: 100 200 300 var2: 3 6 9 I'm trying to use a foreach command to associate the two variables together. My script looks like this: #! /bin/tcsh set a=(`cat file.txt | grep 'var1' | cut -d... (8 Replies)
Discussion started by: SimonWhite
8 Replies

4. Shell Programming and Scripting

Combine two lists From Multiple Grep commands.

I'm working with a file with an xml structure. I'd like to parse it down to just the bits i want. Here is and example of the file <message id="96352877" method="status"> <date rfc="Sat, 12 Mar 2011 16:13:15 -0600" unix="1299967995" /> <services> <service id="facebook"... (4 Replies)
Discussion started by: Erulisseuiin
4 Replies

5. UNIX for Dummies Questions & Answers

File Field Replacement, Assigning Fields to Variables, Lists/Arrays?

Okay, I've made threads on extracting fields and comparing strings in separate files in .csv's. I've written the following code with intentions of learning more. I just want this one question answered: How can I assign fields from a file(comma separated) to variables? My goal is to check... (0 Replies)
Discussion started by: chickeneaterguy
0 Replies

6. Shell Programming and Scripting

Shell Script to Create non-duplicate lists from two lists

File_A contains Strings: a b c d File_B contains Strings: a c z Need to have script written in either sh or ksh. Derive resultant files (File_New_A and File_New_B) from lists File_A and File_B where string elements in File_New_A and File_New_B are listed below. Resultant... (7 Replies)
Discussion started by: mlv_99
7 Replies

7. Shell Programming and Scripting

combine variables in one

Within one of my scripts, I have variables extracted from parameters list and want to combine some of them in one (lpath). I tried using awk command..... How do I get it? Thank you. #!/bin/bash # ## Read paramlist.txt file='/name1/name2/paramlist.txt' while read line do # key= echo... (4 Replies)
Discussion started by: Lenora2009
4 Replies

8. AIX

grep using lists?

I have a file that contain a list of files. How can I use grep to search the files in the list for a specific pattern? (2 Replies)
Discussion started by: bbbngowc
2 Replies

9. Shell Programming and Scripting

awk : combine 3 variables into 1

Within one of my awk scripts, I have three variables extracted and calculated on. When done, I simply want to combine the three. The following works, but looks weird. My script reads a field that has text and numbers, knowing the last four comprise MMYY (month and year) # YY are last two... (2 Replies)
Discussion started by: joeyg
2 Replies
Login or Register to Ask a Question