Search code examples
awkgrepcut

Using cut to filter a fixed width file


Let's say I have a file like so:

1        10        20        30        40       50
==================================================
foofoofoo1111111111bblah      moreblahblblahblah
foofoofoo2          foofoo              stuffhere

=================================================

I want to return all rows where positions 11-20 and 31-40 are blank. I can identify them using cut:

cut -b 11-20,31-40 < source.txt

That returns the characters in those positions.

====================
111111111bmoreblahbl

====================

The second row (ignoring the rows of ===) is all blanks. I want to redirect the entire row where those characters are blank/spaces (so the second row here) to a new file. I can't figure out how to combine cut and grep to do this. Surely this has to possible, but I can't work it out.


Solution

  • Something like this? Using awk:

    $ awk 'substr($0,11,10) substr($0,31,10)~/^ *$/' file
    foofoofoo2          foofoo              stuffhere
    

    Explained:

    $ awk '
    substr($0,11,10) substr($0,31,10)~/^ *$/ # positions 11-20 and 31-40 are all space
    ' file
    

    Using grep:

    $ grep "^.\{10\} \{10\}.\{10\} \{10\}" file
    

    From the start (^) there are 10 any chars (.\{10\}) then 10 spaces (\{10\}) and repeat.

    Edit:

    Shorter version of the grep:

    $ grep "^\(.\{10\} \{10\}\)\{2\}" file