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 06-13-2013, 08:27 AM   #1
johnthrax
LQ Newbie
 
Registered: Jun 2011
Distribution: CentOS, RHEL
Posts: 10

Rep: Reputation: Disabled
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
 
Old 06-13-2013, 08:43 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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:
 
Old 06-13-2013, 09:12 AM   #3
johnthrax
LQ Newbie
 
Registered: Jun 2011
Distribution: CentOS, RHEL
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
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.
 
Old 06-13-2013, 09:32 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

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

Last edited by druuna; 06-13-2013 at 09:36 AM.
 
Old 06-13-2013, 09:40 AM   #5
johnthrax
LQ Newbie
 
Registered: Jun 2011
Distribution: CentOS, RHEL
Posts: 10

Original Poster
Rep: Reputation: Disabled
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 View Post
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

Last edited by johnthrax; 06-13-2013 at 11:00 AM. Reason: adding feedback
 
  


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
Parse a CSV file output to text file beto Linux - Newbie 3 04-25-2012 08:45 AM
[SOLVED] Shell script help needed to parse ntpd -q output gkasica Programming 4 03-31-2012 06:19 AM
trying to run a script in the background redirect the output to a file jdwalk Linux - Newbie 5 02-12-2010 07:50 AM
[shell script] execute command and parse output stoiss Programming 2 01-26-2009 01:49 AM
parse input text file and generate output TsanChung Programming 5 07-27-2008 10:23 PM


All times are GMT -5. The time now is 10:14 PM.

Main Menu
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