LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   passing awk output to $variable for shell script? (http://www.linuxquestions.org/questions/programming-9/passing-awk-output-to-%24variable-for-shell-script-678610/)

NewnanNOC 10-23-2008 01:45 PM

passing awk output to $variable for shell script?
 
Greetings, All.

I'm trying to get some data out of fileA.txt for use as a variable in script.sh. I think I want to use awk, but I'm not sure how to go about it.

fileA.txt:

Code:

1234567890,207699107669,00AABBCCDDB7:1,A
awk:

Code:

awk 'BEGIN { FS = "," } ; { print $1 }' fileA.txt
that gives me 1234567890, which is what I want - but I want to use it in place of $variable in:

Code:

#!/bin/bash

variable=$1

sed "s|aaaa|$variable|g" <template.txt> readyfile.txt

I hope that makes sense, and I'm reasonably sure it's something simple, but I just can seem to get it together...

Any assistance is appreciated!!!

Damoek 10-23-2008 01:48 PM

I think all you have to do is set the variable to the awk command in the script itself

variable=$(awk 'BEGIN { FS = "," } ; { print $1 }' /path/to/fileA.txt)

NewnanNOC 10-23-2008 01:54 PM

That you!
 
Quote:

Originally Posted by Damoek (Post 3319956)
I think all you have to do is set the variable to the awk command in the script itself

variable=$(awk 'BEGIN { FS = "," } ; { print $1 }' /path/to/fileA.txt)

That's perfect!!! Thank you so much - I was obiviously trying to over-complicate it...

Thanks again!!

acid_kewpie 10-23-2008 02:08 PM

awk isn't my favoured way for such a trivial thing though...

variable=$(cut -d, -f1 fileA.txt)

simpler. easier.


All times are GMT -5. The time now is 08:59 AM.