regex


Pattern match with stored variable or regular expression in Bash


Since I'm a biologist and do my informatics by patching and editing code form forums like this, I apologise in advance for rookie mistakes.
I've been trying to iterate over files and search pattern in File2, when this pattern is first line in File1. My goal is to append one line after pattern into outFile.
My first attempt was to insert regular expression like this:
awk 'c&&!--c;/"${head -1 "$File1"}"/{c=1}' "$File2" >> outFile
And second to store pattern into variable:
pattern=$(head -1 "$File1")
awk 'c&&!--c;/"$pattern"/{c=1}' "$File2" >> outFile
(File1 and File2 are marked with "$" because I call them with for loop.)
But neither works. If I directly write pattern it works OK, so everything else seems to be fine.
So I'm dealing with sequence data, and after blasting I get list of hits into File1 which looks something like this:
BLAST_hit_Jerry
BLAST_hit_Tom
BLAST_hit_Dog
...
Now Jerry is the best. I would like to extract first line (BLAST hit Jerry) and pattern match it in sequence file (File2), that looks like:
>BLAST_hit_BugsBunny_andsomethingelse
unique_sequence_BugsBunny
>BLAST_hit_MickyMouse_andsomethingelse
unique_sequence_MickyMouse
>BLAST_hit_Jerry_andsomethingelse
unique_sequence_Jerry
...
I have many File1 BLAST hit files and corresponding File2 sequence files that I iterate over and always store into outFile only one unique_sequence that corresponds to top BLAST hit. outFile would look something like:
unique_sequence_Jerry
unique_sequence_from_second_loop_(other sequence corresponding to next top BLAST hit)
unique_sequence ...
Thank you for help!
$pattern isn't expanded inside the single quotes. Pass the value as an awk variable, then use the ~ operator to match the value.
awk -v p="$pattern" 'c&&!--c; $0 ~ p {c=1}' "$File2" >> outFile

Related Links

Regular expression optional match start/end of line
preg_replace with UTF-8 letter
regex just have 2 items [closed]
Regular expression find and replace Notepad ++
regex wont find a match
Selective regex on a word/character
Why doesnt [\n$] work while (\n|$) does?
How to create an array of regex matches
Greedy and non-greedy regex
How can find and replace muiti text at once?
regex for float with 6 decimals and 3 numbers
regex invalid expression nothing to repeat
capture IP addresses only using R
What strings satisfy regex: (^$)|(^.*\{count\}.*$)
Regular Expression - Binary String Matches But Not When Reversed
regex : string who don't have [/img]

Categories

HOME
typescript
document
redirect
hortonworks-data-platform
angular2-forms
copy
tcl
word-vba
datetime
swashbuckle
turing
stripe-payments
c++14
sony
jfreechart
phpmailer
telerik
plotly
google-weather-api
marketplace
aac
jquery-jtable
qualtrics
raspberry-pi2
android-dialogfragment
spring-cloud-consul
pywin32
procedure
git-bash
advantage-database-server
spring-restdocs
man
ibm-datapower
dsc
py4j
mouseevent
core-location
iteration
epicorerp
parallax
meanjs
dapper-fastcrud
udid
swipe
lifecycle
titanium-alloy
jetty-9
qweb
weebly
sdcc
gretl
hostapd
lcov
singlepage
asp.net-mvc-custom-filter
replicate
mysql-notifier
cakephp-2.3
cufft
oracle-policy-automation
cover
vsvim
mutation
android-popupwindow
flexigrid
java-gstreamer
dnx
jedi
uikeyboard
par
appv
oboe.js
protected
tabbar
avalonedit
crowdflower
android-print-framework
alt
mkpolyline
sigkill
kue
iscroll4
lwuit-list
ash
xacml3
hobbitmon
jqgrid-php
dotliquid
windows-phone-7.1
antisamy
sshj
nmea
prudentia
emitmapper
flash-cs4
programmers-notepad
memoization
hresult
ccombobox
capitalize
nsmanagedobjectmodel
impdp
html5-apps
datadesign
hudson-plugins
google-provisioning-api
file-structure
makumba
hiphop
expression-encoder-sdk
payment-services
codi
jboss-cache
gumstix
msn-messenger
variable-names
user-testing

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App