简体   繁体   中英

BASH - How to read file contents to insert results into mysql WHERE clause

I'm trying to make a file (call it accounts ) and have a bash script (call it *name_checker*) loop through each line and make it into multiple strings to store in a variable called ' $NAMES ' for a mysql WHERE IN clause

For example:

My query in my script *name_checker* is basic:

SELECT * FROM table WHERE account_name IN ('$NAMES')

The values for the IN statement will need to be separated by commas and put into single quotes.

My accounts file will be names separated by newlines:

NAME1

NAME2

NAME3

So I would need my script (*name_checker*) to get rid of the newlines and surround each name with single quotes and separate them with a comma. The desired results when the script is run would be

SELECT * FROM table WHERE account_name IN ('NAME1','NAME2','NAME3')

I am having some difficulty with this and I'm not too familiar with using sed. Help is appreciated!

It can be done without calling external utilities.

cat >Accounts.txt <<XXX
NAME1
NAME2
NAME3
XXX

Script:

while read x; do NAMES="$NAMES,'$x'"; done <Accounts.txt
NAMES=${NAMES:1}
SQL='SELECT * FROM table WHERE account_name IN ('$NAMES')'
echo "$SQL"

Output:

SELECT * FROM table WHERE account_name IN ('NAME1','NAME2','NAME3')

Or it can be even simplified removing explicit loop

printf -v NAMES ",'%s'" $(<Accounts.txt)
NAMES=${NAMES:1}
echo "$NAMES";

Output:

'NAME1','NAME2','NAME3'

If you are using bash 4 or later, you can use the mapfile command (also spelled readarray ) to pull the entire file into a single array. Then you can expand the array into a single string of comma-separated user names.

$ mapfile users < Accounts.txt
$ quotedUsers=( $( printf "'%s' " ${users[@]%?} ) )
$ userString=$( IFS=,; echo "${quotedUsers[*]}" )

You can try this as well ..

Accounts.txt

NAME1
NAME2
NAME3

Code

NAME=`awk '{print "-"$0"-"}' Accounts.txt| tr "-" "'" | tr '\n' ',' | sed 's/.$//g'`
echo "$NAME"

Output

'NAME1','NAME2','NAME3'

Note - Use the variable $NAME in your SQL code.

names="'$(cat <your file> | sed 's/(\\w)\\n(\\w)/\\1\\', \\'\\2/g)'"

Try that?

:D

try putting this into your query:

echo $NAMES | sed -e "s/\([a-zA-Z]*\)/'\1',/g" -e 's/,$//';

Example:

$ NAMES='Martin John Barbera Holly'
$ echo $NAMES | sed -e "s/\([a-zA-Z]*\)/'\1',/g" -e 's/,$//';
'Martin', 'John', 'Barbera', 'Holly'

This will convert your newline-separated entries into single-quoted, comma-separated entries.

var=$(awk '{print "\x27"$0"\x27"}' ORS=, accounts_file)
var="${var%,}"

Notes:

  1. ORS stands for output record separator. It's basically the character you use to terminate every line output by awk (default: newline)
  2. \\x27 is the hex code for single quotes, to avoid clashing with the outer pair of single quotes (because there cannot be any escape sequence inside single quotes)
  3. ${var%,} is a parameter expansion which removes a trailing comma because the awk command would leave you with an extra comma.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM