Home > Net >  Linux script Pipe delimiter count check
Linux script Pipe delimiter count check

Time:05-08

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

CodePudding user response:

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

CodePudding user response:

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
  • Related