Review your favorite Linux distribution.
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 02-19-2009, 07:43 AM   #1
Registered: Aug 2008
Posts: 54

Rep: Reputation: 15
Assigning variables by awk(?)

I have a file of the following form:

123 456
234 678
456 789

I want to iterate through each line and save the first number as variable1 and the second as variable2

I was thinking

awk '{print $1}' | (something where I can assign the value to variable1 = $1 or something)

Any suggestions of how to do this or iterate line by line?
Old 02-19-2009, 09:25 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can do that without awk. Just use the read statement of the shell in a while loop. Take in mind that read can accept more than one argument, assigning each field of the current line to the specified variables. Then, within the while loop, you can assign the value of these variables to the elements of an array. In this way you can use each value read from the original file later in the script.
Old 02-19-2009, 12:01 PM   #3
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,143

Rep: Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877
man awk
awk -v totalclaims=$arg1 -v vendor=$arg2 -v vid=$arg5 -v numclaims=$1 \
  'index($0,"H") == 6 && index($0,vid) == 58 {a = a + 1} \
  END {printf " %s - hosp - %d\n", vendor, a / totalclaims * numclaims}' /etc/prod.stats

i think you should post programming related questions in the programming forum:

Last edited by schneidz; 02-19-2009 at 12:03 PM.


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
help with a little perl script and assigning variables sal_paradise42 Programming 8 10-15-2008 12:44 PM
How to make 2 variables from one variable value in awk intikhabalam Linux - General 1 07-30-2008 04:32 AM
Can you use grep / awk on variables instead of files? zest n zeal Linux - Newbie 2 02-11-2008 12:37 PM
awk: /matching/ variables passed with -v aunquarra Linux - General 2 02-17-2005 06:47 PM
C++ Assigning attributes to variables ? xconspirisist Programming 16 11-05-2003 06:08 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:53 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration