LinuxQuestions.org
Help answer threads with 0 replies.
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 08-16-2004, 05:53 PM   #1
Veteq
Member
 
Registered: Feb 2004
Posts: 155

Rep: Reputation: 30
Shell Program


I need some help with a command

I have a datafile the contains a list of programs and a password.per program

e.g. of dat file

Xawtv home1
calc home2
draw home3


I would like to write a script file that reads the data file and loads column one to a variable and column two into a second variable.....this is easy to do?

anyhelp would be greatly appreciated....


Veteq
 
Old 08-16-2004, 05:59 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
yeah, that's pretty easy really. for each line, you'd use the cut program (one of many solutions)

$one=`echo $line | cut -f 1`
$two=`echo $line | cut -f 2`

should do it. to have the entire column, just replacing the echo with a cat of the entire file should do it.
 
Old 08-16-2004, 07:56 PM   #3
Veteq
Member
 
Registered: Feb 2004
Posts: 155

Original Poster
Rep: Reputation: 30
thank you Chris,

for taking your time to answer my question....



thank you

Veteq

Last edited by Veteq; 08-17-2004 at 12:08 AM.
 
Old 08-17-2004, 12:08 AM   #4
Veteq
Member
 
Registered: Feb 2004
Posts: 155

Original Poster
Rep: Reputation: 30
I am still having some problems

process=`awk ' { print $1 } ' test.dat`


what it's doing is loading all the values into the variable.

what I need to do is to load one record at a time and through the use of a If stmt, check if that is the record that I am looking for.....if not would then move to the next record.....until it find it or hit the end of the file

thank you

Veteq

Last edited by Veteq; 08-17-2004 at 12:09 AM.
 
Old 08-17-2004, 03:31 AM   #5
rkdugar
LQ Newbie
 
Registered: Jul 2004
Posts: 13

Rep: Reputation: 0
Try the following script:

var=calc #You store the string that u want to compare
while read line
do
process=`echo "$line" | cut -f1 -d " "`
if [ "$process" = "$var" ]
then
echo "Found"
echo "The line is: $line" #Do whatever you want
exit
fi
done < test.dat


Cheers,

rkdugar
 
Old 08-17-2004, 03:59 PM   #6
Veteq
Member
 
Registered: Feb 2004
Posts: 155

Original Poster
Rep: Reputation: 30
thank you for all your help, I was able to complete the script and will be implementing it tomorrow

thank you
 
Old 08-17-2004, 06:28 PM   #7
cracauer
Member
 
Registered: Jul 2004
Location: Boston, MA, USA
Distribution: FreeBSD, Debian/AMD64, Ubuntu/i386
Posts: 59

Rep: Reputation: 15
Folks, there is no reason to call cut or awk for simple splitting, it is hugely expesive to do a fork() and exec() once or twice each line.

The shell has a built-in-input splitter, and it works on the $*/$@ variable.

With "set -- $something" you set $*/$@ to what is in something, which invokes the splitter. Then you can use $1, $2 etc to access the splitted parts of $something.

Example, lets assume your datafile with the lines is in a file called 'foobar.txt'


cat foobar.txt | while read foo ; do
set -- $foo
var1=$1
var2=$2
echo $var1 comes with $var2
done

This is a pentozillion times faster. And since you avoid not only CPU time wastage but you avoid excessive fork()ing, all processes on the machine benefit a lot.
 
Old 08-18-2004, 02:57 AM   #8
apeekaboo
Member
 
Registered: Apr 2003
Location: Stockholm/Sweden
Distribution: Kubuntu, Debian, Slax
Posts: 91

Rep: Reputation: 16
Quote:
Originally posted by cracauer

cat foobar.txt | while read foo ; do
set -- $foo
var1=$1
var2=$2
echo $var1 comes with $var2
done
OK, that's just great... now I have to rewrite all my shell-scripts!
It is neat tricks like this that makes your shell-scripting evolve!
Too bad set doesn't come with a man page... Do you know any resource where I can read more about this?
 
Old 08-18-2004, 03:17 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Being a shell builtin, set is documented in the shell man page:

man bash
Code:
...
SHELL BUILTINS
...
      set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
              Without  options,  the  name and value of each shell variable are
              displayed in a format that can be reused as input.  The output is
              sorted  according to the current locale.  When options are speci-
              fied, they set or unset shell attributes.  Any arguments  remain-
              ing after the options are processed are treated as values for the
              positional parameters and are assigned, in order, to $1, $2,  ...
...
 
Old 08-18-2004, 04:20 AM   #10
apeekaboo
Member
 
Registered: Apr 2003
Location: Stockholm/Sweden
Distribution: Kubuntu, Debian, Slax
Posts: 91

Rep: Reputation: 16
Thanks!
man bash |grep -c set came back with 247 instances on my system. No wonder it was hard to find...

Am I the only one thinking that man pages are written for those that already know most of their content and only want to refresh their memory on some simple parameter?
Code:
               --      If no arguments follow this option, then the  positional
                      parameters are unset.  Otherwise, the positional parame-
                      ters are set to the args, even if  some  of  them  begin
                      with a -.
Maybe this makes sense to a native english speaking person, but without reading cracauer's example I would never have guessed this would allow me to use set like he describes it...

OK, I'll quit my whining... for now.
 
Old 08-18-2004, 07:17 AM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Well, I'm non native english too, and agree I would have never find this use by reading the manual page. I discovered the "set" feature a long time ago, probably while reading someone else's script.

However, the manual pages serve most as reference and usually only formally describe the command, function or file functionality and usage and is not to replace a tutorial.

It is true that the "examples" section is frequently very light if not missing in manual pages, but it would be impossible for a such complex and powerfull tool like a shell is to exhaustively describe every use, tip or trick ...
 
Old 08-18-2004, 08:01 AM   #12
apeekaboo
Member
 
Registered: Apr 2003
Location: Stockholm/Sweden
Distribution: Kubuntu, Debian, Slax
Posts: 91

Rep: Reputation: 16
Quote:
Originally posted by jlliagre
It is true that the "examples" section is frequently very light if not missing in manual pages, but it would be impossible for a such complex and powerfull tool like a shell is to exhaustively describe every use, tip or trick ...
So true... But this is what the forums are for, right?
Sharing tips/tricks and helping eachother out.
I can only imagine how hard it must have been learning Linux before the Internet was as common as it is today.
 
Old 08-18-2004, 08:20 AM   #13
cracauer
Member
 
Registered: Jul 2004
Location: Boston, MA, USA
Distribution: FreeBSD, Debian/AMD64, Ubuntu/i386
Posts: 59

Rep: Reputation: 15
I'm afraid I don't know of a good tutorial either.

I was (am?) the maintainer of FreeBSD's /bin/sh, so I picked up all the gory details the hard way out of the Posix spec and all the dirty tricks out of bug reports in FreeBSD's GNATS.

I heared that O'Reilly's "Unix Power Tools" has some of it, can anybody verify this?
 
  


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 program in c SatYr_84 Linux - Software 1 10-03-2005 06:49 PM
opening a program from shell hari78 Linux - Newbie 4 03-08-2005 10:33 AM
HELP!! I need to program a shell pleowulf Programming 2 02-11-2005 02:12 PM
Shell program error, can anyone see it? hamster Programming 2 04-16-2003 04:10 PM
bash shell program help embsupafly Programming 7 11-27-2002 01:05 AM


All times are GMT -5. The time now is 06:09 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