Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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
将CSV文件馈送到Bash中的变量中进行解析_Bash_Csv_Scripting - Fatal编程技术网

将CSV文件馈送到Bash中的变量中进行解析

将CSV文件馈送到Bash中的变量中进行解析,bash,csv,scripting,Bash,Csv,Scripting,对于CSV文件中的每一行,我想提取一个字段,并使用Bash在该行中重新定位它。该行是一个URL,我使用/作为分隔符 这是起始文件(start.csv): 供以后参考,URL是https://{URL}/{version}/unrelated/{guide}/unrelated.html 所需输出为(end.csv): 我尝试了以下多种变体,但均未成功: file="start.csv" var="$(<<<"${file}" cut -d'/' -f4)" sed -e "s

对于CSV文件中的每一行,我想提取一个字段,并使用Bash在该行中重新定位它。该行是一个URL,我使用
/
作为分隔符

这是起始文件(start.csv):

供以后参考,URL是https://{URL}/{version}/unrelated/{guide}/unrelated.html

所需输出为(end.csv):

我尝试了以下多种变体,但均未成功:

file="start.csv"
var="$(<<<"${file}" cut -d'/' -f4)"

sed -e "s|$|,$var,,,,0|g" < start.csv > end.csv
file=“start.csv”

var=“$”(使用
awk
更容易:

awk -F/ -v OFS=, '{print $0, "", "", $4, $6, 0}' file


比我想弄明白的要好得多。谢谢。这里有两个问题,一个是琐碎的,一个是严重的。琐碎的一个是
@GordonDavisson谢谢你的解释-现在我更清楚了
file="start.csv"
var="$(<<<"${file}" cut -d'/' -f4)"

sed -e "s|$|,$var,,,,0|g" < start.csv > end.csv
cut -d'/' -f4 < start.csv
awk -F/ -v OFS=, '{print $0, "", "", $4, $6, 0}' file
https://docs.website.com/12-3/articles/guide-1/article-1.html,,,12-3,guide-1,0
https://docs.website.com/12-2/articles/guide-2/article-5.html,,,12-2,guide-2,0
https://docs.website.com/12-1/articles/guide-3/article-6.html,,,12-1,guide-3,0