How to use values in variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use values in variables
# 1  
Old 06-01-2010
How to use values in variables

Hi Legends,

Please help me in solving the below:

I have the file size and file name using `ls` and awk command.
Code:
ls -ltr *.pf |awk '{print $5, $9}'
2000003072 ABC.pf
2000003072 DEF.pf
2000003072 GHI.pf
56000588    JKL.pf
2000003072 MNO.pf
2000003072 PQR.pf


Now, I want to take the first field (file size) and second field (filename) and want to run my own copy file utility.

Example: From the above list.
First i want to take $1 as 2000003072
Second i want to take $2 as ABC.pf

then want to run a for loop based on the list that contains the file names from above output i.e. (ABC.pf DEF.pf GHI.pf...)

Example:
Code:
For i in ABC.pf DEF.pf GHI.pf...
do
my code which will take filename (as $i) and then the corresponding file size , filesize may differ)
echo "Completed $i"
done

OR

Please let me know, if WHILE loop will be more easier and robust in these cases ??? Because i need to copy the binary files as well. little mistake will corrupt the file and can result in data loss.

---------- Post updated at 06:21 PM ---------- Previous update was at 06:07 PM ----------

I guess i found the way.

Code:
ls -ltr *.pf |awk '{print $5, $9}' | while read SIZE NAMES; do echo "size is $SIZE , and name is $NAMES"; < My code> ; done

Please do let me know if any other possible solution as well.

Last edited by Franklin52; 06-02-2010 at 03:23 AM.. Reason: Please use code tags!
# 2  
Old 06-02-2010
awk is huge progam to use in this kind of small parser. It's always better to use builtin properties, if possible.

Parse using read:
Code:
ls -ltr *.pf | while read f1 f2 f3 f4 SIZE f6 f7 f8 NAMES
do
        ...
done

Parse using array:
Code:
ls -ltr *.pf | while read line
do
       flds=($line)           #parse line to array flds using delimiters which are defined in variable IFS
       SIZE="${flds[4]}"  # 0=1st fld
       NAMES="${flds[8]}"
       numOfFlds=${#flds[@]}
        ...
done

This User Gave Thanks to kshji For This Post:
# 3  
Old 06-02-2010
Thanks kshji. it works well. Smilie
# 4  
Old 06-02-2010
If u just need to store the file names in another file then u can try this option.

Code:
$ cat run.sh
read name
ls $name >> file_name.txt

Make sure while entering the file pattern you put a *.
Example: err*
In this case file_name.txt will have all the files matching the patter.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract multiple values into corresponding variables

Here is my input # MANIFEST.MF Manifest-Version: 1.0 Build-Jdk: 1.6.0 Built-By: CM_TEAM Build_SvnRev: 662789 Build_Number: 13.0.0.0-JDK8 Build_Date: Wed 04/05/2017-20:48:19.17 Archiver-Version: Plexus Archiver Created-By: Apache Maven 3.1.0 Here is the expected output:... (4 Replies)
Discussion started by: kchinnam
4 Replies

2. Shell Programming and Scripting

A better way to assign values to variables - shell

so i've been used to doing it this way: SVAL=$(echo "7 3 2 38 3" | awk '{print $2}') 4VAL=$(echo "4:21:N:3" | awk -F":" '{print $4}') I know there's a way to do it by putting the value in an array and assigning it that way. but i'm not sure how to do it efficiently. any ideas? i dont... (9 Replies)
Discussion started by: SkySmart
9 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

Help in assigning values to variables from the file

Hi! This might be a simple thing, but I'm struggling to assign values to variables from the file. I've the following values stored in the file.. It consists of only two rows.. 10 20 I want to assign the first row value to variable "n1" and the second row value to variable "n2".. That is ... (3 Replies)
Discussion started by: abk07
3 Replies

5. Shell Programming and Scripting

bash -- concatenating values from variables

Hi This is a simple one but I got a lost in translation when doing. What I want to do, given both variables in the example below, to get one value at the time from both variables, for example: 1:a 2:b etc... I need to get this in bash scripting code: varas="1 2 3 4" varbs="a b c d"... (4 Replies)
Discussion started by: ranmanh
4 Replies

6. Shell Programming and Scripting

Getting values from a block into variables

Hi there, If I run "ipmitool bmc info" on any of my x86 boxes, i get Device ID : 32 Device Revision : 1 Firmware Revision : 1.1 IPMI Version : 2.0 Manufacturer ID : 42 Manufacturer Name : Sun Microsystems Product ID ... (7 Replies)
Discussion started by: rethink
7 Replies

7. Shell Programming and Scripting

how to concatenate values of two variables with an underscore(_) in between

Hi, I'm new to shell programming. I have two variables a and b a=val1 b=val2 could anyone kindly post the shell script to concatenate the values of variable a and b with an underscore(_) in between? The final output should be val1_val2. (8 Replies)
Discussion started by: badrimohanty
8 Replies

8. Shell Programming and Scripting

read values from ps -ef into variables in ksh?

Hi, I want to get the first two items returned by ps -ef into two variables? Can anyone please help Thanks (8 Replies)
Discussion started by: JamesByars
8 Replies

9. Shell Programming and Scripting

Parsing and getting values of variables

suppose i have a file value where it returns 3 values a=1 b=2 c=4 when i run it. i am using this file in my shell script. how do i parse and get the value of a b and c? (3 Replies)
Discussion started by: Rekha
3 Replies

10. Shell Programming and Scripting

Problem in assigning values to variables

Hi, I have some problem in assigning values to variables: This is what Iam literally doing: i=0 input=test temp$i = $input In the sense, I try to assign the value in the variable input (ie., test) to another variable temp0 (since i is assigned 0, temp$i is temp0). Seems simple, but I get... (3 Replies)
Discussion started by: mohanprabu
3 Replies
Login or Register to Ask a Question