While loop with awk issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop with awk issue
# 1  
Old 11-08-2011
While loop with awk issue

Hi folks.

I am trying to use a while loop along with awk to get the colums where 2 specific words are found.

Here is the output of the command i run where i need to pull the column numbers if specific words are found:

Code:
State    Type    Rebal  Sector  Block       AU  Total_MB  Free_MB  Req_mir_free_MB  Usable_file_MB  Offline_disks  Voting_files  Name
MOUNTED  EXTERN  N         512   4096  1048576   1049996  1048065                0         1048065              0             N  DATA/

What i am trying to do, is have the loop "check" each columns until it finds Total_MB and set a TOTALCOL=7 (as this is the 7th column) and then continue through its loop until it finds Free_MB and set a FREECOL=8 variable that i can use later in the script...When the loop reaches then end, i was guessing an empty return, it would exit the loop and continue...

Here is what i have so for, but it does not work as expected. I may be completely off too! Smilie Note that the ultimate goal is to extract the Total and free space in 2 separate variables. If that can be done during the loop, then even better...

Code:
# Query ASM through lsdg for Free_MB and Total_MB columns

i=1

while [[ $QUERYLSDG != " " ]]
do

QUERYLSDG=$(asmcmd lsdg | awk '{print $i}')

echo $QUERYLSDG

case $QUERYLSDG in
       Total_MB)       TOTALCOL=$i;;
       Free_MB)        FREECOL=$i;;
esac

i=i+1
done

echo $TOTALCOL
echo $FREECOL

# 2  
Old 11-08-2011
I'm not sure if this is what you are looking for

if you are very well aware of that Total_MB & Free_MB are 7th and 8th position in your output then try to store those values in a file let say space_details.txt and then execute below AWK.

Code:
State    Type    Rebal  Sector  Block       AU  Total_MB  Free_MB  Req_mir_free_MB  Usable_file_MB  Offline_disks  Voting_files  Name
MOUNTED  EXTERN  N         512   4096  1048576   1049996  1048065                0         1048065              0             N  DATA/
MOUNTED  EXTERN  N         512   4096  1048576   1049996  1048065                0         1048065              0             N  DATA/

then
try this out
Code:
awk '{total_mb +=$7 ; total_free +=$8}  END { print "total_mb="total_mb"& total free="total_free}' space_details.txt

# 3  
Old 11-08-2011
If the two fields positions are unknown, this awk expression may help. It'll step through the fields and when it finds the matching field prints out the number of that field:

Code:
awk '/Total_MB/{ for(i=1;i<=NF;++i) if($i ~ /Total_MB/) print i }' file.txt
7

awk '/Free_MB/{ for(i=1;i<=NF;++i) if($i ~ /Free_MB/) print i }' file.txt
8

Hope this helps.
# 4  
Old 11-08-2011
Quote:
Originally Posted by manas_ranjan
I'm not sure if this is what you are looking for

if you are very well aware of that Total_MB & Free_MB are 7th and 8th position in your output then try to store those values in a file let say space_details.txt and then execute below AWK.

Code:
State    Type    Rebal  Sector  Block       AU  Total_MB  Free_MB  Req_mir_free_MB  Usable_file_MB  Offline_disks  Voting_files  Name
MOUNTED  EXTERN  N         512   4096  1048576   1049996  1048065                0         1048065              0             N  DATA/
MOUNTED  EXTERN  N         512   4096  1048576   1049996  1048065                0         1048065              0             N  DATA/

then
try this out
Code:
awk '{total_mb +=$7 ; total_free +=$8}  END { print "total_mb="total_mb"& total free="total_free}' space_details.txt

Thanks for the answer, but the issue is that the script is to be used on multiple servers which have different versions of the asm commands. The output differs (column numbers) from one version to another...Hence why i was looking for a way to "find" the matching columns first, then do the queries...

---------- Post updated at 10:42 AM ---------- Previous update was at 10:40 AM ----------

Quote:
Originally Posted by in2nix4life
If the two fields positions are unknown, this awk expression may help. It'll step through the fields and when it finds the matching field prints out the number of that field:

Code:
awk '/Total_MB/{ for(i=1;i<=NF;++i) if($i ~ /Total_MB/) print i }' file.txt
7

awk '/Free_MB/{ for(i=1;i<=NF;++i) if($i ~ /Free_MB/) print i }' file.txt
8

Hope this helps.
Thanks, that may do the trick. I'll try it...

---------- Post updated at 12:47 PM ---------- Previous update was at 10:42 AM ----------

