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.