More UNIX BASH scripting help please!

Discussion in 'OT Technology' started by siniquezu, Aug 25, 2004.

  1. siniquezu

    siniquezu New Member

    Joined:
    Aug 8, 2003
    Messages:
    8,669
    Likes Received:
    0
    I have another UNIX bash question if you guys would be so kind.

    I have a data file like so:

    Pencils 4
    Pens 4
    Erasers 6
    Highlights 9

    Now I want to pass each line as arguments $1 and $2 so that I can work with each line individually. I would like to process each line, for example, like so:

    echo "$1 have this volume: $2 item(s)"

    with the following results:

    Pencils have this volume: 4 item(s)
    Pens have this volume: 4 item(s)
    Erasers have this volume: 6 item(s)
    Highlights have this volume: 9 item(s)

    I've been racking my brains. I tried the cat command and the < input but it doesn't process them individually. Any help would be great guys. I think bash may be the wrong language for me...
    Thanks!
     
  2. col_panic

    col_panic calm like a bomb Moderator

    Joined:
    Sep 19, 2003
    Messages:
    188,160
    Likes Received:
    0
    Location:
    winter haven, fl
    why do this in a shell script? perl is better for that stuff


























    or is this a homework question?
     
  3. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,446
    Likes Received:
    12
    Exactly what I was thinking. First a floating point script, now this. But this is very easy to do in shell. I do this all the time when I create plexes and subdisks for my Veritas volumes.

    Code:
    while read input
    do
    set - $input
    echo "$1 have this volume: $2 item(s)"
    done < data.file
    
     
  4. siniquezu

    siniquezu New Member

    Joined:
    Aug 8, 2003
    Messages:
    8,669
    Likes Received:
    0
    I wish I was still in school. I'm trying to do this and learn at the same time for a work project I'm on. Trouble is, I read the unix scripting book already and online already and they aren't any help with these specific routines. Nobody at my work is helpful because they know perl and tcl/tk. Would I have been better off learning tcl/tk pr perl? Isn't perl less adaptable because many systems haven't installed it yet?

    As for the script, the little that everyone knows about bash, they suggest using "cat". Is that ever going to work?
     
  5. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,446
    Likes Received:
    12
    Then why are the questions so school-like? If this was a project for an intern or admin, then the question would be essentially the same, but told in a different manner. You are never going to read about "specific routines" ever, for UNIX scripting, for any scripting, for any coding. You will have to form your own routines based on what you've read.

    You wouldn't necessarily be better off per say, but it wouldn't hurt. Perl supports just about every platform out there. If it's not installed on the systems that you work on, that's another issue.

    I just posted an example. Did you try it? But if you must do it cat, then here's one method of many:


    Code:
    for i in `cat text | awk '{print $1}'`
    do
    echo "$i have this volumes: `grep $i text | awk '{print $2}'` item(s)"
    done
    
    or

    Code:
    cat text | awk '{print $1" have this volumes: "$2" item(s)"}'
    The fact that "everyone" insists on using cat implies that this is for school and not for work.
     
    Last edited: Aug 25, 2004
  6. siniquezu

    siniquezu New Member

    Joined:
    Aug 8, 2003
    Messages:
    8,669
    Likes Received:
    0
    If I were to list the details specifically on how I wanted the help, it would sound too complicated for such a simple problem for you guys. The two guys at work (the so called "everybody") who know just a little bit about bash claimed that cat was the recommended approach but they aren't true programmers but more of neuro researchers who use unix. What I needed to do was make a script that read a list as PET scan input and extract data from the incorporated filenames and process the images downstream. If I were to ask:

    How can I process a file with this list of filenames to echo the 20-26 character, echo the 3-6 character, echo the filenames, and echo the Session number?:
    PCMCDO__-OPTH______0159630001.IMM.1 Session1
    PCMCDO__-OPTH______0159640001.IMM.1 Session2 PCMCDO__-OPTH______0167300001.IMM.1 Session3 PCMCDO__-OPTH______0167310001.IMM.1 Session4

    It would sound more complicated than it should be. I tried to keep it textbook simple so that I could extract the basic principles. I'm not flaming you guys at all. I appreciate and thank you for the assistance. I'm just trying to explain my side.
     
    Last edited: Aug 25, 2004
  7. siniquezu

    siniquezu New Member

    Joined:
    Aug 8, 2003
    Messages:
    8,669
    Likes Received:
    0
    Tested one of them and it worked! I'm happy. I had the problem with the first script because the $2 over wrote the item(s) text. Weird.
    Thanks guys!
     
  8. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,446
    Likes Received:
    12
    Well, it's good to be exact on what you need to do because the examples you have given won't help you jack for this list of PET scan files. You don't have any spaces and it doesn't seem like you have an unique delimiters. So the code I gave were useless. Just ask the scenario, not give examples, because as in your examples, they are nothing like this Pet scan list task.

    Just catting out the file doesn't help either. It's massaging the cat output is the hard part. So just researching cat and basing your resolution on the function of cat is not going to help.

    And you don't need a script to do everything. Although this particular task is where Perl is golden, this is also easily done with awk, not cat.

    I have a file called text with the four lines you listed. Here's a command to echo the 20-26 character, echo the 3-6 character, echo the filenames, and echo the Session number.

    Code:
    # cat text
    PCMCDO__-OPTH______0159630001.IMM.1 Session1
    PCMCDO__-OPTH______0159640001.IMM.1 Session2
    PCMCDO__-OPTH______0167300001.IMM.1 Session3
    PCMCDO__-OPTH______0167310001.IMM.1 Session4
    # awk '{print substr($1,20,7)" "substr($1,3,4)" "$1" "substr($2,8,3)}' text
    0159630 MCDO PCMCDO__-OPTH______0159630001.IMM.1 1
    0159640 MCDO PCMCDO__-OPTH______0159640001.IMM.1 2
    0167300 MCDO PCMCDO__-OPTH______0167300001.IMM.1 3
    0167310 MCDO PCMCDO__-OPTH______0167310001.IMM.1 4
    
    But to make your two developers happy

    Code:
    # cat text | awk '{print substr($1,20,7)" "substr($1,3,4)" "$1" "substr($2,8,3)}'
    0159630 MCDO PCMCDO__-OPTH______0159630001.IMM.1 1
    0159640 MCDO PCMCDO__-OPTH______0159640001.IMM.1 2
    0167300 MCDO PCMCDO__-OPTH______0167300001.IMM.1 3
    0167310 MCDO PCMCDO__-OPTH______0167310001.IMM.1 4
    
    The top assumes that your Session numbers won't be greater than 3 digits. You have to change that accordingly.
     

Share This Page