awk:searching for a pattern and remove everything before it
Hi all !
I am trying with awk to search for lines matching one of 2 patterns in the 2nd field, and returns only the lines with the pattern + everything after it in this field (= removing everything before the pattern in the field). input (pattern 1 in red, pattern 2 in blue): Code:
AAA|bbbbbZXCVjhkjhkjhk|DDDDDDDD Code:
AAA|ZXCVjhkjhkjhk|DDDDDDDD I was thinking using 2 gensub commands for each pattern: Code:
gensub(/(.*)(pattern1)(.*)/,"\\2\\3","g",$2) Thanks for your help ! |
Try regexp group alternatives:
Code:
gensub(/(.*)(pattern1|pattern2)(.*)/,"\\2\\3","g",$2) |
Thanks Jebram !
Before I tried with "||" instead of "|", it works now ! |
Actually, it doesn't work...
It takes only pattern2 but doesn't change anything when there is pattern1 ! output: Code:
AAA|bbbbbZXCVjhkjhkjhk|DDDDDDDD And when I write the patterns between braces: Code:
awk 'BEGIN{FS=OFS"|"} {gensub(/^(.*)((pattern1)|(pattern2))(.*)$/,"\\2\\3","g",$2)}1' input Code:
|
I also tried with a regex for the 2 patterns:
Code:
awk 'BEGIN{FS=OFS"|"} {gensub(/^(.*)([ZR][XT][CY][VU])(.*)$/,"\\2\\3","g",$2)}1' input Help would be welcome... :-) |
How about:
Code:
awk 'BEGIN{OFS=FS="|"}match($2,/(ZXCV|RTYU)/,f){split($2,a,f[1]);sub(a[1],"",$2);$1=$1;print}' file |
All times are GMT -5. The time now is 12:17 PM. |