Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-25-2012, 10:32 PM
|
#1
|
LQ Newbie
Registered: Jun 2012
Posts: 16
Rep:
|
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
AAAAAA|jtyfytthyhyRTYUewertyu|OOOOOOOOO
output:
Code:
AAA|ZXCVjhkjhkjhk|DDDDDDDD
AAAAAA|RTYUewertyu|OOOOOOOOO
I was thinking using 2 gensub commands for each pattern:
Code:
gensub(/(.*)(pattern1)(.*)/,"\\2\\3","g",$2)
gensub(/(.*)(pattern2)(.*)/,"\\2\\3","g",$2)
But is there a way to write it only with one command?
Thanks for your help !
|
|
|
06-26-2012, 02:15 AM
|
#2
|
LQ Newbie
Registered: May 2007
Location: Berlin, FRG
Distribution: Ubuntu
Posts: 22
Rep:
|
Try regexp group alternatives:
Code:
gensub(/(.*)(pattern1|pattern2)(.*)/,"\\2\\3","g",$2)
|
|
|
06-26-2012, 09:16 PM
|
#3
|
LQ Newbie
Registered: Jun 2012
Posts: 16
Original Poster
Rep:
|
Thanks Jebram !
Before I tried with "||" instead of "|", it works now !
|
|
|
06-27-2012, 01:38 AM
|
#4
|
LQ Newbie
Registered: Jun 2012
Posts: 16
Original Poster
Rep:
|
Actually, it doesn't work...
It takes only pattern2 but doesn't change anything when there is pattern1 !
output:
Code:
AAA|bbbbbZXCVjhkjhkjhk|DDDDDDDD
AAAAAA|RTYUewertyu|OOOOOOOOO
And when I write the patterns between braces:
Code:
awk 'BEGIN{FS=OFS"|"} {gensub(/^(.*)((pattern1)|(pattern2))(.*)$/,"\\2\\3","g",$2)}1' input
It doesn't change anything when it matches pattern1, and only conserve pattern2 when it matches pattern2:
Code:
Code:
AAA|bbbbbZXCVjhkjhkjhk|DDDDDDDD
AAAAAA|RTYU|OOOOOOOOO
I don't know if it is a problem of syntax or if this command can handle several patterns at once !!!!
Last edited by lcvs; 06-27-2012 at 01:50 AM.
|
|
|
06-27-2012, 02:20 AM
|
#5
|
LQ Newbie
Registered: Jun 2012
Posts: 16
Original Poster
Rep:
|
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
Surprisingly, it works only when the field matches pattern2 (and returns the entire string when it matches pattern1).
Help would be welcome... :-)
|
|
|
06-27-2012, 03:16 AM
|
#6
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017
|
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
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 06:34 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|