[英]Bash to loop over two columns but just print the first columns and on second run the desired command
i have a bash script where i want echo two distinct fields and combine the command output into it to make it a single line.我有一个 bash 脚本,我想在其中回显两个不同的字段并将命令输出组合到其中以使其成为单行。
I am reading from a csv
file two columns that's $2
which is a project name and another $NF
which is a volume name.我正在从csv
文件中读取两列,其中$2
是项目名称,另一列$NF
是卷名。 where i am running a command against each volume in a for loop to get the data but i want project and $2
also be printed along with each volume name in a echo command So, i could know a particular project have particular data.我在 for 循环中对每个卷运行命令以获取数据,但我希望project and $2
也与 echo 命令中的每个卷名一起打印所以,我可以知道一个特定的项目有特定的数据。
below is sample code:以下是示例代码:
#!/bin/bash
#
s_list=$(cat ldap-project-nismap.csv | tr "," "\t"| awk '{print $2, $NF}'| tr -d '"'|sed 1d|head -5)
echo $s_list
for name in $(echo "$s_list");
do
echo -n "$2: $NF"
# do Some action on "$NF"
locate $NF
done
CSV DATA SAMPLE: CSV 数据样本:
"DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle_pd2/q",1000,"761.87",76,economy,"eaeagle_pd2","dbc2002_dnpc_eaeagle_pd2"
"DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle2_7/q",4096,"3536.99",86,scratch,"eaeagle2_7","dbc2002_dnpc_eaeagle2_7"
"DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle2_6/q",4096,"2976.74",73,scratch,"eaeagle
Below is the s_list
list which contains Project Name
and Volume Name
.下面是包含Project Name
和Volume Name
的s_list
列表。
d62320 fxn3008_d62320 d62200 fxn3008_d62200 d62150 fxn3008_d62150 d62110 fxn3008_d62110 d62100 fxn3008_d62100
Below is what it looks like:下面是它的样子:
Project_name Volume_name
d62320 fxn3008_d62320
d62200 fxn3008_d62200
d62150 fxn3008_d62150
d62110 fxn3008_d62110
d62100 fxn3008_d62100
What is desired:想要什么:
d62320:fxn3008_d62320:/q/volset/fxn3008_d62320
First, you can get rid of cat
tr
sed
head
with a single awk
.首先,您可以使用单个awk
摆脱cat
tr
sed
head
。
Also, the locate
command uses a pattern;此外, locate
命令使用模式; it might not be a problem with your volumes names but at the very least you need to prepend */
to it.你的卷名可能不是问题,但至少你需要在它前面加上*/
。
awk -F, '2 <= NR && NR <= 6 {gsub(/"/,""); print $2,$NF}' file.csv |
while read -r project volume
do
path=$(locate -n 1 "*/$volume")
printf '%s:%s:%s\n' "$project" "$volume" "$path"
done
remark: parsing a CSV this way only works when there isn't any comma nor double-quote nor newline embedded in the fields data.备注:仅当字段数据中没有嵌入任何逗号、双引号或换行符时,以这种方式解析 CSV 才有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.