printing positional charaters in a loop

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printing positional charaters in a loop
# 1  
Old 09-06-2009
printing positional charaters in a loop

usep=`df -hT | awk '{ print $5 }'`
for (1=1,1<8,i++)
output=`echo $usep | awk '{ print $i }'| cut -d'%' -f1`
echo $output
if [ $output -gt 10 ]
echo "critical value"

i need to echo critical value if disk usage pecentage xceeds 10

and i am face problem in position marked red here i am trying to print i where i represent postional parameter from 1to8 but i am not able to print
but i am able to get output if i am directly useing postional parameters instead of i ....

please suggest accordingly

Thanks in Advance


Last edited by bakunin; 09-06-2009 at 02:48 PM.. Reason: pursued my favourite pastime: giving code-tags to the needy
# 2  
Old 09-06-2009
Originally Posted by josgeorge
i need to echo critical value if disk usage pecentage xceeds 10
$6 is the "Use %" column in my system's output.

df -hT | awk '{x=$6; sub(/%/,"",x); if (x > 10){print $0,"\t <= critical value"}}'

# 3  
Old 09-06-2009
Not tested, -T don't work on my system.
df -hT | awk 'int($5) > 10{print $0,"\t <= critical value"}'

# 4  
Old 09-06-2009
Originally Posted by danmero
Not tested, -T don't work on my system.
df -hT | awk 'int($5) > 10{print $0,"\t <= critical value"}'

It works, $5 just needs to changed to $6 :
df -hT | awk 'int($6) > 10 {print $0,"\t <= critical value"}'

Although I don't see why disk usage of more than 10% should be considered critical. Perhaps the OP means 90% ?

Last edited by Scrutinizer; 09-06-2009 at 01:10 PM..
# 5  
Old 09-06-2009
I think we need to know which Operating System and version. There is much variety in the "df" command.
For this purpose, sample input (from df -Ht) and sample expected output would do.

Last edited by methyl; 09-06-2009 at 02:44 PM..
# 6  
Old 09-06-2009
If you just want to filter out a positional part of the output you do not need awk at all:

read var1 var2 var3 ...

will break down the output and assign word 1 to var1, word 2 to var2 and so on. If there are more words than variables the last variable will get the remainder of the line. You can use a dummy variable over and over again to filter out what you are not interested in:

example (filter out the size only):

df -hT | tail -1 | read junk junk fssize junk

Your code could be simplified to (adjust the number of "junk"s to adapt to your systems output):

df -hT | while read fs_name junk junk junk junk fs_used_percent junk ; do
     if [ $fs_used_percent -gt 10 ] ; then
         echo "Warning: $fs_name usage above threshold"

I hope this helps.


Last edited by bakunin; 09-06-2009 at 02:54 PM..
# 7  
Old 09-06-2009
Bi bakunin. No offence but the output from many versions of "df -k" contains a "%" sign suffixed to the value. O/P jossgeorge script suggests that this is one of those versions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing the output of sed using a loop

So I am writing a bash script that will search a file line by line for unix timestamps, store all of the timestamps into an array, then check how many of those timestamps were created within the last hour, and finally increment a counter every time it finds a timestamp created within the last hour.... (6 Replies)
Discussion started by: jsikarin
6 Replies

2. Shell Programming and Scripting

Use positional parameters in loop / while syntax in whiptail

I like to “optimize” / make more like a real program my bash script by replacing repetitious code which utilizes positional parameters. I am having two issues I cannot solve and would appreciate some assistance with resolving them. a) how to modify the whiptail checklist... (3 Replies)
Discussion started by: annacreek
3 Replies

3. Shell Programming and Scripting

Array not printing values if used in a loop

Hello! I'm making an English to Morse Code translator and I was able to mostly get it all working by looking through older posts here; however, I have one small problem. When I run it it's just printing spaces for where the characters should be. It runs the right amount of times, and if I try... (3 Replies)
Discussion started by: arcoleman10
3 Replies

4. Shell Programming and Scripting

Awk: printing column using for loop

Hello: I've input data: Input data --- 3:60069:C:T 60069 C T 1 0 0 1 0 0 1 0 0 1 0 0 1 --- 3:60079:A:G 60079 A G 1 0 0 0.988 0.012 0 1 0 0 1 0 0 1 --- rs186476240:60157:G:A 60157 G A 1 0 0 1 0 0 1 0 0 1 0 0 1 I edit/make first few columns before numbers (6th column) and want to... (4 Replies)
Discussion started by: genome
4 Replies

5. Shell Programming and Scripting

Filter ONLY lines with non-printing charaters

I have a file contains data with non-printing characters. i have used cat -v filename to display whole data with non-printing characters also. However, i need lines with non-printing characters into seperate file. My file is huge and looks like i have to manully find lines using cat -v filename |... (3 Replies)
Discussion started by: JSKOBS
3 Replies

6. Shell Programming and Scripting

For Loop in shellscript - Printing Output for every iteration

for VGLIST in `lsvg -o` do CLOSED_OUT=`echo $VGLIST | lsvg -l $VGLIST | awk '{print $6 " " $7}' | grep closed` if ]; then echo "Filesystems $CLOSED_OUT in VG that are in Closed status" else echo "\n Some message" fi Above Code is working fine, but echo "Filesystems $CLOSED_OUT... (8 Replies)
Discussion started by: chandu123
8 Replies

7. Shell Programming and Scripting

Awk while-loop printing extra character

Hi, I'm using a while-loop in an awk script. If it matches a regular expression, it prints a line. Unfortunately, each line that is printed in this loop is followed by an extra character, "1". While-statement extracted from my script: getline temp; while (temp ~ /.* x .*/) print temp... (3 Replies)
Discussion started by: redbluefish
3 Replies

8. Shell Programming and Scripting

printing with awk through while loop

ive input file contains to clums a and b spreated by pipe a | b 123|456 323|455 and xyz contains other info about a and b now i want to print as follows: a | b | "info from xyz" but "info from xyz" might be more than 1 line and i want to keep the format to 3 cloums. how to do it?... (3 Replies)
Discussion started by: windows
3 Replies

9. UNIX for Dummies Questions & Answers

Silly question on printing for loop in AWK

Hi, One silly question. I would like to add statement like below and append to a file. I used the below code; however, it does not work. Can anyone please tell me what mistakes I have made? awk ' { for (i=1;i<=563;i++) print i }'>>output.txt Thanks. -Jason (1 Reply)
Discussion started by: ahjiefreak
1 Replies

10. UNIX for Dummies Questions & Answers

Finding funny charaters in a string

Hi, I have got a little problem where some of the files I created had some funny characters e.g space ,"^M" etc inserted in the full file name and which caused our backup script to skip those files. Is it possible to find out if there are any funny charaters in a string ? Thanks, (4 Replies)
Discussion started by: ahjaeasedqa
4 Replies
Login or Register to Ask a Question