2

I have a files test.txt which contains 1000 rows and 2 column:

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

and If the data separated by double space, and still have a space after that:

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

Lets say, the first column is a namespace and the second column is a buildconfig. My question is, how am i able to print like this in a loop :

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
    Do you _need_ to do that [using a loop in the shell](https://unix.stackexchange.com/q/169716/315749)? – fra-san Sep 15 '20 at 09:46
  • @fra-san Processing text in the shell is somewhat bad practice because it's slow, but the goal of this question is not to do text processing except incidentally to obtain parameters to work on. Reading a list of elements to work on is a perfectly cromulent use of the shell. – Gilles 'SO- stop being evil' Sep 15 '20 at 19:53
  • @fra-san yes i need it in a loop. – White Mask Guy Sep 17 '20 at 10:35

1 Answers1

3

Use the read builtin to read a line and (optionally) split it into words.

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

If you want to process the lines in parallel, you can use GNU parallel.

parallel '
  line={};
  a=($=line); namespace=$a[1] buildconfig=$a[2];
  echo "this namespace is: $namespace and this buildconfig is: $buildconfig"'
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • Hi Gilles, Thanks for your help. Save me a lot of time! But can you tell me if the data separate by 2 space like above (already update the question), is it still doable with the same syntax or it should be different ? – White Mask Guy Sep 24 '20 at 04:25
  • @WhiteMaskGuy `read` treats any (non-empty) sequence of whitespace as separators, so it's the same with two spaces. (It's different if you specify a non-whitespace separator: `echo 'foo bar' | read one two` is two fields but `echo foo,,bar | IFS=, read one two three` has an empty field in the middle.) – Gilles 'SO- stop being evil' Sep 24 '20 at 08:04
  • Thanks again for the answer Gilles, Toss! – White Mask Guy Nov 09 '20 at 13:01