I have a script that is trying to get blocks of information from gparted.
My Data looks like:
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 316MB 315MB primary ext4 boot
2 316MB 38.7GB 38.4GB primary ext4
3 38.7GB 42.9GB 4228MB primary linux-swap(v1)
log4net.xml
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 316MB 315MB primary ext4 boot
5 316MB 38.7GB 38.4GB primary ext4
6 38.7GB 42.9GB 4228MB primary linux-swap(v1)
I use a regex to break this into two Disk blocks
^Disk (/dev[\S]+):((?!Disk)[\s\S])*
This works with multiline on.
When I test this in a bash script, I can't seem to match \s
, or \S
-- What am I doing wrong?
I am testing this through a script like:
data=`cat disks.txt`
morematches=1
x=0
regex="^Disk (/dev[\S]+):((?!Disk)[\s\S])*"
if [[ $data =~ $regex ]]; then
echo "Matched"
while [ $morematches == 1 ]; do
x=$[x+1]
if [[ ${BASH_REMATCH[x]} != "" ]]; then
echo $x "matched" ${BASH_REMATCH[x]}
else
echo $x "Did not match"
morematches=0
fi
done
fi
However, when I walk through testing parts of the regex, whenever I match a \s
or \S
, it doesn't work -- what am I doing wrong?
\s
and\S
are PCRE extensions; they are not present in the ERE (Posix Extended Regular Expression) standard. Just be glad you aren't trying to use BRE. – Upanchorx=$[x+1]
is an antique syntax;((x++))
is the modern bash version, orx=$((x + 1))
the modern POSIX version. Using==
inside of[ ]
is not POSIX-compliant; either use[[ ]]
(which doesn't try to be POSIX compliant, and allows you to not quote by virtue of having parse-time rules that turn off string-splitting) or use=
instead of==
(and make it[ "$morematches" = 1 ]
, WITH THE QUOTES!). Always quote your expansions:echo "$x did not match"
; otherwise, globs inside of$x
are expanded and runs of whitespace compressed. – Upanchor/dev/xyz matched 4.9GB
? – Intrude