0

I need to check delimiter '|' count for each line in text file for that used awk command and stored count of output file in temp file. It was generating count of delimiter for each row and script also finally I can see success with exit code 0. but in one of the line it was showing arithmetic syntax error could some one tell me how to resolve this.

I provided sample filedata, script and script output could someone tell me what was the issue here for arithmetic syntax error.

Text file Sample data: in below sample file there were 5 '|' delimiter and some sample rows

Name|Address|phone|pincode|location|
xyz|usa|123|111|NY|
abc|uk|123|222|LON|
pqr|asia|123|333|IND|

Script:

Standard_col_cnt="5"
cd /$SRC_FILE_PATH
touch temp.txt 
col_cnt=`awk -F"|" '{print NF}' $SRC_FILE_PATH/temp.txt`  >>$Logfile   2>&1

while read line
do
i=1
echo $line >/temp.txt

  if [ "$col_cnt" -ne "$Standard_col_cnt" ] 
   then
   echo "No of columns are not equal to the standard value in Line no - $i:" >>$Logfile
  exit 1
fi
i=`expr $i + 1`
done < $File_name

Awk command will generate below output to temp file:

5
5
5
5
--------- Script output -----------

script.sh[59]: [: |xyz|usa|123|111|NY|
: arithmetic syntax error
+ expr 1 + 1
+ i=2
+ read line
+ i=1
+ echo 'xyz|usa|123|111|NY|\r'
+ script.sh[48]: /temp.txt: cannot create [Permission denied]
+ 'abc|uk|123|222|LON|\r' -ne 91 ]
script.sh[59]: [: pqr|asia|123|333|IND|: arithmetic syntax error
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • `col_cnt` contains `'abc|uk|123|222|LON|\r'` which fails in `if [ "$col_cnt" -ne "$Standard_col_cnt" ]`. Further, based on the data you posted with the tailing `'|'` at the end of each row will cause `awk -F"|" '{print NF}'` to report `6` fields, not `5`. (the last field is an empty field) It's is a bit puzzling how the script you show generated that output in `col_cnt` . – David C. Rankin May 08 '22 at 07:09
  • Yes correct my bad while giving sample data I added extra ‘|’ symbol, because not to show actual log or data. But what is that arithmetic error and why it’s showing permission denied in log – codecheck123 May 08 '22 at 10:11
  • `expr` is extremely brittle, and should basically never be used in Bash scripts. The Bash shell itself (but not POSIX `sh`) has built-in facilities which supersede and surpass the capabilities of `expr`, and provide better error messages to boot. – tripleee May 08 '22 at 10:35
  • Perhaps see also https://stackoverflow.com/questions/71732793/why-do-i-get-a-syntax-error-when-using-expr-in-bash-macos – tripleee May 10 '22 at 09:17

2 Answers2

1

Your current script will constantly reset i to 1 every time the line is read.

It is unclear how your awk code is writing to the temp file, when it seems it has just been created and is then being used to create a variable, while empty!

If you want to check the condition that the | pipe delimiters per line are 5, you could do so with just awk

Sample Data

$ cat test
Name|Address|phone|pincode|location|
xyz|usa|123|111|NY|
abc|uk|123222|LON|
pqr|asia|123|333|IND|
$ export logfile
$ cat script.awk 
BEGIN {
    FS="|"
    Standard_col_cnt=5
    logfile=ENVIRON["logfile"]
} {
    if (NF-1 != Standard_col_cnt) print "No of columns are not equal to the standard value in Line no - "NR
}
$ awk -f script.awk test
$ cat "$logfile"
No of columns are not equal to the standard value in Line no - 3
HatLess
  • 10,622
  • 5
  • 14
  • 32
0
col_cnt=5
grep -o -n '[|]' input_file |awk '{print $1}' | uniq -c| \
awk -v d="$col_cnt" '$1!=d {print "No of columns are not equal to the standard value in Line no - "NR}'
No of columns are not equal to the standard value in Line no - 3

other

count=5
string="No of columns are not equal to the standard value in Line no -"
grep -o -n '[|]' input_file|cut -d: -f 1| uniq -c|sed "s/^ *//;"| sed "/^[${count} ]/d"|sed "s/^[^${count} ]/${string}/"
No of columns are not equal to the standard value in Line no - 3
ufopilot
  • 3,269
  • 2
  • 10
  • 12