LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   parse file and use output to run a script (https://www.linuxquestions.org/questions/programming-9/parse-file-and-use-output-to-run-a-script-4175465843/)

johnthrax 06-13-2013 08:27 AM

parse file and use output to run a script
 
I'm trying to grab text out of a line in a file, and then use those words to run another script. This is all bash scripts.

Here is an example of the line I'm trying to grab text out of and how it's formatted:

one|two|three|four|five|six|seven|eight|nine

I'm using a combination of grep and awk to grab just that line, and then two of the words out of that line. When I run the command by itself:

cat file | grep -v "^\#" | awk -F "|" '{print $2, $4}'

it returns what I'm looking for:

two four


I need to run another script using "two four" as input and I'm using a for loop to do a couple things, example:

for i in $(cat file | grep -v "^\#" | awk -F "|" '{print $2, $4}'); do; echo "Adding '$i' to these files"; /bin/sh /pathetoscript/script $i; done

It errors out because it's now placing the output of the cat/grep/awk command as two separate lines, so instead of "two four", it's doing:

two
four

Obviously I'm pretty new to bash scripting, so bear with me, I understand I may not being doing this in the best way possible. I hope I added enough info for everyone to understand. I've tried looking around at a lot of guides/tutorials, and I just can't find an answer to this.

Thanks

druuna 06-13-2013 08:43 AM

You should get rid of the for loop and use a while read loop.

Something like this:
Code:

$ awk -F"|" '{ print $2, $4 }' input | \
while read FIRST SECOND
do
  echo $FIRST
  echo $SECOND
  echo "First is: $FIRST, second is: $SECOND"
done
two
four
First is: two, second is: four

Maybe these links will help:

Bash:
Sed and Awk:

johnthrax 06-13-2013 09:12 AM

Quote:

Originally Posted by druuna (Post 4970980)
You should get rid of the for loop and use a while read loop.

Something like this:
Code:

$ awk -F"|" '{ print $2, $4 }' input | \
while read FIRST SECOND
do
  echo $FIRST
  echo $SECOND
  echo "First is: $FIRST, second is: $SECOND"
done
two
four
First is: two, second is: four

Maybe these links will help:

Bash:
Sed and Awk:

That doesn't give me what I need. I need the output to be two four on the same line, so I can run the script:
/pathtoscript/script two four. Maybe I didn't explain things well enough.

druuna 06-13-2013 09:32 AM

I just gave an example, not a copy/paste solution.

Maybe this clears things up more:
Code:

awk -F"|" '!/^#/ { print $2, $4 }' input | \
while read FIRST SECOND
do
  echo "Adding $FIRST $SECOND to these files"
  /bin/sh /pathetoscript/script $FIRST $SECOND
done


johnthrax 06-13-2013 09:40 AM

sorry, i guess i was thrown off by things being on two lines in your example. that did the trick. thanks so much for the help!

Quote:

Originally Posted by druuna (Post 4971013)
I just gave an example, not a copy/paste solution.

Maybe this clears things up more:
Code:

awk -F"|" '!/^#/ { print $2, $4 }' input | \
while read FIRST SECOND
do
  echo "Adding $FIRST $SECOND to these files"
  /bin/sh /pathetoscript/script $FIRST $SECOND
done




All times are GMT -5. The time now is 03:54 AM.