Skip to main content
added 3 characters in body; edited title
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265

Display grep output whithwith line breaks inside a if statement in a bash script

I want to validate a text file with a script.

The file to validate is:

FDFHDK JKL
1545665 152
HDKFHDK UHG
YRYRUBH DFG
867HDKE WER

Valid lines must match the regex '[A-Z]{7}+[[:space:]]+[A-Z]{3}'.

If all the lines are valid, the script shows a message saying that the file is OK.

If at least there is at least one line that don'tdoesn't match with the regex, the script should show a message and display the lines that don't match the regex.

The script is:

#!/usr/bin/env bash
result=""
output=$(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1" |wc -l)
if [[ $output > 0 ]]
then
  echo "These lines don't match:"
  result="${resultado} $(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1") \n"
  echo -e $result
else
  echo "The text file is valid"
fi  

The expected output is

These lines don't match
FDFHDK JKL
1545665 152
867HDKE WER

But I'm getting

These lines don't match:
FDFHDK JKL 1545665 152 867HDKE WER

So the actual script is not taking the line break into account.

Display grep output whith line breaks inside a if statement in a bash script

I want to validate a text file with a script.

The file to validate is:

FDFHDK JKL
1545665 152
HDKFHDK UHG
YRYRUBH DFG
867HDKE WER

Valid lines must match the regex '[A-Z]{7}+[[:space:]]+[A-Z]{3}'.

If all the lines are valid, the script shows a message saying that the file is OK.

If at least there is one line that don't match with the regex, the script show a message and display the lines that don't match the regex.

The script is:

#!/usr/bin/env bash
result=""
output=$(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1" |wc -l)
if [[ $output > 0 ]]
then
  echo "These lines don't match:"
  result="${resultado} $(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1") \n"
  echo -e $result
else
  echo "The text file is valid"
fi  

The expected output is

These lines don't match
FDFHDK JKL
1545665 152
867HDKE WER

But I'm getting

These lines don't match:
FDFHDK JKL 1545665 152 867HDKE WER

So the actual script is not taking the line break into account.

Display grep output with line breaks inside a if statement in a bash script

I want to validate a text file with a script.

The file to validate is:

FDFHDK JKL
1545665 152
HDKFHDK UHG
YRYRUBH DFG
867HDKE WER

Valid lines must match the regex '[A-Z]{7}+[[:space:]]+[A-Z]{3}'.

If all the lines are valid, the script shows a message saying that the file is OK.

If at there is at least one line that doesn't match the regex, the script should show a message and display the lines that don't match the regex.

The script is:

#!/usr/bin/env bash
result=""
output=$(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1" |wc -l)
if [[ $output > 0 ]]
then
  echo "These lines don't match:"
  result="${resultado} $(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1") \n"
  echo -e $result
else
  echo "The text file is valid"
fi  

The expected output is

These lines don't match
FDFHDK JKL
1545665 152
867HDKE WER

But I'm getting

These lines don't match:
FDFHDK JKL 1545665 152 867HDKE WER

So the actual script is not taking the line break into account.

Re-tag
Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77
Became Hot Network Question
I edit the question to add FDFHDK JKL to expected output.
Source Link
Emilio Galarraga
  • 1.3k
  • 1
  • 12
  • 20

I want to validate a text file with a script.

The file to validate is:

FDFHDK JKL
1545665 152
HDKFHDK UHG
YRYRUBH DFG
867HDKE WER

Valid lines must match the regex '[A-Z]{7}+[[:space:]]+[A-Z]{3}'.

If all the lines are valid, the script shows a message saying that the file is OK.

If at least there is one line that don't match with the regex, the script show a message and display the lines that don't match the regex.

The script is:

#!/usr/bin/env bash
result=""
output=$(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1" |wc -l)
if [[ $output > 0 ]]
then
  echo "These lines don't match:"
  result="${resultado} $(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1") \n"
  echo -e $result
else
  echo "The text file is valid"
fi  

The expected output is

These lines don't match
FDFHDK JKL
1545665 152
867HDKE WER

But I'm getting

These lines don't match:
FDFHDK JKL 1545665 152 867HDKE WER

So the actual script is not taking the line break into account.

I want to validate a text file with a script.

The file to validate is:

FDFHDK JKL
1545665 152
HDKFHDK UHG
YRYRUBH DFG
867HDKE WER

Valid lines must match the regex '[A-Z]{7}+[[:space:]]+[A-Z]{3}'.

If all the lines are valid, the script shows a message saying that the file is OK.

If at least there is one line that don't match with the regex, the script show a message and display the lines that don't match the regex.

The script is:

#!/usr/bin/env bash
result=""
output=$(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1" |wc -l)
if [[ $output > 0 ]]
then
  echo "These lines don't match:"
  result="${resultado} $(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1") \n"
  echo -e $result
else
  echo "The text file is valid"
fi  

The expected output is

These lines don't match
1545665 152
867HDKE WER

But I'm getting

These lines don't match:
FDFHDK JKL 1545665 152 867HDKE WER

So the actual script is not taking the line break into account.

I want to validate a text file with a script.

The file to validate is:

FDFHDK JKL
1545665 152
HDKFHDK UHG
YRYRUBH DFG
867HDKE WER

Valid lines must match the regex '[A-Z]{7}+[[:space:]]+[A-Z]{3}'.

If all the lines are valid, the script shows a message saying that the file is OK.

If at least there is one line that don't match with the regex, the script show a message and display the lines that don't match the regex.

The script is:

#!/usr/bin/env bash
result=""
output=$(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1" |wc -l)
if [[ $output > 0 ]]
then
  echo "These lines don't match:"
  result="${resultado} $(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1") \n"
  echo -e $result
else
  echo "The text file is valid"
fi  

The expected output is

These lines don't match
FDFHDK JKL
1545665 152
867HDKE WER

But I'm getting

These lines don't match:
FDFHDK JKL 1545665 152 867HDKE WER

So the actual script is not taking the line break into account.

Copy edited
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718
Loading
code to none to avoid ' text highlight
Source Link
Loading
Source Link
Emilio Galarraga
  • 1.3k
  • 1
  • 12
  • 20
Loading