Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash连接文件中的行w/boolean_Bash - Fatal编程技术网

Bash连接文件中的行w/boolean

Bash连接文件中的行w/boolean,bash,Bash,我一直在尝试逐行读取文件,并输出每一行的值和布尔结果 文本文件的内容 082843ab-c1e5-4729-8c03-2cec11996f01 09180b12-21b3-4bdb-a27b-ef9c026909f3 #!/bin/bash my_file="${1}" declare -a myarray let i=0 while IFS=$'\n' read -r line_data; do myarray[i]="${line_data}" ((++i)) done

我一直在尝试逐行读取文件,并输出每一行的值和布尔结果

文本文件的内容

082843ab-c1e5-4729-8c03-2cec11996f01
09180b12-21b3-4bdb-a27b-ef9c026909f3
#!/bin/bash

my_file="${1}"

declare -a myarray
let i=0
while IFS=$'\n' read -r line_data; do
    myarray[i]="${line_data}"
    ((++i))
done < $my_file

force_transmission="true"
let i=0
while (( ${#myarray[@]} > i ));do
    new_var=${myarray[i++]}
    combined_var="$new_var $force_transmission"
    echo $combined_var

    #this will print out the external id(file)
    #printf "${myarray[i++]}\n"
done
用于分析文件的脚本

082843ab-c1e5-4729-8c03-2cec11996f01
09180b12-21b3-4bdb-a27b-ef9c026909f3
#!/bin/bash

my_file="${1}"

declare -a myarray
let i=0
while IFS=$'\n' read -r line_data; do
    myarray[i]="${line_data}"
    ((++i))
done < $my_file

force_transmission="true"
let i=0
while (( ${#myarray[@]} > i ));do
    new_var=${myarray[i++]}
    combined_var="$new_var $force_transmission"
    echo $combined_var

    #this will print out the external id(file)
    #printf "${myarray[i++]}\n"
done
实际值

true3ab-c1e5-4729-8c03-2cec11996f01
trueb12-21b3-4bdb-a27b-ef9c026909f3

任何帮助都将不胜感激,我已经尝试了这方面的5种变体

如果您想这样做,请使用其中一种专用工具,而不是
bash
脚本

已更新 要消除
CR
,只需

$ sed 's/\r$/ true/' file

082843ab-c1e5-4729-8c03-2cec11996f01 true
09180b12-21b3-4bdb-a27b-ef9c026909f3 true

您的输入文件有DOS换行符。打印时,这些命令会将光标发送回行首,从而在打印后覆盖内容

在输入文件上使用
dos2unix
,或修改以下行:

new_var=${new_var%$'\r'} # remove $'\r', a CR character, from the end of new_var

正如预期的那样,它工作得很好!我刚刚在UbuntuforWindows上试用了你的脚本,它按预期输出。我不知道你的实际情况。你如何调用你的脚本?/my_script.sh myfile.txt问题很明显——输入文件有DOS换行符。如果给定一个带有CRLFs的输入文件,这两个换行符都将与OP的原始代码具有完全相同的行为。这确实产生了相同的结果,但也让我意识到我的文件是问题的一部分。谢谢,就这样,谢谢。这有助于我在运行脚本之前不必手动解析文件。我会给你一个向上投票,但我没有足够的代表,当我这样做时,我将不得不返回。欢呼