1

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...

kakigoori
  • 1,218
  • 10
  • 20

2 Answers2

1

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.

Jotne
  • 40,548
  • 12
  • 51
  • 55
  • We are here to help, and you did try to solve it. If you like my answer, accept it by selecting the check mark on the side of the post :) – Jotne Dec 18 '13 at 21:17
1
awk -F'IdentifierA\\s*' '$0=$2' file

this line would pick all text after your IdentifierA (with leading spaces/tabs removed) no matter how many spaces do you have in the target part. that means, you don't have to print $(NF-n), $(NF-n-1)... or if the text has TAB instead of space

E.g in this case:

IdentifierA a b c d e f g h i j k l

test:

kent$  echo "IdentifierA Some Text
IdentifierB Some Other"|awk -F'IdentifierA\\s*' '$0=$2' 
Some Text

kent$  echo "IdentifierA a b c d e f g h i j k l"|awk -F'IdentifierA\\s*' '$0=$2'
a b c d e f g h i j k l 

btw, grep is a very handy tool if you want to extract text:

kent$  echo "IdentifierA a b c d e f g h i j k l"|grep -oP 'IdentifierA\s*\K.*'  
a b c d e f g h i j k l
Kent
  • 189,393
  • 32
  • 233
  • 301