从文件读入变量 - bash脚本TAKE2 [英] Read from file into variable - Bash Script take2
问题描述
这是的第二部分<一href=\"http://stackoverflow.com/questions/23390579/read-from-file-into-variable-bash-script/23390691?noredirect=1#comment35875691_23390691\">Read从文件到变量 - bash脚本
我有一个bash脚本,在文件读取解析字符串,并将其分配给一个变量。该文件看起来像这样(file.txt的):
数据库1表1
表4的Database1
DATABASE2
数据库3表2
在脚本中使用awk的:
S = $(AWK'{$ 1 = $ 1} 1'= OFS = ORS'。''|'file.txt的)
LIST =$ {S%|}回声$ LIST
database1.table1 | database1.table4 | DATABASE2 | database3.table2
但我需要在每个字符串的末尾添加一些通配符。我需要这样的结果:
database1.table1 * |。database1.table4 * | DATABASE2 * |。database3.table2 *
的条件是:如果我们读DATABASE2输出应该是DATABASE2 *如果我们读取的数据库和表格输出应该是database1.table1 *
。假设(有点奇怪)正则表达式是正确的以下awk脚本对我的作品在你的榜样输入。
BEGIN {OFS =。 ORS =|}
!$ 2 {$ 1 = $ 1*}
{$(NF + 1)=*}
1
- 设置
OFS
和ORS
。 - 如果我们没有第二个字段中添加
*
我们的第一个字段。 - 添加
*
作为最后一个字段。 - 打印就行了。
运行为的awk -f script.awk inputfile中
其中,上面的脚本是在 script.awk
(或无论)文件。
This is a second part of Read from file into variable - Bash Script
I have a bash script that reads strings in a file parses and assigns it to a variable. The file looks like this (file.txt):
database1 table1
database1 table4
database2
database3 table2
Using awk in the script:
s=$(awk '{$1=$1}1' OFS='.' ORS='|' file.txt)
LIST="${s%|}"
echo "$LIST"
database1.table1|database1.table4|database2|database3.table2
But I need to add some wildcards at the end of each substring. I need this result:
database1.table1.*|database1.table4.*|database2*.*|database3.table2.*
The conditions are: if we read database2 the output should be database2*.* and if we read a database and a table the output should be database1.table1.*
Assuming the (slightly odd) regex is correct the following awk script works for me on your example input.
BEGIN {OFS="."; ORS="|"}
!$2 {$1=$1"*"}
{$(NF+1)="*"}
1
- Set
OFS
andORS
. - If we do not have a second field add a
*
to our first field. - Add a
*
as a final field. - Print the line.
Run as awk -f script.awk inputfile
where the above script is in the script.awk
(or whatever) file.
这篇关于从文件读入变量 - bash脚本TAKE2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!