Testing grep output - shell

The cmd:
STATUS=`grep word a.log | tail -1 | awk '{print $1,$2,$7,$8,$9}'`
echo "$STATUS"
The output:
2020-05-18 09:27:01 1 of 122
I need to display this $STATUS and need to do the test comparison as well.
How to compare number 122 in below? How to represent 122 in $X?
The number 122 can be any number, resulted from above cmd.
if [ "$X" -gt "300" ]
then
echo "$STATUS. This in HIGH queue ($X)"
else
echo "$STATUS. This is NORMAL ($X)"
fi

You could do it with one awk script:
awk '
/word/{ status=$1" "$2" "$7" "$8" "$9; x=$9 }
END{ printf status". This %s (%s)\n", (x>300 ? "in HIGH queue" : "is NORMAL"), x }
' a.log

I would suggest using lowercase for variables to reduce possible confusion for someone other than the original author reading the script in the future. Also using $() is typically preferable to using back-ticks -- makes quoting easier to get right.
status="$(grep word a.log | tail -1 | awk '{print $1,$2,$7,$8,$9}')"
x="$(printf '%s' "$status" | awk '{ print $NF }')"
if [ "$x" -gt 300 ]
then
echo "$status. This in HIGH queue ($x)"
else
echo "$status. This is NORMAL ($x)"
fi
Note -- we could refactor the status line a bit:
status="$(awk '/word/ { x = $1 OFS $2 OFS $7 OFS $8 OFS $9 } END { print x }' a.log)"

Related

Would it be possible to print the file used to redirect STDERR?

