Search code examples
cut

How to avoid leading space using cut command?


Requirement: I need to use only grep/cut/join.

I have data like:

  3 abcd
 23 xyz
1234 abc

I want to pipe this data to cut and then extract columns. But, when I am using cut -d' ' -f 1,2 it treats each space as its own column divider. I would prefer the first two rows be trimmed prior to cut. Is there a way?

Example (I have used tr for demonstration purposes of where the spaces are here; it is not allowed in the solution):

$ echo '  3 abcd
23 xyz
1234 abc' | cut -d' ' -f 1,2 | tr ' ' '_'
_
_23
1234_abc

Expected output:

$3 abcd
23 xyz
1234 abc

Solution

  • Using just grep, you can accomplish this with the following pipe:

    grep -oe "[^ ][^ ]*  *[^ ][^ ]*$"
    
    grep    # a tool for matching text
      -o    # only prints out matching text
      -e    # uses a regex
      [^ ]  # match anything that isn't a space
      *     # match zero or more of the previous element
      $     # the end of the line
    

    Note: This does not account for trailing whitespace.

    Demonstration:

    $ echo '  3 abcd
     23 xyz
    1234 abc' | grep -oe "[^ ][^ ]*  *[^ ][^ ]*$"
    3 abcd
    23 xyz
    1234 abc