LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-06-2021, 08:35 PM   #1
TSCollins
LQ Newbie
 
Registered: Apr 2004
Location: Chicago, IL for now
Distribution: Slackware
Posts: 5

Rep: Reputation: 0
Scripting help


Have a multi line file that contains four fields, the first field is a label and the other three are numbers. I need to find the percentage difference between field two and four and if it is greater then 90% print the label and if it is less then 90% ignore it and move onto the next line.
 
Old 05-06-2021, 09:26 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,611
Blog Entries: 3

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
That sounds like an easy job for AWK. What have you tried so far, where are you stuck? Also, please show a few lines of sample input and a few lines of expected output.
 
Old 05-07-2021, 07:14 AM   #3
TSCollins
LQ Newbie
 
Registered: Apr 2004
Location: Chicago, IL for now
Distribution: Slackware
Posts: 5

Original Poster
Rep: Reputation: 0
Actually the source file has more then four fields but I only am interested in four of the fields, here are a few sample lines from the source file:

Name Id RootInode ParentId Created InodeSpace MaxInodes AllocInodes UsedInodes
root 0 3 -- Fri Aug 18 11:53:03 2017 0 100000000 1528320 4187
home 1 524291 0 Fri Aug 18 12:06:10 2017 1 100000000 22502400 5828699
apps 5 2621443 0 Fri Aug 18 14:38:12 2017 5 50000128 17380352 11488908

I have been able to extract the four that I want to work with (Name, MaxInodes, AllocInodes & UsedInodes) like this:

cat inode.txt | awk '{print $1,$11,$12,$13}'
root 100000000 1528320 4187
home 100000000 22502400 5828699
apps 50000128 17380352 11488908

The problem I am having is with keeping the name in sync with the numbers then doing the calculation and determining if it is > or < 90%. Here is what I have so far in the script:

#!/bin/bash
# check inodes of filesets
declare THRESHOLD=90
Key=`cat inode.txt | sed "1d" | awk '{print $1,$11,$12,$13}'`
for K in $Key
do
echo $K
done

When I run the script I get the following output:

root
100000000
1528320
4187
home
100000000
22502400
5828699
apps
50000128
17380352
11488908

I have tried setting different variables like this:

Name=`cat inode.txt | awk '{print $1}'`
MaxInodes=`cat inode.txt | awk '{print $11}'`
AllocInodes=`cat inode.txt | awk '{print $12}'`
UsedInodes=`cat inode.txt | awk '{print $13}'`

for N in $Name
for M in $MaxInodes
for A in $AllocInodes
for U in $UsedInodes

and then thought something like

do
echo $N
echo $U / $A \* 100 | bc -l
if (( ${above_result} >= ${THRESHOLD} )) ; then
echo "fileset $N is close to allocated inodes"
else
echo "fileset $N is ok"
fi
done

The first problem I of course am having is the multiple 'for' statements. I then tried nesting them but then I just get the first value for $N i.e. root with all the values for say $U i.e. 4187, 5828699 & 11488908.

Last edited by TSCollins; 05-07-2021 at 07:16 AM.
 
Old 05-07-2021, 07:33 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 2,321

Rep: Reputation: Disabled
Well, you don't have any filesystem with more than 90% inodes used, so here it is with 66%:
Code:
awk 'NR>1&&$13/$12>0.66,$0=$1' inode.txt
 
Old 05-07-2021, 07:34 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,611
Blog Entries: 3

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
AWK can do much more, so Bash is not needed or desired probably:

Code:
awk '$13 && $13/$12>=0.9 {
        print "Fileset " $1 " is close to allocate inodes"
        next
        }
$13 {
        print "Fileset " $1 " is OK" 
}' inodes.txt

Last edited by Turbocapitalist; 05-07-2021 at 07:36 AM.
 
Old 05-08-2021, 04:36 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,741

Rep: Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790
Quote:
Key=`cat inode.txt | sed "1d" | awk '{print $1,$11,$12,$13}'`
This retains the spaces and newlines, but
Quote:
for K in $Key
splits into words according to $IFS.
You can improve it by modifying IFS to only a newline
Code:
oIFS=$IFS
IFS="
"
and later restore IFS with
Code:
IFS=$oIFS
But better use a while-read loop where the read defaults to a newline, and can assign fields to variables.
Code:
awk 'NR>1 {print $1,$11,$12,$13}' inode.txt |
while read Name MaxInodes AllocInodes UsedInodes
do
  echo "do something with $MaxInodes and $UsedInodes e.g. $((UsedInodes * 100 / AllocInodes)) and print along with $Name" 
done
The pipe might force the while loop into a subshell so you cannot set variables in the main shell.
The following while loop runs in the main shell:
Code:
while read Name MaxInodes AllocInodes UsedInodes
do
  echo "do something with $MaxInodes and $UsedInodes $((UsedInodes * 100 / AllocInodes)) and print along with $Name" 
done < <(awk 'NR>1 {print $1,$11,$12,$13}' inode.txt)
This construct is called a "process substitution".
 
Old 05-08-2021, 04:48 AM   #7
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,247
Blog Entries: 1

Rep: Reputation: Disabled
First drop any unneeded fields. Just filter file and create new with only these fields required. Do by hand a little - look at coreutils - and I am sure you will solution. Bash automates tasks we can do by hands - but it is too tedious, error prone, or data are too large. But reworking small sample should definitely clarify situation. I am not posting ready solution - task is really very easy to do.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Crash Course in Python Scripting needed - helper must know python scripting and about audio files ending in m4a BW-userx Programming 4 04-07-2017 01:55 PM
LXer: Shell Scripting Part I: Getting started with bash scripting LXer Syndicated Linux News 0 04-29-2015 08:03 AM
LXer: Scripting the Linux desktop, Part 2: Scripting Nautilus LXer Syndicated Linux News 0 02-17-2011 04:02 AM
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 10:09 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration