For loop or while loop from a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop or while loop from a text file
# 1  
Old 02-19-2018
For loop or while loop from a text file

Hi all,

i developed a script to measure the uptime of a process in a Solaris 10/11 environments.
All is well, but i came across a situation where there are multiple processes of the same name. Basically i have the following result file:

Code:
beVWARS 13357 19592122
beVWARS 14329 19591910
beVWARS 6371 20436149
beVWARS 6368 20436149
beVWARS 6374 20436149
beVWARS 13361 19592118
beVWARS 13988 19591989
beVWARS 6367 20436149
beVWARS 6365 20436149
beVWARS 6355 20436149
beVWARS 6363 20436149
beVWARS 14055 19591982
beVWARS 13267 19592147
beVWARS 13562 19592097
beVWARS 6361 20436149
beVWARS 14228 19591944

Where first column is the process name, followed by it's pid and the uptime in seconds.

i am trying to find a way to get the uptime vs. an if condition (by a loop?) where if the uptime is less then say 3600 seconds it outputs the process and it's pid on the same line.

Bascially i tried:

Code:
while read line; do
if [[ $line -lt $CRITICAL_SIZE ]]; then
echo -n `grep $line $CRONOUTFILE | awk '{print$2,$3}'`
fi
done <<< "$TIME"

Where $TIME contains only the 3rd column, i.e. the uptime, but it's not working as it should.
$CRONOUTFILE is the output file as shown above.
What i am trying to achieve is to output only the process and pid in a similar output:

Critical - the following processes $PROCESS are less then $CRITICAL_SIZE - $PID:$TIME, $PID:$TIME...,$PID:$TIME

Where the $PROCESS can be easily set an argument in the script.

I am totally confused in using the for loops or the while loop. May you kindly give me some hints?

Rgds,

Matthew
# 2  
Old 02-19-2018
Code:
PROCESS=beVWARS
CRITICAL_SIZE=3600
CRONOUTFILE=cronoutfile

echo "Critical - the following processes $PROCESS are less then $CRITICAL_SIZE - $(awk '$1== proc && $3 < cs {printf (!c++ ? "" : ",") $2 ":" $3}' proc=$PROCESS cs=$CRITICAL_SIZE $CRONOUTFILE)"

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 02-19-2018
Hi,

Thanks. i understood the flow but can't understand where the error is:

awk: syntax error near line 1
awk: illegal statement near line 1

i added a "}" in

Code:
echo "Critical - the following processes $PROCESS are less then $CRITICAL_SIZE - $(awk '{$1== proc && $3 < cs {printf (!c++ ? "" : ",") $2 ":" $3}' proc=$PROCESS cs=$CRITICAL_SIZE $CRONOUTFILE)"

But still got the same error
# 4  
Old 02-19-2018
If on Solaris, use either nawk or /usr/xpg4/bin/awk
This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 02-19-2018
Thanks, both nawk and /usr/xpg4/bin/awk worked
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with while loop reading every line of a text file

Hello, I'm using RHEL 5.1 with bash. How to handle "read" inside while loop reading every line? Please see below: # cat /tmp/passwd_sample CARRJ12:qVSn4ja4mFA72,..:20021:125:JULIAN CARR:/home/everyone:/bin/bash HERCOT01:NK/3j2ZB4ZC7Q:20022:125:TOM HERCOCK:/home/everyone:/bin/bash... (4 Replies)
Discussion started by: reddyr
4 Replies

2. Shell Programming and Scripting

Loop through the dir and Rename zip files and their underlying text file.

I have files in the ABC_YYYYMMDD.zip format under a directory. Each zip file contains A text file in the ABC_YYYYMMDD.txt format. I am trying to create a script that will Rename the zip files and their underlying text file replacing the datepart in them with . For eg: in the case of... (1 Reply)
Discussion started by: bash987
1 Replies

3. Shell Programming and Scripting

How to loop a text file to remove the old files?

Hi , I'm using Ksh 88 version. I've a text file which contains the directory names DIR1 DIR2 ---- DIR10 I've to remove the data which has more that 30 days from the above directories . The data has to be removed from the following path cd /home/etc/DIR1/hist cd /home/etc/DIR2/hist... (2 Replies)
Discussion started by: smile689
2 Replies

4. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

5. Shell Programming and Scripting

matching user input to a text file loop?

until do read -p "Invalid cars. Try againa" cars1 done Ok i have the above code, im getting users input and if it doesnt match in the file the user has to try again untill its correct But when i run this it gives me an error saying ./Cars.bash: line 43: (2 Replies)
Discussion started by: gangsta
2 Replies

6. UNIX for Advanced & Expert Users

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the values... (12 Replies)
Discussion started by: manishab00
12 Replies

7. Fedora

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the... (1 Reply)
Discussion started by: manishab00
1 Replies

8. Shell Programming and Scripting

while loop to add text to the end of a file

Hi all, I've got 2 files. File 1 has a list say a b c d e f File 2 got start= What I want is to create File 3 which look like this start=a,b,c,d,e,f So is it possible to loop throught File1 to echo it into File3 in one line? (3 Replies)
Discussion started by: stinkefisch
3 Replies

9. Shell Programming and Scripting

Storing the values in text file using while loop in shell script

Hi Frdz while read line do name=`echo $line | cut -d' ' -f 1 ` password=`echo $line | cut -d`-` -f 2` name > logfile.txt password > logfile.txt done < list.txt When it is run, am getting last values in list.txt file only,it is not storing lall the list entry values. How can i... (5 Replies)
Discussion started by: KiranKumarKarre
5 Replies

10. Shell Programming and Scripting

Get data from two text file in one loop

Hi all I have two file that contain different data.I want to get each file data line by line in one loop.i try with cat but cat works ok against one file File a 23 34 45 File b abc xyz cgh now i want like this in a loop.Below is just example.i am doing some thing smiler.can any... (3 Replies)
Discussion started by: aliahsan81
3 Replies
Login or Register to Ask a Question