[英]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 我正在尝试创建一个文件(称为帐户 )并使用bash脚本(称之为* name_checker *)循环遍历每一行并将其转换为多个字符串以存储在名为' $ NAMES '的变量中,用于mysql WHERE IN条款
For example: 例如:
My query in my script *name_checker* is basic: 我的脚本* name_checker *中的查询是基本的:
SELECT * FROM table WHERE account_name IN ('$NAMES') SELECT * FROM表WHERE account_name IN('$ NAMES')
The values for the IN statement will need to be separated by commas and put into single quotes. IN语句的值需要用逗号分隔并放入单引号中。
My accounts file will be names separated by newlines: 我的帐户文件将是由换行符分隔的名称:
NAME1 NAME1
NAME2 NAME2
NAME3 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. 所以我需要我的脚本(* name_checker *)来删除换行符并用单引号括起每个名称并用逗号分隔它们。 The desired results when the script is run would be
运行脚本时所需的结果将是
SELECT * FROM table WHERE account_name IN ('NAME1','NAME2','NAME3') SELECT * FROM表WHERE account_name IN('NAME1','NAME2','NAME3')
I am having some difficulty with this and I'm not too familiar with using sed. 我对此有一些困难,我对使用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. 如果您使用的是
bash
4或更高版本,则可以使用mapfile
命令(也拼写为readarray
)将整个文件拉入单个数组。 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 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. 注 -在SQL代码中使用变量$ NAME。
names="'$(cat <your file> | sed 's/(\\w)\\n(\\w)/\\1\\', \\'\\2/g)'"
Try that? 试试吗?
:D :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: 笔记:
ORS
stands for output record separator. ORS
代表输出记录分隔符。 It's basically the character you use to terminate every line output by awk (default: newline) \\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) \\x27
是单引号的十六进制代码,以避免与外部单引号对冲突(因为单引号内不能有任何转义序列) ${var%,}
is a parameter expansion which removes a trailing comma because the awk
command would leave you with an extra comma. ${var%,}
是一个参数扩展 ,它删除一个尾随逗号,因为awk
命令会给你一个额外的逗号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.