LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 03: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, 05:24 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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 05:25 AM.
 
Old 03-07-2005, 04: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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:14 AM.

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
Open Source Consulting | Domain Registration