regex


How to use sed to identify a string in brackets?


I want to find the string in that is placed with in the brackets. How do I use sed to pull the string?
# cat /sys/block/sdb/queue/scheduler
noop anticipatory deadline [cfq]
I'm not getting the exact result
# cat /sys/block/sdb/queue/scheduler | sed 's/\[*\]//'
noop anticipatory deadline [cfq
I'm expecting an output
cfq
It can be easier with grep, if it happens to be changing the position in which the text in between brackets is located:
$ grep -Po '(?<=\[)[^]]*' file
cfq
This is look-behind: whenever you find a string [, start fetching all the characters up to a ].
See another example:
$ cat a
noop anticipatory deadline [cfq]
hello this [is something] we want to [enclose] yeah
$ grep -Po '(?<=\[)[^]]*' a
cfq
is something
enclose
You can also use awk for this, in case it is always in the same position:
$ awk -F[][] '{print $2}' file
cfq
It is setting the field separators as [ and ]. And from that, prints the second one.
And with sed:
$ sed 's/[^[]*\[\([^]]*\).*/\1/g' file
cfq
It is a bit messy, but basically it is looking from the block of text in between [] and prints it back.
I found one possible solution-
cut -d "[" -f2 | cut -d "]" -f1
so the exact solution is
# cat /sys/block/sdb/queue/scheduler | cut -d "[" -f2 | cut -d "]" -f1
Another potential solution is awk:
s='noop anticipatory deadline [cfq]'
awk -F'[][]' '{print $2}' <<< "$s"
cfq
Another way by gnu grep :
grep -Po "\[\K[^]]*" file
with pure shell:
while read line; do [[ "$line" =~ \[([^]]*)\] ]] && echo "${BASH_REMATCH[1]}"; done < file
Another awk
echo 'noop anticipatory deadline [cfq]' | awk '{gsub(/.*\[|\].*/,x)}8'
cfq
perl -lne 'print $1 if(/\[([^\]]*)\]/)'
Tested here

Related Links

Regexp to selectively match email
Regex match two strings but not third (in random order)
Test but not select with regex
Pattern to delete lines causes the whole contents of the file to be deleted in Sed
Using a regex in a conditional vs. an outright awk/sed?
Regex for a valid month
Remove everything except a certain pattern
Hubot Not Responding to regex
RegExp to find first letter of words before #
Creating Index based on another field in logstash
Bug in R's grepl? [closed]
Regular Expression Google Form only numbers in a range
MS Word VBA macro to search and replace (Regex)
Repeat capture group ignoring whitespaces
regex help in notepad++ marking embedded values and copying into a new list
uWSGI routing rule is overriding static files

Categories

HOME
elastic-beanstalk
typescript
auth0
typeahead.js
saml
format
spi
delphi-xe7
google-analytics-api
python-xarray
zipfile
intl
axure
oracle-agile-plm
googletest
calabash-android
apex
export-to-excel
jquery-selectors
oracle11gr2
appcelerator-alloy
google-distancematrix-api
datamatrix
aspdotnetstorefront
jsgrid
background-subtraction
padding
point-cloud-library
operator-keyword
ruamel.yaml
dapper-fastcrud
jsplumb
angular2-databinding
ollydbg
tweenmax
capstone
jformattedtextfield
outlook-vba
zfs
autofocus
simplesamlphp
domdocument
android-gps
xbrl
htc
jce
haste
jcuda
omniauth
assembly-references
mapr
vs-2015-preview
sizing
itamae
tealeaf
visual-build-professional
4d
pager
geneos
oracle-policy-automation
docklight
id3
balana
dbfit
sejda
gulp-protractor
multiscreen
sql-server-ce-3.5
odoo
emberfire
isql
modern-ui
servlet-listeners
razor-2
imaging
knockout-mvc
yourls
magic-unipaas
android-lru-cache
modx-evolution
transport-stream
ioerror
shift
datasift-python
datapump
qdebug
jquery-data
geometry-surface
mosek
hogan.js
jcycle
cocosbuilder
makumba
tipsy
letter-spacing
consistent-hashing
bapi
junitperf
closest
blackberry-jde
alivepdf
syncml
project-settings
greensoftware

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