LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to cycle over two columns using entries from each as vars in same command (https://www.linuxquestions.org/questions/programming-9/script-to-cycle-over-two-columns-using-entries-from-each-as-vars-in-same-command-592902/)

no_treble 10-18-2007 10:46 PM

Script to cycle over two columns using entries from each as vars in same command
 
Hi.

I'm trying to write a bash script that will cycle through column 1, use each entry as the first variable in a command, then use each entry in column 2 as the second variable in the same command. Not having much luck though...

I have a list of raw device bindings the way they should be bound on multiple boxes in a cluster. I want to use this list to create the raw devices on each box, without having to manually type them all in on each box (there will be well over 100 devices).

Here's what the top part of the list looks like (truncated):

raw1:emcpowerb
raw2:emcpowerc
raw3:emcpowerl1
raw4:emcpowerm1
raw5:emcpowerd
raw6:emcpowere
raw7:emcpowerg
raw8:emcpowerh
raw9:emcpoweri
raw10:emcpowerj
raw11:emcpowerk
raw12:emcpowerq
raw13:emcpowerr
raw14:emcpowers
raw15:emcpowert
raw16:emcpoweru
raw17:emcpowerv
raw18:emcpowerw
raw19:emcpowerx
raw20:emcpowery
raw21:emcpowerz
raw22:emcpoweraa
raw23:emcpowerab
raw24:emcpowerac
raw25:emcpowerad


Here's what I started with, but it fails because I'm trying to loop over each entry twice I guess. It just gives me the usage message for the raw command, which is formatted correctly, so it's not getting the info it needs out of the variables I guess:


for rawdev in "$(cat raw.txt | cut -d: -f1)"; do
for emcdev in "$(cat raw.txt | cut -d: -f2)"; do
raw /dev/raw/$rawdev /dev/$emcdev;
done;
done;


How do I get just column 1 entries into one variable in the command, and just column 2 entries into the other variable, and keep them sorted the way they are now?

Thanks for any help or advice.

angrybanana 10-18-2007 11:01 PM

EDIT: Unless awk is not an option, skip this and use ghostdogs answer.
Code:

$ while read line;do rawdev=${line%:*};emcdev=${line#*:};echo "/dev/raw/$rawdev /dev/$emcdev";done <raw.txt

/dev/raw/raw1 /dev/emcpowerb
/dev/raw/raw2 /dev/emcpowerc
/dev/raw/raw3 /dev/emcpowerl1
...
...

Just replace echo with whatever you'd like to do.

ghostdog74 10-18-2007 11:04 PM

don't know what is "raw", so i assume its a command
Code:

awk 'BEGIN{FS=":"}
{
    cmd="raw /dev/raw/" $1 " /dev/"$2
    system(cmd)
}

' "file"


gilead 10-18-2007 11:04 PM

EDIT: No need to read this :) ghostdog74's solution is the same thing...

You could also use awk for that. For example (remove the echo):
Code:

#!/bin/sh

DATAFILE=~/data

awk -F: ' { system("echo raw /dev/raw/" $1 " /dev/" $2) } ' $DATAFILE

Gives an output of:
Code:

raw /dev/raw/raw1 /dev/emcpowerb
raw /dev/raw/raw2 /dev/emcpowerc
raw /dev/raw/raw3 /dev/emcpowerl1
raw /dev/raw/raw4 /dev/emcpowerm1
raw /dev/raw/raw5 /dev/emcpowerd
raw /dev/raw/raw6 /dev/emcpowere
raw /dev/raw/raw7 /dev/emcpowerg
raw /dev/raw/raw8 /dev/emcpowerh
raw /dev/raw/raw9 /dev/emcpoweri
raw /dev/raw/raw10 /dev/emcpowerj
raw /dev/raw/raw11 /dev/emcpowerk
raw /dev/raw/raw12 /dev/emcpowerq
raw /dev/raw/raw13 /dev/emcpowerr
raw /dev/raw/raw14 /dev/emcpowers
raw /dev/raw/raw15 /dev/emcpowert
raw /dev/raw/raw16 /dev/emcpoweru
raw /dev/raw/raw17 /dev/emcpowerv
raw /dev/raw/raw18 /dev/emcpowerw
raw /dev/raw/raw19 /dev/emcpowerx
raw /dev/raw/raw20 /dev/emcpowery
raw /dev/raw/raw21 /dev/emcpowerz
raw /dev/raw/raw22 /dev/emcpoweraa
raw /dev/raw/raw23 /dev/emcpowerab
raw /dev/raw/raw24 /dev/emcpowerac
raw /dev/raw/raw25 /dev/emcpowerad


chrism01 10-18-2007 11:25 PM

or in pure bash:

Code:

for rec in `cat raw.txt`
do
    rawdev=`echo $rec|cut -d':' -f1`
    emcdev=`echo $rec|cut -d':' -f2`
    do your thang...
done


bigearsbilly 10-19-2007 03:44 AM

what's all this uneccessary catting and cutting ;)

Code:

(IFS=:
while read a b; do
    echo do_this $a $b
done < input_file
)


no_treble 10-19-2007 08:54 AM

Wow, you people are quick (and good!). I think I really need to improve my weak pseudo-scripting skills. :)

Tried several of these and they work like a charm.

Thank you!!!


All times are GMT -5. The time now is 05:26 PM.