LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 06-25-2012, 10:32 PM   #1
lcvs
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Rep: Reputation: Disabled
Smile 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 !
 
Old 06-26-2012, 02:15 AM   #2
Jebram
LQ Newbie
 
Registered: May 2007
Location: Berlin, FRG
Distribution: Ubuntu
Posts: 22

Rep: Reputation: 4
Try regexp group alternatives:
Code:
gensub(/(.*)(pattern1|pattern2)(.*)/,"\\2\\3","g",$2)
 
Old 06-26-2012, 09:16 PM   #3
lcvs
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thanks Jebram !

Before I tried with "||" instead of "|", it works now !
 
Old 06-27-2012, 01:38 AM   #4
lcvs
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-27-2012, 02:20 AM   #5
lcvs
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
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... :-)
 
Old 06-27-2012, 03:16 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,513

Rep: Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891
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.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what happens when the file in which grep is searching for pattern is blank? ameylimaye Linux - Newbie 4 10-01-2011 02:10 AM
[SOLVED] Pattern Searching whocares357 Linux - Newbie 4 11-02-2010 05:06 PM
[SOLVED] Searching a directory and pulling out filenames with a certain pattern LadyAnne Linux - Newbie 5 05-17-2010 09:30 AM
Searching for a PATTERN in source files RECURSIVELY dsuratman Linux - General 6 11-23-2007 01:51 AM
pattern searching - kindly help hinetvenkat Linux - Software 2 08-08-2005 11:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration