Sponsored Content
Top Forums Shell Programming and Scripting Assign value to external variables from awk Post 302531966 by Spleshmen on Sunday 19th of June 2011 04:18:37 AM
Old 06-19-2011
Assign value to external variables from awk

Hello
I have a text file with the next pattern
Code:
Name,Year,Grade1,Grade2,Grade3
Name,Year,Grade1,Grade2,Grade3
Name,Year,Grade1,Grade2,Grade3

I want to assign to external variables the grades using the awk method.
After i read the file line by line in order to get the grades i use this
Code:
grade1=echo $line | awk -F, '{ print $3 }'
grade2=echo $line | awk -F, '{ print $4 }'
grade3=echo $line | awk -F, '{ print $5 }'

Is there a way to assign values returned by awk to external variables from inside the awk command?
I know the next code wont work but something like this
Code:
echo $line | awk -F, '{ grade1= $3; grade2= $4; grade3= $5; }'

Thanks
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign variables with cut

I need to read a file (a list) and assign the value to a variable (for each line), I'm looping until the end of the file. My problem is, I want to assign 2 separate variables from the list. The process I'm using is: awk '{print $3}' file1 > file2 awk '{print $4}' file1 > file3 cat file2... (2 Replies)
Discussion started by: douknownam
2 Replies

2. Shell Programming and Scripting

awk - arithemetic functions with external variables

I'm trying to get awk to do arithmetic functions with external variables and I'm getting an error that I cannot figure out how to fix. Insight would be appreciated money=$1 rate1=$(awk -F"\t " '/'$converting'/{print $3}' convert.table) rate2=$(awk -F"\t"... (2 Replies)
Discussion started by: DKNUCKLES
2 Replies

3. Shell Programming and Scripting

Awk/shell question: Read from file and assign to variables.

Is anyone able to help with writing a program that will do the following: 1. Read the contents of a file, line by line, and on each line, assign each of the two columns to a shell variable. 2. perform an action on the variables 3. Read the next line. Here is what I've gotten so far. ... (3 Replies)
Discussion started by: akbar
3 Replies

4. Shell Programming and Scripting

Substituting variables from external files

Hi All, I have the following problem: 1. I have a file containing a line: a,b,d,${d},e,f 2. From within a script I grep the file for '^a,' to get the line 3. I obtain the fourth field as follows: Field4="$( print -r $fileEntry | cut -d, -f4 )" 4. The script exports variables at the... (1 Reply)
Discussion started by: oneillc9
1 Replies

5. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

6. Shell Programming and Scripting

how to assign a value to several variables inside awk call

Hi I am using 'awk" to get file size and date of the file: op - sundev3 $ ls -l /bb/bin/tsfiles.sel | awk '{print $5 $6 $7}' 1587May8 May be somobody knows the way to combine this command with variable assignmet inside the awk, so I would have variables SIZE, MONTH and DATE assigned after... (1 Reply)
Discussion started by: aoussenko
1 Replies

7. Shell Programming and Scripting

match and assign variables

Hi guys, I'm currently writing a script for automating a FreeBSD ZFS setup (ZFSonRooT). I got stuck at one point for raidz(1,2 a.k.a raid5,6) and am in need of assistance. This is what I need. example: #!/bin/sh <- must stay sh echo -n "hdd list: " read hdd_list echo -n "hdd label list:... (2 Replies)
Discussion started by: da1
2 Replies

8. Shell Programming and Scripting

How can we assign value to an array variable from an external file?

is it possible to assign value to an array variable from an external file?? if yes then how?? I am using below code but its not working. #!bin/bash myarray < file_name echo ${mayarray} (6 Replies)
Discussion started by: mukulverma2408
6 Replies

9. Shell Programming and Scripting

awk - Why can't value of awk variables be passed to external functions ?

I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post. function my_func_local { echo "In func $1" } export -f my_func_local echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) { if ($k == "a" ) { system("my_local_func $k") } else{... (19 Replies)
Discussion started by: sreyan32
19 Replies

10. Shell Programming and Scripting

awk to assign points to variables based on conditions and update specific field

I have been reading old posts and trying to come up with a solution for the below: Use a tab-delimited input file to assign point to variables that are used to update a specific field, Rank. I really couldn't find too much in the way of assigning points to variable, but made an attempt at an awk... (4 Replies)
Discussion started by: cmccabe
4 Replies
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 05:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy