Loop through a file with colon-separated strings

后端 未结 2 1651
情深已故
情深已故 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:53

    Just do the whole thing in awk:

    awk -F: '$4=="england"{print "this user works in England";next}
             {print "You do not work in England"}' file
    

    Set the field separator to a colon. If the fourth field is "england", print the first message. next skips to the next line. Otherwise, print the second message.

    The fields on each line are accessible by $1, $2, etc. so you can use the data in each field within awk to do whatever you want. The field is read line by line automatically, so there's no need to write your own while read loop.

提交回复
热议问题