How to change awk and shell variable value?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change awk and shell variable value?
# 1  
Old 08-20-2010
How to change awk and shell variable value?

In awk script,
Code:
#!/bin/sh

awk 'BEGIN{i=0;}{i=i+5;}END{print i}' in.txt

vr=0;
vr=$i;
echo "$vr"

How can i assign that value of i in $vr(variable) of shell script?
# 2  
Old 08-20-2010
Try:
Code:
vr=`awk 'BEGIN{i=0;}{i=i+5;}END{print i}' in.txt`

# 3  
Old 08-20-2010
Quote:
Originally Posted by kevintse
Try:
Code:
vr=`awk 'BEGIN{i=0;}{i=i+5;}END{print i}' in.txt`

If this is the code:
Code:
#!/bin/sh

vr=0
awk '
BEGIN{a=0;i=0;}
{
    a=a+$5;
    arr[i]=$5;
    i++;
}
END{
    print a;
    for(j=0;j<i;j++){
        print arr[j];
    }
}' in_awk.txt)

I want to get the value of i to "$vr" variable.
How can i do that?
# 4  
Old 08-20-2010
I don't know if there's a standard way of doing this, I just come up with this and it works.

Code:
#!/bin/sh

vr=0
vr=`awk '
BEGIN{a=0;i=0;}
{
    a=a+$5;
    arr[i]=$5;
    i++;
}
END{
    print a;
    for(j=0;j<i;j++){
        print arr[j];
    }
     print i
}' in_awk.txt | tail -n 1`

echo $vr



---------- Post updated at 12:40 AM ---------- Previous update was at 12:35 AM ----------

Well there's another problem with this solution, all "print"s in AWK don't get printed to stdout...so it's not perfect.
# 5  
Old 08-20-2010
Quote:
Originally Posted by cola
If this is the code:
Code:
#!/bin/sh

vr=0
awk '
BEGIN{a=0;i=0;}
{
    a=a+$5;
    arr[i]=$5;
    i++;
}
END{
    print a;
    for(j=0;j<i;j++){
        print arr[j];
    }
}' in_awk.txt)

I want to get the value of i to "$vr" variable.
How can i do that?
Do you want to store all the lines in one variable or to process the lines one by one in your shell script?
# 6  
Old 08-20-2010
Quote:
Originally Posted by Franklin52
Do you want to store all the lines in one variable or to process the lines one by one in your shell script?
Hello,
I just want to get the value of i after processing every line.
Something like this:
vr=$i
There are seven lines in in.txt file.
So the value of i and vr would be 7.
# 7  
Old 08-20-2010
Quote:
Originally Posted by cola
Hello,
I just want to get the value of i after processing every line.
Something like this:
vr=$i
There are seven lines in in.txt file.
So the value of i and vr would be 7.
Post your input file and the desired output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Make change to variable value inside of awk script

Hello, I have text data that looks like this, Mrv16a3102061815532D 6 6 0 0 0 0 999 V2000 -0.4018 1.9634 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -1.1163 1.5509 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -1.1163 0.7259 ... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

3. Shell Programming and Scripting

Awk: How to get an awk variable out to the shell, using system() ?

I am reasonably capable with awk and its quirks, but not with shell weirdness. This has to be Bourne Shell for portability reasons. I have an awk program that is working just fine; it handles multiple input streams and produces several reports, based on the request (-v Variables). In addition... (3 Replies)
Discussion started by: DerekAsirvadem
3 Replies

4. Shell Programming and Scripting

Change default shell of a specific user with awk

I would like to replicate the functionality of chsh (or passwd -e) by awk. This is what I got so far, but I think there should be an easier way to search and replace field $7 only for lines beginning with user_name: awk -v user_name="$user_name" -v new_shell="$new_shell" -F: '$1 == user_name {... (2 Replies)
Discussion started by: nomad84
2 Replies

5. UNIX for Dummies Questions & Answers

awk - change variable on fly

Hello All, I have csv file, where one of fields is date (yyyy/mm/dd 00:00:00). Using awk I am trying to find all records with date newer/older than specific date. My idea was to compare unix timestamps of both dates: start=`date +%s -d "$DateStart"` awk -v start="$start" -v current=`date +%s... (34 Replies)
Discussion started by: haczyk
34 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. Shell Programming and Scripting

awk: change string variable

Hi. How to change string variable in awk? for example, I parse with awk script text file named some_name_with_extension.txt I want to print only some_name in my script .... varCompName = FILENAME print varCompName How to put not all symbols from FILENAME to variable? thank you This... (4 Replies)
Discussion started by: cintlt
4 Replies

8. Shell Programming and Scripting

Shell variable in awk

I have 3 files and each contain m*n matrix. m = number of rows (horizontal lines) n = number of columns (entries in a particular line) What I wish to find is the sum of the 2nd number in the last row. Ex file1.dat 2 5 8 8 4 6 7 8 3 8 3 7 file2.dat 3 4 1 4 8 4 0 3 4 7 3 7 ... (3 Replies)
Discussion started by: dynamics
3 Replies

9. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

10. Shell Programming and Scripting

Not able to change the environment variable in k shell

Hi All, I am trying the following to set the environment variable in my scirpt.But it is not setting with the correct value. Can you guys please help me out to get the correct value. 1. I have environment variable NLS_LANG=American_America.UTF8 2. In my script (ksh) i am trying the following... (1 Reply)
Discussion started by: girish.raos
1 Replies
Login or Register to Ask a Question