I want the script to open a file and read the file line by line then count the number of commas per row/line. If that is more than the $2 parameter value, then write the offending line number (from my read loop) and the total commas found to a log file.
I'm not sure what's wrong, but I'm getting a no parent error.
#!/bin/ksh
filename=$1 #First input parameter path with filename
pipe=$2 #Total Pipe Value
#Filename to parse
if [ $filename -ne $1 ]
then
echo "Filename required"
exit 1
fi
#Check pipe/comma
if [ $pipe -ne $2 ]
then
echo "Filename and number of expected pipes required"
exit 1
fi
if [ -f $1 ]
then
while read -r line
do
((i+=1))
count=${line//[^|]}
echo Line#"$i:" "${#count}" pipe per line compare to "$2" expected
done <$filename
fi
if [ $count > $2 ]
then
echo Line# "$i" has "${#count}" pipe in total > uhs_sm_qa_csv.ksh.log
fi
exit 0
Output of script
:
[root@uhspaastream01 scripts]# ksh uhs_sm_qa_csv.ksh test.txt 10
uhs_sm_qa_csv.ksh[6]: [: test.txt: no parent
Line#1: 1 pipe per line compare to 10 expected
Line#2: 1 pipe per line compare to 10 expected
Line#3: 1 pipe per line compare to 10 expected
Line#4: 1 pipe per line compare to 10 expected
Line#5: 1 pipe per line compare to 10 expected
Contents of test.txt
:
cyberciti.biz|74.86.48.99
nixcraft.com|75.126.168.152
theos.in|75.126.168.153
cricketnow.in|75.126.168.154
vivekgite.com|75.126.168.155
Contents of the log fileuhs_sm_qa_csv.ksh.log
:
Line#5 has 1 pipe in total
"$#" -eq 2
before assigning or using the parameters. – Jeff Schaller Mar 16 '18 at 19:58awk
orperl
and use the number of fields minus 1 e.g.awk -F'|' 'NF>n+1 {printf("Line# %d has %d pipe in total\n",NR,NF-1)}' n=2 filename
– steeldriver Mar 16 '18 at 20:04