populate a bash variable from an awk operation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting populate a bash variable from an awk operation
# 1  
Old 04-07-2011
populate a bash variable from an awk operation

Hi,

I'm trying to populate bash script variable, data_size with the
size of the largest file in my current directory

data_size=$(ls -lS | grep -v "total" | head -1) | awk '{ print $5 }'

I've tried adding an echo before the call to awk

data_size=$(ls -l | grep -v "total" | head -1) | echo | awk '{ print $5 }'

data_size is echoed as blank.
Appreciate any help that is offered.
Thanks
# 2  
Old 04-07-2011
Code:
(08:28:29\[D@DeCoWork15)
[~]$ data_size=$(ls -lS | grep -v "total" | head -1 | awk '{ print $5 }')

(08:28:37\[D@DeCoWork15)
[~]$ echo $data_size
6671723580

Just put the closing parentheses out side of the awk statement. Remember, you're setting the value of the variable to everything between the two parentheses.
This User Gave Thanks to DeCoTwc For This Post:
# 3  
Old 04-07-2011
Wrench

Thanks, typo on my part - I'd better start using vim instead of vi ...

---------- Post updated at 07:48 AM ---------- Previous update was at 07:37 AM ----------

Is it also possible to extract an array value, which consists of multiple space delimited alphanumeric fields (eg '1 abcd efdg')
using something like the following (from within a loop which cycles thro' data_array):

data_id=$(${data_array[$element2]}|awk '{print $1}')

or

data_id=$(${data_array[$element2]}|echo|awk '{print $1}')

(Both don't work)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable expansion in awk script

Hello, I need to split a file into two of different locations by re-direction in awk. cat infle aaa 1 3 bbb 2 4 aaa 3 3 bbb 4 4 aaa 5 3 bbb 6 4 cat /storage/tmp/group_a.gtf aaa 1 3 aaa 3 3 aaa 5 3 cat /storage/tmp/group_b.gtf bbb 2 4 bbb ... (2 Replies)
Discussion started by: yifangt
2 Replies

2. Shell Programming and Scripting

Problem with awk instructions and bash variable

Hi everyone, I'm trying to write a small script to automatize row data treatment. However, I got some trouble with the awk command. I want to use awk to extract a define paragraph from a text file. The first and final lines are defined externally in two variables called debut and fin. I... (2 Replies)
Discussion started by: TeaTimeSF
2 Replies

3. Shell Programming and Scripting

Populate Column Value Using AWK

Hi All, I am trying to find out awk one liner for the following requirement but failed in doing that. I have two files. Content of first file: file1.txt SUCCESS A file2.txt SUCCESS B file3.txt SUCCESS A file4.txt FAIL ... (7 Replies)
Discussion started by: angshuman
7 Replies

4. Shell Programming and Scripting

Variable%pattern operation within awk

Hello; I have: ll | grep -v ^d | awk '{print $9}' rcx_access_report_fid.txt rcx_access_report_hsi.txt rcx_access_report_mmm.txt rcx_access_report_qsc.txt I want to get: rcx_access_report_fid.txt rcx_access_report_hsi rcx_access_report_mmm rcx_access_report_qsc But when I try: (9 Replies)
Discussion started by: delphys
9 Replies

5. Shell Programming and Scripting

awk not escape my bash variable

I tried to parse data from switch configuration files vlan 1727 name SQ5506-15 by port tagged ethe 8/1 to 8/2 untagged ethe 1/13 ! vlan 2105 name SQ5620-7007(BR2) by port tagged ethe 8/1 to 8/2 untagged ethe 1/17 ! interface ethernet 1/13 port-name SQ5506-15.nic0 rate-limit... (2 Replies)
Discussion started by: winggundamth
2 Replies

6. Shell Programming and Scripting

PHP populate variable with function

Hi, I've a php scirpt that calls in an external class. The external class, myClass, has several public and private functions. There is one public function which derives a value that I want be able to bring it to the main php scirpt. Here is the code. ....snip.... include... (1 Reply)
Discussion started by: nitin
1 Replies

7. Shell Programming and Scripting

Creating variable using awk in bash

I would like to create a variable within my bash script using awk. I'm reading in a line from an external file, then outputting to a new file in a specific format. But, it doesnt quite work as I have expected and could use some help. (A pertinent excerpt of ) the bash code is: count=1 ... (4 Replies)
Discussion started by: snoitacilppa
4 Replies

8. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies

9. Shell Programming and Scripting

running command remotely to populate local variable

If I run this # ssh remote-server 'du -sk /usr/platform/`uname -i`/' 174 /usr/platform/SUNW,Sun-Fire-V245 I get my output just fine, However, if i try to do the same but populate a local variable within my script called for example 'result' #!/bin/ksh result=`ssh remote-server... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

10. Shell Programming and Scripting

populate variable from remote result

Hi there i am trying to pass the result of a remote command into a variable into my script ie #!/bin/sh var = `ssh remote_box 'uname'` echo $var but all i get back is ./script.sh: var: not found However when i add a -x to put it into diag mode i get the following + ssh... (2 Replies)
Discussion started by: hcclnoodles
2 Replies
Login or Register to Ask a Question