LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 09-08-2021, 03:17 AM   #1
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Rep: Reputation: Disabled
grep semi-variable pattern


Hello everyone,

I have used grep to process data from this input:

Code:
Scaffolds_16_pilon      ACmerged_contig_2215    5_1009.75816    11_1010 11_1011
To that output:

Code:
Scaffolds_16_pilon      ACmerged_contig_2215    1    5   
Scaffolds_16_pilon      ACmerged_contig_2215    2    11
Part of my script is based on selection the correct first number before the underscore in column three and four.
For this I have used the regexp '$f'_.* to select a specific $f followed by underscore, then a variable pattern.

Code:
grep --only-matching '\<'$f'_.*\>' |  wc --word

Unfortunately, when $f=5 the script select the three last column and not just the first one. And I don't really understand why. Any idea anyone?

Last edited by Drosera_capensis; 09-08-2021 at 03:23 AM.
 
Old 09-08-2021, 03:24 AM   #2
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Original Poster
Rep: Reputation: Disabled
A colleague just gave me this answer:

Quote:
grep -o ''$f'_' file.txt
 
Old 09-08-2021, 04:03 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by Drosera_capensis View Post
Unfortunately, when $f=5 the script select the three last column and not just the first one. And I don't really understand why.
Because the * quantifier is greedy. _\S* would select to the end of field.

Last edited by shruggy; 09-08-2021 at 04:23 AM.
 
Old 09-08-2021, 04:10 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Quote:
Originally Posted by Drosera_capensis View Post
Unfortunately, when $f=5 the script select the three last column and not just the first one. And I don't really understand why. Any idea anyone?
If the data is it tab-delimited columns, then you might also consider AWK. It has lesser pattern matching capabilities than grep but has other advantages. Perl is another option if advanced patterns are needed.

Code:
export f=5; 

awk -v f=$f '$3~f { print $1,$2 }' FS="\t" OFS="\t" ./data.tab

perl -0x08 -a -e 'chomp; if($F[2] =~ /$ENV{"f"}/){ printf("%s\t%s\n", $F[0],$F[1]); }' ./data.tab
 
Old 09-08-2021, 04:48 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Code:
grep -o -P "$f"'_\S*'
 
1 members found this post helpful.
  


Reply



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
[SOLVED] grep pattern grep -Ff phpshell Programming 6 11-24-2013 11:31 PM
[SOLVED] assign full name to variable then grep a file for the variable socalheel Programming 3 09-16-2013 11:04 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
$$ variable as GREP pattern: "weird" behavior courteous Linux - Newbie 4 12-12-2010 09:29 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM

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

All times are GMT -5. The time now is 12:26 PM.

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
Open Source Consulting | Domain Registration