Awk: Print specific value range - bash

Say i have:
> id|lastName|firstName|gender|birthday|creationDate|locationIP|browserUsed
>
> 13194139534963|Berty|Jean|male|1988-08-02|2012-04-02T08:33:15.012+0000|41.216.190.153|Google
> Chrome
>
> 13194139535544|Oliveira|Manuel|male|1984-10-31|2012-03-14T16:00:12.287+0000|109.71.166.230|Internet
> Explorer
>
> 13194139537327|Wei|Lei|male|1987-01-06|2012-03-13T03:07:51.899+0000|27.99.188.150|Internet
> Explorer
>
> 13194139539118|Alvarez|Monica|male|1989-10-17|2012-02-25T19:18:54.137+0000|190.169.213.242|Internet
> Explorer
>
> 13194139539746|Xu|Wei|female|1986-11-30|2012-03-19T23:16:12.495+0000|27.103.77.193|Firefox
I want to make a command with those parameters : ./tool.sh --born-since dateA --born-until dateB -f file
1)If born-since and born-until dates are given i want to print all those born(the whole line)between two specific dates ( Year-Month-Date )
Example
./tool.sh --born-since 1988-08-02 --born-until 2012-09-13 -f file
Output:
13194139534963|Berty|Jean|male|1988-08-02|2012-04-02T08:33:15.012+0000|41.216.190.153|Google
13194139539118|Alvarez|Monica|male|1989-10-17|2012-02-25T19:18:54.137+0000|190.169.213.242|Internet
Explorer
2)If only born-since date is given i want to list all the people(whole line) with born dates of that and after.
Example:
./tool.sh --born-since 1988-08-02 -f file
Output:
Same as 1)
3)If only born-until date is given i want to list all the people born until that date(again the whole line about them).
./tool.sh --born-until 1988-08-02 -f file
Output:
13194139535544|Oliveira|Manuel|male|1984-10-31|2012-03-14T16:00:12.287+0000|109.71.166.230|Internet Explorer
13194139537327|Wei|Lei|male|1987-01-06|2012-03-13T03:07:51.899+0000|27.99.188.150|Internet Explorer
13194139539746|Xu|Wei|female|1986-11-30|2012-03-19T23:16:12.495+0000|27.103.77.193|Firefox
My code is :
while [ $# -gt 0 ];do #Get and store Dates (Since-Until)
if [ "$1" = --born-since ];then
if [[ "$2" =~ $re ]];then #re='[0-9]-*' # Check if $2 is number
BSDate=$2
BSYear=$(echo "$BSDate" | awk -F '-' '{print $1}') # Get BSYear
BSMonth=$(echo "$BSDate" | awk -F '-' '{print $2}') # Get BSMonth
BSDay=$(echo "$BSDate" | awk -F '-' '{print $3}') # Get BSDay
fi
elif [ "$1" = --born-until ];then
if [[ "$2" =~ $re ]];then
BUDate=$2
BUYear=$(echo "$BUDate" | awk -F '-' '{print $1}') # Get BUYear
BUMonth=$(echo "$BUDate" | awk -F '-' '{print $2}') # Get BUMonth
BUDay=$(echo "$BUDate" | awk -F '-' '{print $3}') # Get BUDay
fi
fi
shift
done
if [ "$BSDate" ] && [ "$BUDate" ];then #If both date arguments exist
elif [ "$BSDate" ];then
elif [ "$BUDate" ];then
fi
If i enter --born-since 1998-10-30 the arguments get passed correctly for evaluation in awk , 1998 = BSYear , 10 = BSMonth , 30 = BSDay. Can someone help me implement the awk part ?

For the awk part :
cat ./tool.sh
awk -F'|' -vs="$1" -ve="$2" '
BEGIN{if(!s)s="0000-00-00";if(!e)e="9999-99-99"}
NR>1 && $5>=s && $5<=e' infile
And you call it like that
./tool.sh '1987-01-06' '1988-08-02'
or
./tool.sh '' '1988-08-02'
or
./tool.sh '1987-01-06' ''

I fixed it with : awk -F'|' '{if ($5 >= "'$BSDate'" && $5 <= "'$BUDate'")

Related

check if a file contains two variables in a line

I am trying to check the values of my variables are exist in the file or not using if condition in Bash.
I tried is as follows, but for all the values I am getting value false.
a=-127.5256
b=24.5632
file=test.txt
-54.2565 58.9685
-127.2568 12.5890
-127.5256 24.5632
-78.9865 35.2366
I tried as follow but not working in my case:
if grep -Fxq "($a $b | bc)" $file; then
echo True
else
echo False
Is there any other way to perform the above job?
Thank you
I would prefer to use AWK.
awk -v a=-127.5256 -v b=24.5632 '{if ($1==a && $2=b) print "True"}' < input file
using bash
while IFS=" " read -r f1 f2;
do
if [ "$a" == "$f1" ] && [ "$b" == "$f2" ]
then
echo "True"
fi
done < input file
Demo:
$cat test.txt
-54.2565 58.9685
-127.2568 12.5890
-127.5256 24.5632
-78.9865 35.2366
$awk -v a=-127.5256 -v b=24.5632 '{if ($1==a && $2=b) print "True"}' test.txt
True
$echo $a $b
-127.5256 24.5632
$while IFS=" " read -r f1 f2;
> do
> if [ "$a" == "$f1" ] && [ "$b" == "$f2" ]
> then
> echo "True"
> fi
> done < test.txt
True
$

shell script : comma in the beginning instead of end

This is a part of my shell script.
for line in `cat $1`
do
startNum=`echo $line | awk -F "," '{print $1}'`
endNum=`echo $line | awk -F "," '{print $2}'`
operator=`echo $line | awk -F "," '{print $3}'`
termPrefix=`echo $line | awk -F "," '{print $4}'`
if [[ "$endNum" == 81* ]] || [[ "$endNum" == 33* ]] || [[ "$endNum" == 55* ]]
then
areaCode="${endNum:0:2}"
series="${endNum:2:4}"
startCLI="${startNum:6:4}"
endCLI="${endNum:6:4}"
else
areaCode="${endNum:0:3}"
series="${endNum:3:3}"
startCLI="${startNum:6:4}"
endCLI="${endNum:6:4}"
fi
echo "Add,${areaCode},${series},${startCLI},${endCLI},${termPrefix},"
#>> ${File}
done
input is csv contains below many rows :
5557017101,5557017101,102,1694
5515585614,5515585614,102,084
Output od shell script :
,dd,55,5701,7101,7101,1694
,dd,55,1558,5614,5614,0848
Not sure why comma is coming in startign of output, instead as per shell script it should come in the end.
please help
Here is a suggested awk command that should replace all of your shell+awk code. This awk also takes care of trailing \r:
awk -v RS=$'\r' 'BEGIN{FS=OFS=","} NF>3{
startNum=$1; endNum=$2; termPrefix=$4;
if (endNum ~ /^(81|33|55)/) {
areaCode=substr(endNum,1,2); series=substr(endNum,3,4)
}
else {
areaCode=substr(endNum,1,3); series=substr(endNum,4,3)
}
startCLI=substr(startNum,7,4); endCLI=substr(endNum,7,4);
print "Add", areaCode, series, startCLI, endCLI, termPrefix
}' file
Add,55,5701,7101,7101,1694
Add,55,1558,8561,5614,084

