How to save both matching and non-matching from grep

后端 未结 3 1401
天涯浪人
天涯浪人 2021-01-12 23:36

I use grep very often and am familiar with it\'s ability to return matching lines (by default) and non-matching lines (using the -v parameter). However, I want to be able to

3条回答
  •  無奈伤痛
    2021-01-13 00:12

    Step 1 : Read the file

    Step 2 : Replace spaces with a new line and save the result in a temporary file

    Step 3 : Get only lines contains '_' from the temporary file and save it into multiwords.txt

    Step 4 : Exclude the lines that contains '-' from the temporary file then save the result into singlewords.txt

    Step 5 : Delete the temporary file

      cat file | tr ' ' '\n' > tmp.txt | grep '_' tmp.txt > multiwords.txt | grep -v '_' tmp.txt > singlewords.txt | find . -type f -name 'tmp.txt' -delete
    

提交回复
热议问题