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

StackOverflow https://stackoverflow.com/questions/20668378

  •  19-09-2022
  •  | 
  •  

質問

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

役に立ちましたか?

解決

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.

他のヒント

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top