Unix - How do I have my shell script process more than one file from the command line?

I'm trying to modify an existing script I have to take up to three text files and transform them. Currently the script will only transform the text from one file. Here's the existing script I have:
if [ $# -eq 1 ]
then
if [ -f $1 ]
then
name="My Name"
echo $name
date
starting_data=$1
sed '/^id/ d' $starting_data > raw_data3
sed 's/-//g' raw_data3 > raw_data4
cut -f1 -d, raw_data4 > cutfile1.col1
cut -f2 -d, raw_data4 > cutfile1.col2
cut -f3 -d, raw_data4 > cutfile1.col3
sed 's/$/:/' cutfile1.col2 > last
sed 's/^ //' last > last2
sed 's/^ //' cutfile1.col3 > first
paste -d\ first last2 cutfile1.col1 > final
cat final
else
echo "$1 cannot be found."
fi
else
echo "Please enter a filename."
fi
All those temp files are unnecessary. awk can do all of what sed and cut can do, so this should be what you want (pending the output field separator question)
if [ $# -eq 0 ]; then
echo "usage: $0 file ..."
exit 1
fi
for file in "$#"; do
if ! [ -f "$file" ]; then
echo "file not found: $file"
continue
fi
name="My Name"
echo "$name"
date
awk -F, -v OFS=" " '
/^id/ {next}
{
gsub(/-/, "")
sub(/^ /, "", $2)
sub(/^ /, "", $3)
print $3, $2 ":", $1
}
' "$file" > final
cat final
done
Note all my double quotes: those are required.

How can I list a specific line/word from my text?

I have this until now:
if [[ $1 = "-s" ]] && [[ $2 =~ ^[0-9]+$ ]]
echo "ok"
for $2 = 1 awk {print $1} something.txt
fi
What I want is, for example, when I enter -s 2, to list the second word of every line first!
I already know the for part is wrong.
Replace
for $2 = 1 awk {print $1} something.txt
by
awk -v var="$2" '{print $var}' something.txt

Get the highest value from a variable using bash script

I am trying to extract the highest number from $countip and copy it to $totalip, the problem is that $totalip is allways returning 0. Can anybody help me please, i am new to bash script.
for srcip in `cat /var/log/messages | grep "WACSLAW1 CRITICAL INCOMING" | awk '{ print $14 }'|grep -v 192.168.1. |grep -v IN=eth1 |grep -v MAC`;do
if (! grep "$srcip" /var/wacstemp/ids.tmp > /dev/null) ; then
countip=0
echo $srcip >> /var/wacstemp/ids.tmp
else
countip=`expr $countip + 1`
if [ $countip -gt $totalip ];
then
# echo $countip
countip=$totalip
# echo $totalip
fi
fi
done
You must switch
countip=$totalip
to
totalip=$countip
otherwise totalip will never be updated.
OT: You could simplify the initial filter to
awk '/WACSLAW1 CRITICAL INCOMING/ && $14 !~ /192.168.1./ && $14 !~ /IN=eth1/ && $14 !~ /MAC/ { print $14 }' /var/log/messages

Resources