Results 1 to 10 of 10

Thread: bash script for each line of input

  1. #1
    Join Date
    Jan 2011
    Beans
    47

    bash script for each line of input

    I've written my script so that it reads in the variables $1 $2 $3 $3....$12

    I have an input file as follows

    word1 1 1 2 3 4 5
    word2 1 1 2 3 4 5 6
    word3 1 1 2 3 4

    I would like to take the values from the input file and run the script once for each line.

    i.e.

    ./script word1 1 1 2 3 4 5
    ./script word2 1 1 2 3 4 5 6
    ./script word3 1 1 2 3 4

    please note that the numbers are not nessicarily in ascending order like this and their order is important.

    sorry for the newb question. but I've looked around a bit and the solutions I found did not work for me.

  2. #2
    Join Date
    May 2011
    Location
    Golden, CO
    Beans
    96
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash script for each line of input

    SO let me see if I understand.

    You have an input file.. lets just say input.txt that has a value on each line.
    You want the script to read the file, assign each line to a variable, and then run whatever process on each variable?
    Ubuntu 12.04 for personal use
    Solaris 9, 10 and Red Hat Ent 5 @ work

  3. #3
    Join Date
    Jan 2011
    Beans
    47

    Re: bash script for each line of input

    no I need to input each line into the script. the script gets run one time for each line

    ./script line1

    where line1 (word1 1 2 3 4....) automatically gets broken into

    $1 -> word1
    $2 -> 1
    $3 -> 1
    $4 -> 2
    $5 -> 3
    ...etc

    and the script uses these variable

  4. #4
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,658
    Distro
    Ubuntu

    Re: bash script for each line of input

    Scrub that then ^^^

  5. #5
    Join Date
    Jan 2011
    Beans
    47

    Re: bash script for each line of input

    scrub?

  6. #6
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,658
    Distro
    Ubuntu

    Re: bash script for each line of input

    I wrote how to take each line as a variable. When I saw your answer I scrubbed mine because it was wrong.

  7. #7
    Join Date
    Jan 2011
    Beans
    47

    Re: bash script for each line of input

    needlessly wasteful but is there a way to just cat each line to a file

    ie

    word1 1 2 3 4 5 -> vars1.txt
    word 2 1 2 3 4 -> vars2.txt
    word3 1 2 3 4 5 6 -> vars3.txt

    then I could for loop across all the files cause the script works if I use cat and there is only one line

    there should probably be a one liner for this, but its escaping me at the moment and I'm not running on much sleep

  8. #8
    Join Date
    Jul 2011
    Location
    South-Africa
    Beans
    678
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash script for each line of input

    Not the cleanest approach, but it might work?

    Count the number of "words" in the line, then put each "word" on a new line, in a new temp file if you need to (you should be able to pipe it) then use that new file (or pipe) as a var per line.

  9. #9
    Join Date
    Jul 2011
    Location
    South-Africa
    Beans
    678
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash script for each line of input

    $ echo "cherry apple peach" | tr " " "\n"
    cherry
    apple
    peach

    Take this approach for each line, but pipe that into your program, or to a file with the ">". Then read the output file as a single line. Thus each line of the new file, will be a variable.

    Then as the program executes further, line 2 of the original file, gets broken up again, and the new file is read again, and so it continues..


    A few google searches gave some very decent results. Try it:
    http://www.linuxquestions.org/questi...n-bash-511760/

    http://mandrivausers.org/index.php?/...ine-with-bash/

    also do some research on CAT, GREP, SED etc... some of them you can truncate a file down to a line and coulom. Making the entire process more universal.
    Last edited by zero2xiii; July 21st, 2011 at 02:47 PM. Reason: Added some reference

  10. #10
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,658
    Distro
    Ubuntu

    Re: bash script for each line of input

    Code:
    for w in $(cat blah.txt); do echo $w >> blag.txt; done
    Now each word is on a new line

    Code:
    while read l; do <fancy commands> "$l"; done< blag.txt

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •