Loop through a file with colon-separated strings

后端 未结 2 1648
情深已故
情深已故 2021-01-22 04:38

I have a file that looks like this:

work:week:day:england:
work1:week:day:sweden:
work2:week:day::
..... 

Each time I loop through the list I w

2条回答
  •  离开以前
    2021-01-22 04:49

    You can use just bash for this: set IFS (internal field separator) to : and this will catch the fields properly:

    while IFS=":" read -r a b c country
    do
      echo "$country"
    done < "file"
    

    This returns:

    england
    sweden
    
    

    This way you will be able to use $a for the first field, $b for the second, $c for the third and $country for the forth. Of course, adapt the number and names to your requirements.

    All together:

    while IFS=":" read a b c country
    do
       if [[ "$country" == "england" ]]; then
          echo "this user works in England"
       else
          echo "You do not work in England"
       fi
    done < "file"
    

提交回复
热议问题