[英]Bash: how to read a multiple lines into another file, while replacing delims and truncating after token $n
What I'm trying to do is change the format of one particular file into another: 我想做的是将一个特定文件的格式更改为另一个:
input.csv: input.csv:
value1,value2,value3,value4,value5,value6
output.txt: output.txt:
value2:value3
I can almost do this using the following, but it all gets read out on the same line, rather than out to multiple: 我几乎可以使用以下命令执行此操作,但是所有这些操作都在同一行上读取,而不是多次读取:
output=$(while IFS="," read -r value1 value2 value3 remainder; do echo $value2:$value3 ; done < "input.csv")
echo $output > output.txt
solved my own issue by adding \\ at the end of the echo string 通过在回显字符串的末尾添加\\解决了我自己的问题
With awk
: 用
awk
:
awk -F',' '{printf "%s:%s\n", $2, $3}' file.csv
Printing comma separated second and third column with :
as the separator. 印刷用逗号分开的第二和第三列
:
作为分隔符。
Example: 例:
% awk -F',' '{printf "%s:%s\n", $2, $3}' <<<'value1,value2,value3,value4,value5,value6'
value2:value3
通过在回显字符串的末尾添加反斜杠“ \\”来解决
If the output
variable is not needed for anything else you can save a pid and some grief by removing the command substitution 如果不需要其他任何
output
变量,则可以通过删除命令替换来保存pid和一些麻烦
while IFS=',' read -r value1 value2 value3 remainder; do
echo "$value2:$value3"
done
A more robust and general way to process csv input is to use awk as shown at https://www.gnu.org/software/gawk/manual/html_node/Splitting-By-Content.html 处理csv输入的更可靠,更通用的方法是使用awk,如https://www.gnu.org/software/gawk/manual/html_node/Splitting-By-Content.html所示
awk 'BEGIN {FPAT="([^,]*)|(\"[^\"]+\")"; OFS=":"} {print $2,$3}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.