0

i have a scenario where i want to check the below details in files match the syntax

Syntax : NAME/SURNAME/COUNTRY

Note : every record in file should match the syntax. if one records does not match then should display failed or success

File data

Ajay/abc/india
Avik/bcs/delhi
Viraj/xyz/

As you can see the file data the last record does not match according to the syntax then should display failed

My code

 for i in `cat /demo/Student.txt`
      do 
      check=`echo $i | cut -d '/' -f3`
      if [[ -z $check ]];
      then 
        echo failed syntax does not match NAME/SURNAME/COUNTRY
        exit 1 
      fi 
    done
  • Did you try anything so far? SO usually expects some indication of effort. Do you want to write this in shell script? For which shell? (`sh`, `bash`, etc.) – underscore_d May 28 '20 at 13:05
  • @underscore_d yes i tried but its not working want to do using sh script . i want check the entire file if one of the record does not match with my syntax then it should display as fail or otherwise success – womegowchordmi May 28 '20 at 13:08
  • In what way does it not work? – underscore_d May 28 '20 at 13:19
  • @underscore_d can you see three are three places NAME/SURNAME/COUNTRY ... the last record of my data does not match with SYNTAX in that case the script should show as failed and record does not match with syntax – womegowchordmi May 28 '20 at 13:23
  • @womegowchordmi: please don't ask the same question twice: https://stackoverflow.com/questions/62022172/how-to-match-the-syntax-in-file-if-matches-display-success-or-fail/62023106#62023106 – Dominique May 29 '20 at 08:09
  • Does this answer your question? [How to match the syntax in file if matches display success or fail](https://stackoverflow.com/questions/62022172/how-to-match-the-syntax-in-file-if-matches-display-success-or-fail) – Dominique May 29 '20 at 08:09

3 Answers3

0

cut -d '/'-f3

This is invalid syntax, as cut freely tells me. Did you not get any console output indicating the problem?

cut: the delimiter must be a single character Try 'cut --help' for more information.

Fix that by putting a space before the -f switch.

The script then works for me: it reports an error with your file as-is, then no error if I append a 'country' (hi) to the final line.


Demo:

input:

Ajay/abc/india
Avik/bcs/delhi
Viraj/xyz/

output:

$ sh -x test.sh
++ cat test.txt
+ for i in `cat test.txt`
++ echo Ajay/abc/india
++ cut -d / -f3
+ check=india
+ [[ -z india ]]
+ for i in `cat test.txt`
++ echo Avik/bcs/delhi
++ cut -d / -f3
+ check=delhi
+ [[ -z delhi ]]
+ for i in `cat test.txt`
++ echo Viraj/xyz/
++ cut -d / -f3
+ check=
+ [[ -z '' ]]
+ echo failed syntax does not match NAME/SURNAME/COUNTRY
failed syntax does not match NAME/SURNAME/COUNTRY
+ exit 1
underscore_d
  • 6,309
  • 3
  • 38
  • 64
  • bro i corrected it ... the issue is not with that the issue is why the script does not fail when it loop through and reach at last record where as last record as two columns Viraj/xyz/ the third value is null it should read as fail – womegowchordmi May 28 '20 at 15:25
  • And as I said, it **does** fail for me. It reports the error and returns an exit code of 1. I cannot reproduce the problem you claim to have, after I fixed the error you first posted. – underscore_d May 28 '20 at 15:26
  • have you run using sh -x your script ... you will get to see only india and delhi and what about third one it does not go through if condition – womegowchordmi May 28 '20 at 15:38
0

Maybe use awk

#check value in third column, using / as a delimiter
check="$(awk -F"/" '{print $3}' /demo/Student.txt)"

#check if third column is empty
if [ "$check" == "" ]; then
    echo "failed syntax..."
    exit 1
fi

You can run this check on the three columns (change print $3 to $1 and $2) Probably not the most efficient solution, since I am also fairly new to shell scripting, but I think it should work.

0

Syntax : NAME/SURNAME/COUNTRY

Is very much underspecified. First write a regex to match your line - then just use a utility to check if each line matches the regex.

With grep:

if grep -v -q '^[[:alpha:]]\+/[[:alpha:]]\+/[[:alpha:]]\+$' "$file"; then
    echo "failed synax grep"
fi

grep matches a regex:

  • -v invert match
  • ^ match beginning of the line
  • [[:alpha:]]\+ one or more consecutive characters sed character classes. You could use [^/]* or [^/]+ instead for the same behavior as your current code wanted to do.
  • / a slash
  • etc.
  • $ match ending of the line

With gnu sed:

if ! sed -n '\@^[[:alpha:]]\+/[[:alpha:]]\+/[[:alpha:]]\+$@!q1' "$file"; then
    echo "failed syntax"
fi

Sed does:

  • \@....@ - match the regex inside
  • ! - if the regex was not matched
  • q1 - quit with exit status 1

You could also remove all characters except newline and a slash and check if each line has 3 characters.

Notes:

KamilCuk
  • 120,984
  • 8
  • 59
  • 111