0

I read a text file line by line in a bash script, the goal is to catch variable declarations.

The bash script:

#!/bin/bash

filePath=$1

while read line
do
    if [[ $line =~ ^[[:blank:]]*([^#[:blank:]]+)[[:blank:]]*=[[:blank:]]*(.*) ]]
    then
        var=${BASH_REMATCH[1]}
        value=${BASH_REMATCH[2]}
        echo "$var=$value"
    fi
done < "$filePath"

With this text file (note that the line "c=3" is the last line of the file):

# First variable
a=1
# Second variable
 b = 2
# Third variable
c=3

The output is:

a=1
b=2

But with this one:

# First variable
a=1
# Second variable
 b = 2
# Third variable
c=3
# empty line

The output is:

a=1
b=2
c=3

It can be an empty line at the end, not necessary a comment.

Can someone explain why the last line is not catched by the regex in the first example? And how can I fix the problem (except from adding an empty line at the end of the text file...)? Thanks

scandel
  • 1,692
  • 3
  • 20
  • 39
  • Does your first file have a linebreak at the end? (Test with `cat -A`, check for end-of-line markers.) I suspect it doesn't. – Benjamin W. Mar 14 '17 at 14:39
  • 1
    And if this is the problem, the solution is http://stackoverflow.com/questions/12916352/shell-script-read-missing-last-line: `while read line || [[ -n $line ]]` – Benjamin W. Mar 14 '17 at 14:39
  • Wow! This is it, and your solution works perfectly! Thank you so much! – scandel Mar 14 '17 at 14:53
  • 1
    Well, it's not mine, I just knew it existed ;) – Benjamin W. Mar 14 '17 at 14:53
  • More fundamentally, maybe ask yourself if this makes sense as a Bash script. Perhaps Awk would be more versatile as well as straightforward. – tripleee Mar 15 '17 at 04:47
  • @tripleee maybe you're right, but what I shown is just a part of a bigger script, and I believe bash is a correct choice. Furthermore, I didn't wrote it in the first place ! – scandel Mar 15 '17 at 08:43

1 Answers1

0

for while read line; do ... ; doneyour file need end with a newline.

try:

while IFS= read -r line || [ -n "$line" ]; do

beliy
  • 445
  • 6
  • 13