problem while storing the output of awk to variable


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers problem while storing the output of awk to variable
# 1  
Old 12-12-2008
Java problem while storing the output of awk to variable

Hi,

i have some files in one directory(say some sample dir) whose names will be like the following.
some_file1.txt
some_file2.txt.

i need to get the last modified file size based on file name pattern like some_

here i am able to get the value of the last modified file size using the following command:
echo `ls -lt /sample/some_* |awk '{print $5}'` | `awk '{ F = " " ; print $1 }'


but the problem is, i need to store this value in one variable, and needs to print that variable value.

i tried several ways like the following.

SIZE=`echo `ls -lt /sample/some_* |awk '{print $5}'` | `awk '{ F = " " ; print $1 }'`
echo $SIZE

but it is not successful.


Your immediate help will be greatly appreciated

Reagrds,
Eswar
# 2  
Old 12-12-2008
Looking for this

Code:
filename=`ls -lt /sample/some_* |awk '{print $5}' | awk '{ F = " " ; print $1 }'`
echo $filename

# 3  
Old 12-12-2008
You may try Perl, to avoid the pipeline:

Code:
size=$(perl -e'
  print -s (sort {-M $a <=> -M $b } glob "some_*")[0]
  ')

# 4  
Old 12-12-2008
Thanks matrixmadhan for your immediate respone.

but i am getting the file size as

2580 111

i.e, both of the file sizes seperated by space.
Note: I have 2 files with name some_ under sample dir, whose file sizes are 2580 and 111.


i need only the last updated file size only.
i.e like 2580 (or) 111


Thanks,
eswar
# 5  
Old 12-12-2008
Thanks matrixmadhan for your immediate respone.

but i am getting the file size as

2580 111

i.e, both of the file sizes seperated by space.
Note: I have 2 files with name some_ under sample dir, whose file sizes are 2580 and 111.


i need only the last updated file size only.
i.e like 2580 (or) 111


Thanks,
eswar
# 6  
Old 12-12-2008
Code:
SIZE=`ls -lrt | awk 'END{ print $5 }'`
echo $SIZE

ls -lrt => display files with file that is last modified at the end
awk 'END{ print $5 }' => list only the file size of the last displayed file

SIZE => will contain the size of the file that is modified latest
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. Shell Programming and Scripting

Storing command output in a variable and using cut/awk

Hi, My aim is to get the md5 hash of a file and store it in a variable. var1="md5sum file1" $var1 The above outputs fine but also contains the filename, so somthing like this 243ASsf25 file1 i just need to get the first part and put it into a variable. var1="md5sum file1"... (5 Replies)
Discussion started by: JustALol
5 Replies

3. Shell Programming and Scripting

Storing output into a variable

My script below seems to be choking because I need the the output of the find command to be stored as a variable that can then be called by used lower in the script. #!/bin/bash cd "/resumes_to_be_completed" var1=find . -mmin -1 -type f \( -name "*.doc" -o -name "*.docx" \)... (1 Reply)
Discussion started by: binary-ninja
1 Replies

4. Shell Programming and Scripting

Problem in storing value to variable

#bash curVer=`cat /var/sadm/clsversion | cut -f 2 -d "_"` echo "CurVer:$curVer" ls |grep -v tar| grep -v sh| grep -v log|cut -f 1 -d "_" | sort -u >tmp1 for line in $(cat tmp1) do ver=`echo $line_$curVer` ls $line* |sort >tmp2 grep -n ${ver} tmp2 >/dev/null ... (2 Replies)
Discussion started by: rajamohan
2 Replies

5. Shell Programming and Scripting

storing output from echo & cut into variable

Hi All, Hope someone can advise here as I have been struggling to find a syntax that works here. I have tried a stack of combination I have seed in the forums but I think because I have needed to use "" and `` in the statments another method is found. I am reading in lines with the following... (1 Reply)
Discussion started by: nkwilliams
1 Replies

6. UNIX for Dummies Questions & Answers

Storing lines of output into a script variable

I'm sure this is a simple thing but I can't figure it out. In a script that I'm writing, I'd like to be able to store each line of output from "ls -l" into a variable. Ultimately I'd like to end up with something like: for a in `ls -l` do something with $a doneBut that's reading each... (2 Replies)
Discussion started by: ewoods
2 Replies

7. 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

8. Shell Programming and Scripting

Using 'defaults read' and storing the output in a variable

Hi all, I'm creating a script which uses 'defaults read' to retrieve details from an Info.plist like this; defaults read "/Path/Contents/Info" CFBundleShortVersionString This works fine in Terminal and returns the expected values. Is it possible to use this command in a script, and... (0 Replies)
Discussion started by: davewg
0 Replies

9. UNIX for Dummies Questions & Answers

Storing the output into a variable

Hi unix gurus, I am trying to store the result of a command into a variable. But it is not getting stored. x='hello' y=echo $x | wc -c but it is giving the output as 0(zero) Pls help me its very urgent (7 Replies)
Discussion started by: ravi raj kumar
7 Replies

10. Shell Programming and Scripting

storing output of awk in variable

HI I am trying to store the output of this awk command awk -F, {(if NR==2) print $1} test.sr in a variable when I am trying v= awk -F, {(if NR==2) print $1} test.sr $v = awk -F, {(if NR==2) print $1} test.sr but its not working out . Any suggestions Thanks Arif (3 Replies)
Discussion started by: mab_arif16
3 Replies
Login or Register to Ask a Question