0

I have a scenario where i have following below data in Student file

My Student.txt data input, need to follow a pattern, on each line need to contains three infos separated by / as follows:

NAME/SURNAME/COUNTRY

So, below I have an example of file that is fine:

    RAM/ABC/INDIA
    RAJ/XYZ/DELHI
    VIRAJ/FDS/GUJRAT

WHAT IS EXPECTED:

Each and every record in file should match with that syntax: NAME/SURNAME/COUNTRY.

If anyone fails then, the overall status should be displayed as failed, with the message syntax does not match, otherwise, mark as success.


WHAT'S HAPPENING:

If I pass data to above code like below:

    RAM/ABC/INDIA
    RAJ/XYZ/DELHI
    VIRAJ/FDS/

When I execute the my code, I don't get any error saying failed: syntax does not match it simply check upto below two records

    RAM/ABC/INDIA
    RAJ/XYZ/DELHI

The last record in file, that is VIRAJ/FDS/ does not check and not thrown error failed: syntax does not match

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 
William Prigol Lopes
  • 1,803
  • 14
  • 31
  • You have to explain what happened and what you expected to happen instead. "Does not work" doesn't tell me any of that. – Benjamin W. May 26 '20 at 12:33
  • @BenjaminW. i have updated the question now i added my error – womegowchordmi May 26 '20 at 12:40
  • @BenjaminW. can you try it my code and sample data – womegowchordmi May 26 '20 at 12:45
  • I get `cut: the delimiter must be a single character`, there's a blank missing after `'/'`. – Benjamin W. May 26 '20 at 12:50
  • When I fix that typo, your code works as expected (doesn't follow a few best practices, though – see https://stackoverflow.com/q/10929453/3266847). – Benjamin W. May 26 '20 at 12:52
  • @BenjaminW. bro the file data is only like that if any of the record does not match according to the syntax then should dispay failed ..... last record VIRAJ/FDS/ – womegowchordmi May 26 '20 at 14:22
  • I understand. What I'm telling you is a) there is a typo in your code which makes it display an error you're not mentioning, so I don't know if it was just not properly copy-pasted or if you're running different code and b) when I fix that typo, I can't reproduce your error. – Benjamin W. May 26 '20 at 14:37

1 Answers1

0

You can solve this using regular expressions, as you can see here:

grep "[A-Z]/[A-Z]*/[A-Z]" test.txt

This shows the following lines:

RAM/ABC/INDIA
RAJ/XYZ/DELHI

Which is caused by following entries which match:

M/ABC/I
J/XYZ/D

If you use grep -v you find the lines which don't match. Launching a wc -l, you find if there are any of such lines and in case yes, you can show your error message.

Some regular expression syntaxes use + as a way to say "one or more entries" (instead of * which means "zero or more entries") but it depends on your system's OS if this works or not.

Can you tell me if this is working out for you?

Dominique
  • 16,450
  • 15
  • 56
  • 112