LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 10-23-2008, 01:45 PM   #1
NewnanNOC
LQ Newbie
 
Registered: Apr 2003
Location: Atlanta, GA
Distribution: Redhat 8.0
Posts: 8

Rep: Reputation: 0
Question 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!!!
 
Old 10-23-2008, 01:48 PM   #2
Damoek
Member
 
Registered: Mar 2004
Location: Us
Distribution: Ubuntu Dapper
Posts: 74

Rep: Reputation: 15
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)
 
Old 10-23-2008, 01:54 PM   #3
NewnanNOC
LQ Newbie
 
Registered: Apr 2003
Location: Atlanta, GA
Distribution: Redhat 8.0
Posts: 8

Original Poster
Rep: Reputation: 0
Thumbs up That you!

Quote:
Originally Posted by Damoek View Post
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!!
 
Old 10-23-2008, 02:08 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
awk isn't my favoured way for such a trivial thing though...

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

simpler. easier.
 
  


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
passing a shell variable into awk - syntax for correct interpretation? johnpaulodonnell Programming 3 06-19-2007 12:13 PM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 07:47 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM
How do I zip and attach the output data of a grep | awk | mail shell script? 360 Programming 1 05-08-2002 08:26 AM


All times are GMT -5. The time now is 05:45 AM.

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