Shell Removing Tabs/Spaces

前端 未结 6 1692
醉酒成梦
醉酒成梦 2021-02-20 18:45

I\'ve used a grep command with sed and cut filters that basically turns my output to something similar to this

    this line 1


    this line 2


    another li         


        
6条回答
  •  感情败类
    2021-02-20 19:11

    Add this filter to remove whitespace from the beginning of the line and remove blank lines, notice that it uses two sed commands, one to remove leading whitespace and another to delete lines with no content

    | sed -e 's/^\s*//' -e '/^$/d' 
    

    There is an example in the Wikipedia article for sed which uses the d command to delete lines that are either blank or only contain spaces, my solution uses the escape sequence \s to match any whitespace character (space, tab, and so on), here is the Wikipedia example:

    sed -e '/^ *$/d' inputFileName 
    
    • The caret (^) matches the beginning of the line.
    • The dollar sign ($) matches the end of the line.
    • The asterisk (*) matches zero or more occurrences of the previous character.

提交回复
热议问题