bash script that reads a txt file,stores the numbers within

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jocamcoliveira
Posts: 2
Joined: 08 Nov 2013 07:10

bash script that reads a txt file,stores the numbers within

#1 Post by jocamcoliveira » 08 Nov 2013 07:14

I have a text file with the following information:

Filesystem Use%
/dev/sda1 44%
/dev/sda7 35%
/dev/sda3 2%
/dev/sda2 5%
/dev/sda5 47%
tmpfs 0%

Now, I want to make a batch file that reads this text file, store the numbers of the lines 2,3,4,5 e 6 into some variables and then compare these numbers with a specific value set by me. The comparison would be something like this:

variable = 44
if variable > 90
then it presents a console message whith the all the line of the variable stored.

variabletwo =35
if variabletwo > 90
then it presents a console message whith the all the line of the variable stored.

and so on...

Can someone help me please?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: bash script that reads a txt file,stores the numbers wit

#2 Post by foxidrive » 08 Nov 2013 10:52

I'm not sure you replied on StackOverflow but I didn't get an alert.

This will currently report the lines that exceed 30.

Code: Select all

@echo off
for /f "delims=%%" %%a in ('find "/" ^< "file.txt" ') do (
for /f "tokens=1,2" %%b in ("%%a") do (
if %%c GTR 30 echo %%a%%
)
)
pause

jocamcoliveira
Posts: 2
Joined: 08 Nov 2013 07:10

Re: bash script that reads a txt file,stores the numbers wit

#3 Post by jocamcoliveira » 08 Nov 2013 11:46

Hi foxidrive,thanks in advance.

I tested your code and it works :) But, can you explain me what it does the following part:


'find "/" ^< "disk.txt" ' -> I understand that you search for the first "/" but then what do you do? do you add a % character?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: bash script that reads a txt file,stores the numbers wit

#4 Post by foxidrive » 08 Nov 2013 23:08

The top for loop removes the percent sign, and filters for all lines that contain a / character anywhere in the line.
The second for loop takes each line in turn, and separates the first term and the number (using space as a default delimiter) and then compares the number.

If it matches then the echo displays the content of %%a and adds a % sign at the end (which needs two %% to create a % sign).

Post Reply