LinuxQuestions.org
Review your favorite Linux distribution.
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 03-04-2005, 04:27 AM   #1
cjs_pro
LQ Newbie
 
Registered: Jul 2004
Posts: 8

Rep: Reputation: 0
pass value from awk back to bash shell


Hi all,

Here is my bash script:

VERSION=""
awk 'BEGIN {
file='"'$SOURCE_PATH'"'
while ( (getline aline < file) > 0 )
{
if ($aline ~ /Id=[0-9]*.[0-9]*/)
{
print aline
VERSION=aline
break
}
}
}'
echo "$VERSION"

What I tried to do is to use awk get a line matching a pattern and pass the line back to bash shell script. I got "print aline" outputing the matched line. However, I was unable to get VERSION out of awk. So, echo "$VERSION" has empty. Could anyone tell me how to get a value back to bash shell from awk?

Thank you
 
Old 03-04-2005, 06:24 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
You can not set a bash variable directly from awk. But you can have bash execute awk and assign its output to a variable with $(command).

Like this:
Code:
#!/bin/bash

SOURCE_PATH=file.txt

VERSION=$(awk 'BEGIN {
    file="'$SOURCE_PATH'"
    while ( (getline aline < file) > 0 ) {
      if (aline ~ /Id=[0-9]*.[0-9]*/) {
        print aline
        break
      }
    }
  }')

echo "Version line: $VERSION"
But in your case (just print 1 matching line) I'd use grep instead of awk:
Code:
#!/bin/bash

SOURCE_PATH=file.txt
VERSION=$(grep 'Id=[0-9]*.[0-9]*' "$SOURCE_PATH")
echo "Version line: $VERSION"
Or at least simplify the awk script:
Code:
#!/bin/bash

SOURCE_PATH=file.txt
VERSION=$(awk '/Id=[0-9]*.[0-9]*/ {print $0}'  "$SOURCE_PATH")
echo "Version line : $VERSION"

Last edited by Hko; 03-04-2005 at 06:25 AM.
 
Old 03-07-2005, 05:53 PM   #3
cjs_pro
LQ Newbie
 
Registered: Jul 2004
Posts: 8

Original Poster
Rep: Reputation: 0
Hi HKO,

Thank you. The short form of awk is working perfectly with a minor change on wildcard (* -> +) to make an exact match in my case.

Thank you once more
Chris
 
  


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
how to change and pass thread parameter back to main dragondad Programming 3 09-19-2005 02:58 PM
[Bash] Concatenate string using awk senorsnor Programming 7 05-05-2005 01:38 AM
Bash script question (grep and awk) hamish Linux - Software 6 04-06-2005 04:14 PM
How to loop or sort in bash, awk or sed? j4r0d Programming 1 09-09-2004 04:22 AM
Awk and Shell CMD Output xanthium Programming 16 04-24-2002 07:13 AM


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

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