Would it be possible to print the filename used to redirect STDERR, given the sample command below:
command.sh 2>file.err
Code in command.sh:
#!/bin/sh
ls -l non_existing_file.txt
echo "STDERR file is: $stderrFilename" # variable should print file.err
It's a little risky, but you could try parsing AIX's procfiles output. It involves capturing the major and minor numbers of the stderr device, along with the inode number, then looking for the corresponding device, its mountpoint, and then using find to look for the file with the given inode number:
#!/bin/sh
dev=$(procfiles $$ | awk '$1 == "2:" { print substr($4, 5) }')
inode=$(procfiles $$ | awk '$1 == "2:" { print substr($5, 5) }')
major=${dev%%,*}
minor=${dev##*,}
if [ "$major}" -eq 0 ]
then
echo I give up, the major number is zero
exit 1
fi
for file in /dev/*
do
[ -b "$file" ] || continue
if istat "$file" | grep -q "^Major Device ${major}.*Minor Device ${minor}$"
then
break
fi
done
fs=$(mount | awk '$1 == "'"${file}"'" { print $2 }')
stderrFilename=$(find "$fs" -inum "$inode")
I made a solution using history. Not sure if there is an easier way to do this ( or a proper one).
#!/bin/sh
stderrfname=`history | tail -1 | awk '{ print $3 }' | sed "s/.*>//"`
echo "STDERR file is: $stderrfname"

Argument not recognised/accesed by egrep - Shell

Egrep and Awk to output columns of a line , with a specific value for the first column
I am to tasked to write a shell program which when ran as such
./tool.sh -f file -id id OR ./tool.sh -id id -f file
must output the name surname and birthdate (3 columns of the file ) for that specific id.
So far my code is structured as such :
elif [ "$#" -eq 4 ];
then
while [ "$1" != "" ];
do
case $1 in
-f)
cat < "$2" | egrep '"$4"' | awk ' {print $3 "\t" $2 "\t" $5}'
shift 4
;;
-id)
cat < "$4" | egrep '"$2"' | awk ' {print $3 "\t" $2 "\t" $5}'
shift 4
esac
done
(Ignoring the opening elif cause there are more subtasks for later)
My output is nothing. The program just runs.
I've tested the cat < people.dat | egrep '125' | awk ' {print $3 "\t" $2 "\t" $5}'
and it runs just fine.
I also had an instance where i had an output from the program while it was run like so
cat < "$2" | egrep '["$4"]' | awk ' {print $3 "\t" $2 "\t" $5}'
but it wasnt only that specific ID.
`egrep "$4"` was correct instead of `egrep '["$4"]'` in
`cat < "$2" | egrep '["$4"]' | awk ' {print $3 "\t" $2 "\t" $5}'`
Double quotes allow variables, single quotes don't. No commands need
certain types of quotes, they are purely a shell feature that are not
passed to the command. mentioned by(#that other guy)

Removing columns from a csv file with different numbers of columns per line

I have this bash script to remove columns from lines of a given csv file, but it runs very slowly. I need to use this script for files larger than 1GB, so I'm looking for a faster solution.
#!/bin/bash
while read line; do
columns=`echo $line | awk '{print NF}' FS=,`
if [ "$columns" == "9" ]; then
echo `echo $line | cut -d \, -f 1,5,6,8,9`
elif [ "$columns" == "24" ]; then
echo `echo $line | cut -d \, -f 1,5,6,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24`
elif [ "$columns" == "8" ]; then
echo `echo $line | cut -d \, -f 1,4,5,6,7,8`
else
echo $line
fi
done <$1
If anyone has advice on how to speed this up or if theres a better way to do it, that'd be awesome. Thanks a lot!
Your entire script can be handled by a single awk.
Try this:
awk 'BEGIN{FS=OFS=","}
NF==9 {print $1, $5, $6, $8, $9; next}
NF==8 {print $1, $4, $5, $6, $8; next}
NF==24{print $1,$4,$5,$6,$8,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24} "$1"

Of Bash loops and if statements

I need to look at a line, and perform a quick if/then->echo on it depending on the content of column 3.
The file looks like this:
name network subnetmask
net_A 192.168.0.0 24
net_b 10.10.0.0 16
Some columns also have a blank 3rd column, and I need to have an if/then for those as well.
Psuedo-code should look like this in my mind:
snet_mask=`cat $filename | grep -i net | awk '{print $3}`
if [ $snet_mask = 24 ]
then
awk '{print "something"$1,"something else"}'
fi
if [ $snet_mask = 23 ]
then
awk '{print "something"$1,"something else"}'
fi
etc
That just doesn't work it seems, since $snet_mask becomes the value of "all" of $3, so I think I need a for loop based on grep -i net, however I don't really know.
What's the right answer? :)
Try this one-liner :
awk '$1 ~ "^net" && $3==24{print "something", $3, "something else"} $1 ~ "^net" $3==23{print "something", $3, "something else"}' file.txt
Or on multi-lines (easier to read) :
awk '
$1 ~ "^net" && $3==24{print "something", $3, "something else"}
$1 ~ "^net" && $3==23{print "something", $3, "something else"}
' file.txt
We can do it simply like this too (depends of your needs) :
awk '
$1 ~ "^net" && ($3==24 || $3==23) {print "something", $3, "something else"}
' file.txt
Or even simpler & shortest with a regex :
awk '
$1 ~ "net" && $3 ~ "^2[34]$" {print "something", $3, "something else"}
' file.txt
you could accomplish what you need in an awk statement, since you're already using awk
cat $filename | grep -i net | awk '{if($3==24) print $1; else print $0;}'
In the if statement (if 3rd col is 23), I'm printing just the first column, otherwise I'm printing everything. Obviously you can expand this to work with all of your cases
Staying in bash without external tools, you could do something like this:
while read name network netmask ; do
if [[ "$name" == net* ]] ; then
case "$netmask" in
"") echo "It's empty" ;;
24) echo "It's 24" ;;
23) echo "It's 23" ;;
*) echo "None of the above" ;;
esac
fi
done < "$filename"

Length of a specific field, and showing the record in much easier way

My goal is to find out the length of the second field and if the length is more than five characters, then I need to show the entire record using shell scripts/command.
echo "From the csv file"
cat latency.csv |
while read line
do
latency=`echo $line | cut -d"," -f2 | tr -d " "`
length=$(echo ${#latency})
if [ $length -gt 5 ]
then
echo $line
fi
done
There is nothing wrong with my code, but being UNIX/Linux, I thought there should be a simpler way of doing such things.
Is there one such simpler method?
awk -F, 'length($2)>5' file
this should work
updated
awk -F, '{a=$0;gsub(/ /,"",$2);if(length($2)>5)print a}' file
awk -F, '{
t = $2
gsub(/ /, x, t)
if (length(t) > 5)
print
}' latency.csv
Or:
perl -F, -ane'
print if
$F[1] =~ tr/ //dc > 5
' latency.csv

Resources