Carriage capture (^) with awk regex

I have output in this format:

/ignore-this/^/../I/want/ this@ignore-this 

I am trying to use awk regex to commit the following:

 ../I/want/this 

This would not be particularly difficult, except that I cannot figure out how to exit ^ correctly, so that it is not interpreted as a new line or not. The following is what I still have, it almost works, except that it prints:

 /ignore-this/^/../I/want/this 

Here is the code:

 #!/bin/awk -f { if (match($0, "\^.*@")){ print substr($0, RSTART, RLENGTH-1); } } 
+4
source share
3 answers

Another possibility using gawk:

 #!/opt/local/bin/gawk -f { if (match($0, /[\^]\/(.*)@/, pieces)) { print pieces[1]; } } 
+2
source
 > echo '/ignore-this/^/../I/want/ this@ignore-this ' |\ awk -F"^" '{split($NF,a,"@");print a[1]}' 

output:

 /../I/want/this 

This divides the input stream into all "^". He then takes the last field and breaks it into "@" and prints the first half of the line.

EDIT: Or use:

 awk '/\^/{split($0,a,"[@^]");print a[2]}' file 

HTH Chris

+2
source
 awk -F'\\^|@' '{print $2}' 

should work in this case

 kent$ echo "/ignore-this/^/../I/want/ this@ignore-this "\ |awk -F'\\^|@' '{print $2}' /../I/want/this 
0
source

Source: https://habr.com/ru/post/1380048/


All Articles