Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-25-2007, 07:22 AM   #1
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
how to pipe/redirect awk output into a variable?


I have a simple awk command to read the number of records in a file which is passed to it. I'd like to then save this output as a variable within a bash script. How is this done? The pipe I use below doesn't work.

awk '{ ++x } END { print x }' file_name | read my_variable

Similarly I get an error when I try:

read my_variable < awk '{ ++x } END { print x }' file_name

Old 01-25-2007, 07:49 AM   #2
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,448

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446


my_variable=$(awk '{ ++x } END { print x }' file_name)
Old 01-25-2007, 07:54 AM   #3
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
That's it.



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
Pipe inside variable isn't working in bash Reginald0 Linux - General 6 12-09-2015 10:56 AM
How to redirect streams to the variable? Rostfrei Programming 3 01-20-2007 04:51 AM
Bash Awk Variable problem _hadi_ Programming 5 12-13-2006 01:25 AM
awk on a variable onradius Programming 8 02-22-2006 09:34 AM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 08:47 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:02 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration