循环遍历具有特定列号 1 和列号 2 的文件

循环遍历具有特定列号 1 和列号 2 的文件

我有一个文件 test.txt,其中包含 1000 行和 2 列:

am-rls-dev am-nexus
am-rls-dev cancel-ppd-nexus
am-rls-dev h2h-autodebet-token-nexus
am-rls-dev h2h-autodebet-transact-nexus
am-rls-dev h2h-onlinepaymentmuf-nexus
am-rls-dev preterm-nexus
am-rls-dev received-trade-ho-nexus
chatboot-api-dev chatbot-api-nexus
chatboot-api-dev chatbot-be-nexus
cis-rls-dev cif-cis-nexus
cis-rls-dev cis-nexus
cis-rls-dev custhandling-cis-nexus
cis-rls-dev rpt-handling-nexus

如果数据用双空格分隔,并且后面还有一个空格:

am-rls-dev  am nexus
am-rls-dev  cancel ppd nexus
am-rls-dev  h2h autodebet token nexus
am-rls-dev  h2h autodebet transact nexus
am-rls-dev  h2h onlinepaymentmuf nexus

可以说,第一列是命名空间,第二列是构建配置。我的问题是,我如何能够像这样循环打印:

echo this namespace is: $namespace and this buildconfig is: $buildconfig
echo this namespace is: $namespace and this buildconfig is: $buildconfig
echo this namespace is: $namespace and this buildconfig is: $buildconfig
echo this namespace is: $namespace and this buildconfig is: $buildconfig
echo this namespace is: $namespace and this buildconfig is: $buildconfig

答案1

使用read内置读取一行并(可选)将其拆分为单词。

while read -r namespace buildconfig ignored; do
  echo "this namespace is: $namespace and this buildconfig is: $buildconfig"
done <test.txt

如果你想并行处理这些行,你可以使用GNU并行

parallel '
  line={};
  a=($=line); namespace=$a[1] buildconfig=$a[2];
  echo "this namespace is: $namespace and this buildconfig is: $buildconfig"'

相关内容