Quote:
Originally Posted by in2nix4life
If the two fields positions are unknown, this awk expression may help. It'll step through the fields and when it finds the matching field prints out the number of that field:

Code:
awk '/Total_MB/{ for(i=1;i<=NF;++i) if($i ~ /Total_MB/) print i }' file.txt
7

awk '/Free_MB/{ for(i=1;i<=NF;++i) if($i ~ /Free_MB/) print i }' file.txt
8

Hope this helps.
Hi.

Here is the script modified. I removed the loop as it is not necessary and tweaked your lines to reflect my needs...

I get a weird output though, that includes the "=" sign? (i did an echo just to see what was discovered through the awk)

Code:
$TOTALCOL=$(asmcmd lsdg | awk '/Total_MB/{ for(i=1;i<=NF;++i) if($i ~ /Total_MB/) print i }')
$FREECOL=$(asmcmd lsdg | awk '/Free_MB/{ for(i=1;i<=NF;++i) if($i ~ /Free_MB/) print i }')

echo $TOTALCOL
echo $FREECOL

output:

Code:
[root@servera ora]# /usr/local/bin/test.sh

Checking variables for servera...

/usr/local/bin/ora/test.sh[124]: =7: not found [No such file or directory]
/usr/local/bin/ora/test.sh[125]: =8: not found [No such file or directory]

line 124 and 125 are "awk" lines...

---------- Post updated at 12:50 PM ---------- Previous update was at 12:47 PM ----------

UPDATE! Nevermind...

Just saw my error...

Treated the "awk" line as a variable to start with

$TOTALCOL=blahblahblah

should be TOTALCOL=$(...

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue in awk parsing under while loop

Hi I am trying to parse a grep output using awk. It works fine individually and not working under the loop with variable name assigned. cat > file.txt dict=/dictr/abcd/d1/wq:/dictr/abcd/d2/wq:/dictr/abcd/d3/wq: sample tried code Nos=`grep -w "dict" file.txt | awk -F"=" '{print... (10 Replies)
Discussion started by: ananan
10 Replies

2. Shell Programming and Scripting

Issue with for loop

Hi Team, I have for loop in my shell script. Which basically loop through all files in the directory, When some files are in the directory it works just fine. But if there are no files at all..still the for loop try to execute. Please help. Below is the code. #!/bin/ksh echo "Program... (5 Replies)
Discussion started by: bharath561989
5 Replies

3. Shell Programming and Scripting

awk - 2 files comparison without for loop - multi-line issue

Greetings Experts, I need to handle the views created over monthly retention tables for which every new table in YYYYMMDD format, there is equivalent view created and the older table which might be dropped, the view over it has to be re-created over a dummy table so that it doesn't fail.... (2 Replies)
Discussion started by: chill3chee
2 Replies

4. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

5. Shell Programming and Scripting

While Loop issue

Hi, i=0 t5=6000001 while do i=`expr $i + 1` t5=`expr $t5 + 1` echo $t5 done I am able to increment "col3" value but unable to get col1,col2 value. Input: t1=10001 t2=abc t3=ghkc (5 Replies)
Discussion started by: onesuri
5 Replies

6. Shell Programming and Scripting

For loop, awk command issue

limit.csv data -------------- 5600050 38Nhava 400077 27Bomay rate.txt data ------------- 38NhaVA 27BomaY 27Bomay below is my script: for i in `cat limit.csv` do b=`awk '{print $1}' $i` (4 Replies)
Discussion started by: p_satyambabu
4 Replies

7. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

8. Shell Programming and Scripting

awk loop issue

Hi I am not able to solve this minor issue even after a lot of trial! Will be thankful if you can help me out. This is part of a awk script and the loop is self-explanatory - num_null_key=0 #for counting the number of null keys num_non_null_key=0 #for counting the number of non null... (2 Replies)
Discussion started by: nsinha
2 Replies

9. Shell Programming and Scripting

Issue with using While loop

Hi, I am trying to move a file from remote server to local server and when the transfer completes successfully i call a script in remote server to remove the file which was successfully transferred. I do this by first getting the list of file in remote server and move the text file to local... (8 Replies)
Discussion started by: funonnet
8 Replies

10. Shell Programming and Scripting

for-while loop issue

sup experts..i had a script which was bugging me..was hoping someone could point out the issue here Input file: space separated 2 columns I wanted to print out the 2 columns after assigning them to variables ( bascially the same output but iterate through line by line ). The code worked... (7 Replies)
Discussion started by: foal_newbie
7 Replies
Login or Register to Ask a Question