-1

I have a bash script that pulls from files within a directory based on an identifier and fills an sqlite3 table. The problem is that before filling the table I need to add characters to the string which is pulled. I would like to add character before and after the variable, adding before seems to work, but I'm having trouble adding to the end for some reason. I'm trying to add these characters with awk and printf, and I've tried just concatenating the strings, but neither solution is working quite as expected.

The script with awk and printf is:

#!/bin/bash

sqlite3 review.sql "CREATE TABLE Review(Review_ID INTEGER PRIMARY KEY, Author TEXT, Date TEXT);"

path="/home/drew/Downloads/testcases/*"

for i in $path
do
        total=$(grep -c '<Author>' $i)
        count=1
        while [ $count -le $total ]
        do
                date=$(grep -m$count '<Date>' $i | sed 's#<Date>##' | tail -n1)
                author=$(grep -m$count '<Author>' $i | sed 's#<Author>##' | tail -n1 | awk '{printf "-      %s      -", $1}')
                echo $author
                ((count++))
        done
done

I'm not sure, but I feel like I shouldn't have to add echo to it with printf, but without it nothing prints. With the echo I get the following output:

 -Jeanjakey
- kareem -
- may -
- RW53 -
 -Marilyn1949
 -AuntSusie006
 -madmatriarch
 -strollaround
 -lulubaby
 -tomu023
 -julcarl
 -slass

It seems to somewhat work, but the spacing disappears, any last names disappear, and it seems to do different things to different inputs. With the string concatenation I use the script:

#!/bin/bash

sqlite3 review.sql "CREATE TABLE Review(Review_ID INTEGER PRIMARY KEY, Author TEXT, Date TEXT);"


path="/home/drew/Downloads/testcases/*"

for i in $path
do
        total=$(grep -c '<Author>' $i)
        count=1
        while [ $count -le $total ]
        do
                date=$(grep -m$count '<Date>' $i | sed 's#<Date>##' | tail -n1)
                author2="-   "
                author2+=$(grep -m$count '<Author>' $i | sed 's#<Author>##' | tail -n1)
                author2+="    -"
                echo $author
                ((count++))
        done
done

with this I get the output:

 -Jeanjakey
 -kareem jabron
 -may flow she
 -RW53 
 -Marilyn1949
 -AuntSusie006
 -madmatriarch
 -strollaround
 -lulubaby
 -tomu023
 -julcarl
 -slass

and a string reassignment:

author2=$(grep -m$count '<Author>' $i | sed 's#<Author>##' | tail -n1)
author2="-   $author2     - "

gives the same output.

Edgar Smith
  • 163
  • 1
  • 10
  • 1
    Consider making a habit of running your code through http://shellcheck.net/ and fixing what it finds before asking questions here. – Charles Duffy Apr 28 '19 at 01:55
  • Note also [DontReadLinesWithFor](https://mywiki.wooledge.org/DontReadLinesWithFor) -- and if you're going to assign a *glob* to an array variable, you're on much safer ground if you assign the result instead (`files=( "$dir"/* ); for file in "${files[@]}"; do ...` will behave properly when your `dir` value has spaces, which the glob case can't). – Charles Duffy Apr 28 '19 at 01:55

1 Answers1

0

You need to quote uses of variables to prevent the shell performing word splitting.

In particular, try:

echo "$author"
jhnc
  • 11,310
  • 1
  • 9
  • 26