Assigning output from awk to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning output from awk to variable
# 1  
Old 12-27-2012
Assigning output from awk to variable

I have a script whose contents are as below

Code:
        result= awk 's=100 END  {print  s }' 
        echo "The result is" $result

The desired output is
Code:
    The result is 100

My script is running without exiting and i am also not getting the desired output.
Please help
# 2  
Old 12-27-2012
where is the input to your awk ? and what are you trying to achieve. If all you want is to assign value to variable you can do it directly right . whats the point in using awk ?
# 3  
Old 12-27-2012
awk command and variable assigment

I have a MyFile.xml whose contents are as below

Code:
<root>
    <Main>
            <someothertag>..</someothertag>
        <Amt Ccy="EUR">13</Amt>
    </Main>
                .
                .
                .
                some other tags
    <Main>
          <someothertag>..</someothertag>
             <Amt Ccy="SGD">10</Amt>
    </Main>
    <another>
      <Amt Ccy="EUR">10</Amt>
     </another>
</root>

I have script file whose contents are as below

Code:
result = `awk '/<Main>/ { f=1 } f && /Amt/ { split($0,a,/[<>]/); s+=a[3] } /<\/Main>/ { f=0 } END {print  s }' MyFile.xml`
echo "The result is " $result

But i am getting output as

Code:
result: 0653-690 Cannot open =.
result: 0653-690 Cannot open 23.
The result is

My Expected output is

Code:
The result is 23


Last edited by Scott; 12-27-2012 at 07:48 AM.. Reason: Merged from another thread
# 4  
Old 12-27-2012
I guess you opened two threads already for the same issue , and this is the third one.

Last edited by Scott; 12-27-2012 at 07:48 AM.. Reason: Merged from another thread
# 5  
Old 12-27-2012
Code:
result=$(awk 'BEGIN {s=100;print s}')
echo "The result is" $result

The result is 100


Last edited by Scott; 12-27-2012 at 07:48 AM.. Reason: Merged from another thread
# 6  
Old 12-27-2012
Code:
result=$(awk -F"[<>]" '/EUR/ {a+=$3} END {print a}' MyFile.xml)
echo "The result is " $result

No need to assign value to an variable.
Code:
echo -n "The result is "
awk -F"[<>]" '/EUR/ {a+=$3} END {print a}' MyFile.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Tcsh command for assigning output of awk to variable

Hi I have a text file with 2 values and I am trying to assign each value to a variable and then write those to text files. So if the textfile is data.txt with 2 values x and y I want to assign mean=x, and stdev=y and then write these out in text files alongwith the id ($id has already been... (6 Replies)
Discussion started by: violin
6 Replies

2. Shell Programming and Scripting

Assigning bc output to a variable

I'm converting decimal to integer with bc, and I'd like to assign the integer output from bc to a variable 'val'. E.g. In the code below: If b is 5000.000, lines 6 and 8 will output: 5000 (5000.000+0.5)/1 | bc I'd like val to take the value 5000 though, rather than 5000.000 Does someone... (3 Replies)
Discussion started by: pina
3 Replies

3. Shell Programming and Scripting

Piping and assigning output to a variable in Perl

Hi All, I am trying to convert the below Csh code into Perl. But i have the following error. Can any expert help ? Error: ls: *tac: No such file or directory Csh set $ST_file = `ls -rt *$testid*st*|tail -1`; Perl my $ST_file = `ls -rt *$testid*st*|tail -1`; (10 Replies)
Discussion started by: Raynon
10 Replies

4. Shell Programming and Scripting

Problem with assigning output of grep + awk to a variable

Hi All, I am getting the output for the following command when i run it on the unix console. --------------------------- grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3 ---------------------------- But i made it into a script and tried to print the variable, its... (5 Replies)
Discussion started by: meheretoknow
5 Replies

5. Shell Programming and Scripting

assigning SED output to a variable = trouble!

i'm on a Mac running BSD unix. i have a script in which i ask the user to input the name of a mounted volume. i then call SED to substitute backslashes and spaces in place of the spaces. that looks like this: echo "Enter the name of the volume" read Volume echo "You've chosen \"$Volume\""... (7 Replies)
Discussion started by: hungryd
7 Replies

6. Shell Programming and Scripting

Assigning output of a command to variable

When I run time -p <command>, it outputs: real X.XX user X.XX sys X.XXwhere X.XX is seconds. How I can take just that first number output, the seconds of real time, and assign that to a variable? (9 Replies)
Discussion started by: jeriryan87
9 Replies

7. Shell Programming and Scripting

Assigning output to a variable

I am new to unix shell scripting. I was trying to convert each lines in a file to upper case. I know how to convert the whole file. But here i have to do line by line. I am getting it in the below mentioned script #!/bin/bash #converting lower to upper in a file #tr "" "" <file1... (3 Replies)
Discussion started by: jpmena
3 Replies

8. Shell Programming and Scripting

assigning nawk output to shell variable

Hello friends, I doing the follwing script , but found problem to store it to a shell variable. #! /bin/sh for temp in `find ./dat/vector/ -name '*.file'` do echo $temp nawk -v temp=$temp 'BEGIN{ split(temp, a,"\/"); print a}' done output: ./dat/vector/drf_all_002.file... (6 Replies)
Discussion started by: user_prady
6 Replies

9. Shell Programming and Scripting

assigning command output to a shell variable

I have the sql file cde.sql with the below contents: abcdefghij abcwhendefothers sdfghj when no one else when others wwhen%others exception when others Now I want to search for the strings containing when others together and ceck whether that does not occur more than once in the... (2 Replies)
Discussion started by: kprattip
2 Replies

10. Shell Programming and Scripting

Assigning output of command to a variable

Hi, I'm trying to assign the output of a command to a variable and then concat it with another string, however, it keeps overwriting the original string instead of adding on to the end of the string. Contents of test.txt --> This is a test var1="`head -n 1 test.txt`" echo $var1 (This is a... (5 Replies)
Discussion started by: oma04
5 Replies
Login or Register to Ask a Question