Search code examples
regexawkackpbcopy

Is there a nice way to copy columns of text from a file using regex into my pasteboard?


Currently, the solution that I have is kind of ugly and is pretty spaghetti-code like... is there a nice way to do this with a "standard" tool like ack?

What I have right now is a file that is in this format:

crap.txt:

IdentifierA Some Text
IdentifierB Some Other

This is how I currently get it:

cat crap.txt |
ack (?<=IdentifierA ).+ |
awk '{ print $(NF-1), $NF }' |
pbcopy

This will yield Some Text in your pasteboard.

Instead of just having ack echo out the line that matches the regex and then getting the last two columns of that line with awk, can I just get the specific regex match to print out to console? I tried using grep -o but that doesn't seem to have positive lookbehind...


Solution

  • Some like this:

    awk '/IdentifierA/ { print $(NF-1),$NF}' crap.txt
    Some Text
    

    This search for IdentifierA and print the two last filed from that line.