bash 从文件读取时在shell脚本中拆分字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27500692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:47:26  来源:igfitidea点击:

Split String in shell script while reading from file

bashshell

提问by Umair Khalid

I have a following script which should read line by line from a ".properties" file and then tokenize it on base of "=" delimiter and store values into two variables and then display it. But I am not getting understanding of how to tokenize it and then store it in two different variables and then use it for further purposes.

我有一个以下脚本,它应该从“.properties”文件中逐行读取,然后根据“=”分隔符对其进行标记并将值存储到两个变量中,然后显示它。但我不明白如何标记它,然后将它存储在两个不同的变量中,然后将其用于进一步的目的。

Following script works fine in reading the file line by line but i need help in implementing the logic of splitting the string.

以下脚本在逐行读取文件时工作正常,但我需要帮助来实现拆分字符串的逻辑。

"Properties File"

“属性文件”

FNAME=John
LNAME=Lock
DOB=01111989

Script

脚本

#!/bin/bash
echo "FileReading Starts"
while read line
do 
    value=$line
    echo $value
    #Tokenize Logic
    property=sample
    property_value=sample
    echo $property
    echo $property_value
done <testprop.properties

echo "Finish"

回答by Gilles Quenot

Try this :

尝试这个 :

#!/bin/bash

while IFS='=' read -r col1 col2
do 
    echo "$col1"
    echo "$col2"
done <testprop.properties

IFSis the Input Filed Separator.

IFS是输入字段分隔符。

But instead of parsing the file (like fedorquisaid), you can source the file and accessing the variables directly:

但不是解析文件(如fedorqui所说),您可以获取文件并直接访问变量:

source testprop.properties
echo "$FNAME"

From $ LANG=C help source:

来自$ LANG=C help source

source: source filename [arguments]
Execute commands from a file in the current shell.

Read and execute commands from FILENAME in the current shell.  The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.

Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.


Last but not least, use more quotes! See http://mywiki.wooledge.org/Quotes, http://mywiki.wooledge.org/Argumentsand http://wiki.bash-hackers.org/syntax/words.

最后但并非最不重要的是,使用更多引号!请参阅http://mywiki.wooledge.org/Quoteshttp://mywiki.wooledge.org/Argumentshttp://wiki.bash-hackers.org/syntax/words

回答by nu11p01n73R

The IFScan be used to set field separator values to read

IFS可用于组字段分隔符值,以read

Example

例子

while IFS="=" read line val
do 
   echo $line : $val; 
done < inputFile 

Gives output as

给出输出为

FNAME : John
LNAME : Lock
DOB : 01111989

Internal Variable

内部变量

$IFS
    internal field separator
    This variable determines how Bash recognizes fields, or word boundaries, when it interprets character strings.

回答by frodo

I wander if it is possible to separate in the answer file itself (sending sql queries)

我徘徊是否可以在答案文件本身中分开(发送 sql 查询)

current file.sql have select ....... select ....... select .......

当前文件.sql有选择.......选择.......选择......

and script getting in file like declaration and working fine if file have only one query

和脚本像声明一样进入文件并且如果文件只有一个查询就可以正常工作

declare -a query=$(cat "file.sql")

声明 -a 查询=$(cat "file.sql")

and

for Q in "${query[@]}"; do ..... something done

对于“${query[@]}”中的 Q;做……做某事

now it is now it is sending all select lines in one go instead of one at the time

现在是一次发送所有选择的行而不是当时的一行