assign values from awk output - help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assign values from awk output - help
# 1  
Old 06-18-2008
assign values from awk output - help

Dear All,

I have a command which gives the number of fields of each line of a comma-separated file.

sthng like this :
cat QDB_20071126_002.bad | awk -F"," '{ print NF }'

I need to assign the first output and the last output of the above command to variables in a script.
Need help to do that.

Thanks in advance.
# 2  
Old 06-18-2008
Code:
set -- `awk -F, 'NR==1 { print $NF; next } { last=$NF } END { print last }' QDB_20071126_002.bad`
firstvariable=$1
lastvariable=$2

You could do it with two separate awk invocations or something, but the above is more efficient (although somewhat obscure, the set -- `command` trick is a standard shell programming idiom).
# 3  
Old 06-18-2008
Hi,

Thanks for your quick reply.
Your command actually prints the last field of first and last line, whereas what i need is the number of fields of first and last line, to be assigned to variables.

Lines are comma-separated.

The below command prints number of fields of each line of the comma-separated file:
cat QDB_20071126_002.bad | awk -F"," '{ print NF }'

6
7
7
7
2

I need to take '6' and '2' and assign it later.
Help me out on this..

Thanks in advance.
# 4  
Old 06-18-2008
Sorry, my bad, just remove the dollar signs before NF in both places; sorry for missing that.

By the by, awk can read the file all by itself; no need to invoke a separate cat for that.
# 5  
Old 06-18-2008
you can assign variables back to the shell using set as era has mentioned
Code:
set -- `awk 'NR==1{print NF}END{print NF}' file`

or just do whatever you need to do in awk itself
Code:
awk 'NR==1{first=NF}END{ 
   print NF #do something.
   print f # do something
}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to assign awk values to shell variable?

Hi Gurus, I have a script which assign awk output to shell variable. current it uses two awk command to assign value to two variables. I want to use one command to assign two values to two variables. I tried the code, but it does't work. kindly provide your suggestion. current code... (2 Replies)
Discussion started by: green_k
2 Replies

2. Shell Programming and Scripting

Using awk to assign binary values to data above/below a certain threshold?

Hello, I have files containing a large amount of values in columns, and I want to simplify the data by making the values binary, i.e. assigning 1/0 for each value above or below a certain threshold. I am curious to know if it's possible to use awk to do this (or another way in bash). I've... (2 Replies)
Discussion started by: ksennin
2 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. Shell Programming and Scripting

awk assign output of array to specific field-number

With this script i want to print the output to a specific field-number . Can anybody help? awk 'NR=FNR{split(FILENAME,fn,"_");nr=$2;f = $1} END{for (i=1;i<=f;i++) print i,$fn=nr}' input_5.csv input_6.csvinput_5.csv 4 135 5 185 6 85 11 30input_6.csv 1 90 3 58 4 135 7 60 8 55 10... (1 Reply)
Discussion started by: sdf
1 Replies

5. Shell Programming and Scripting

help on awk---- need to assign the output of awk to a variable

hi i want to find the size of a folder and assign it to a variable and then compare if it is greater than 1 gb. i am doin this script, but it is throwing error.... #!/bin/ksh cd . | du -s | size = awk '{print $1}' if size >= 112000 then echo size high fi ERROR : (4 Replies)
Discussion started by: Nithz
4 Replies

6. UNIX for Dummies Questions & Answers

using awk iteratively in a script to assign variable values

I have a log file that has certain fields that I want to evaluate, and depending on the value in those fields, I want to put the value of a different field in that line in a particular variable that I'll use later on down the log file. Sort of like setting a switch to change what I do with a bunch... (5 Replies)
Discussion started by: pts2
5 Replies

7. Shell Programming and Scripting

Assign values to variable

Hi Masters, I want to assign the values of one variable to another variable. Here the varaible name 'var' is dynamic. I know the values of V_2 and U_3, but If the i/p of TYPE is 'U' and the NO is 3, then I want to assign the values of U_3 to var. How we can achieve it? TYPE="U"... (4 Replies)
Discussion started by: ecearund
4 Replies

8. Shell Programming and Scripting

How to grep/awk/egrep two values for given output?

Dear Friends, I have a command which can result following output. Packet is: /var/adm/yyyy/pkt6043 Intended for network : /vob/repo I would like to retrive pkt6043 and /vob/repo using single command. Blue color test will be always contstant and red color text will be dynamic ... (2 Replies)
Discussion started by: baluchen
2 Replies

9. Shell Programming and Scripting

assign awk output to bash variable

greetings all, I am have a heck of a time trying to accomplish a very simple thing. I have an array of "shortname<spaces>id" created from a dscl output. I want to assign shortname=word1 and id=word2. I have tried shortname=$(${textArray} | awk '{print $1}') - and get 'awk : cannot open... (3 Replies)
Discussion started by: macnetdaemon
3 Replies

10. Shell Programming and Scripting

typeset and values returned from awk output

Somebody can please give a highlight on this. The problem shows only on Linux(Redhat) not any other unix flavors :confused: Linux : $unset m $m=`find . -newer rman_padev_20051206195000.out -name "*L0.rman" -exec ls -l {} \; | awk '{ s+=$5 } END{printf("%.0f", s)}'` $echo $m 7425089536... (0 Replies)
Discussion started by: prathom
0 Replies
Login or Register to Ask a Question