for loop in bash simply prints n times the command instead of reiterating

前端 未结 2 523
孤街浪徒
孤街浪徒 2021-01-26 12:50

I have a input.txt file with over 6000 lines.

If a line a has over 10 words then I want it to be split but not at the 10th word but where the first comma character appe

2条回答
  •  被撕碎了的回忆
    2021-01-26 13:03

    OK, so here is how I solved this problem. It's ugly, but it works. Plus I can keep piping more sed commands to add more conditions (like my comment above @ghoti).

    sed -r '/((\w)+[., ]+){10}/s/\./\.\n/' input.txt | sed -r '/((\w)+[., ]+){10}/s/\./\.\n/' | sed -r '/((\w)+[., ]+){10}/s/\./\.\n/' | sed -r '/((\w)+[., ]+){10}/s/\./\.\n/'| sed -r '/((\w)+[., ]+){10}/s/\./\.\n/' | sed -r '/((\w)+[., ]+){10}/s/\./\.\n/' | sed -r '/((\w)+[., ]+){10}/s/\./\.\n/' | tr -s [:space:] > output.txt
    

    Basically, I just piped the same sed command 7 times (in the above sample I'm replacing periods instead of commas, but all the same). Based on what I read on-line, I'm surprised this command does not allow some of recursive/reiteration. Or if someone knows, please feel free to edit.

提交回复
热议问题