LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-18-2007, 10:46 PM   #1
no_treble
LQ Newbie
 
Registered: Oct 2007
Location: USA
Distribution: Debian & Ubuntu (for me), Suse & RedHat (for work)
Posts: 7

Rep: Reputation: 0
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.
 
Old 10-18-2007, 11:01 PM   #2
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
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.

Last edited by angrybanana; 10-18-2007 at 11:13 PM.
 
Old 10-18-2007, 11:04 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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"
 
Old 10-18-2007, 11:04 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
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

Last edited by gilead; 10-18-2007 at 11:06 PM. Reason: ghostdog74 posted the same (almost) solution
 
Old 10-18-2007, 11:25 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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
 
Old 10-19-2007, 03:44 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
what's all this uneccessary catting and cutting

Code:
(IFS=:
while read a b; do
    echo do_this $a $b
done < input_file
)
 
Old 10-19-2007, 08:54 AM   #7
no_treble
LQ Newbie
 
Registered: Oct 2007
Location: USA
Distribution: Debian & Ubuntu (for me), Suse & RedHat (for work)
Posts: 7

Original Poster
Rep: Reputation: 0
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!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell - Accessing COLUMNS in a script!? BlueSpirit Programming 3 06-04-2007 01:22 PM
Exporting vars in a script (Local KDE installation) AphoxemaG Linux - General 3 01-21-2007 05:52 PM
Columns after ps -efH command rjcrews Linux - General 2 03-21-2006 10:45 PM
command to cycle X mode dummy1 Linux - Software 2 09-16-2004 06:09 AM
shell script backticks and vars? SheldonPlankton Programming 4 07-21-2004 12:29 PM


All times are GMT -5. The time now is 11:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration