0

I have a variable with, among others, a line that looks like this

iBMC=277

I'm trying to match that like this:

BMC="$(echo "$allnow" | grep iBMC=[0-9] | sed 's/iBMC=//g')"

This makes BMC empty. Here's what it looks like in bash -x:

++ grep 'iBMC=[0-9] '
++ sed s/iBMC=//g
+ BMC=

I also have to match more things, like in this line:

Name="$(echo "$allnow" | grep stringName= | sed 's/stringName=//g')"

But it also turns out empty. Here is $allnow:

QQ1=0999999
QQ2=00aaaaa
RR=yes
GG=no
stringName=skolan8
sTitle=
iBMC=277
L3-cache="8 MB"
Minne="16 GB"    
terdon
  • 242,166
  • 2
    grep 'iBM=[0-9] ' isn't the same as grep iBMC=[0-9]. What did you type? Also, in awk: BMC="$(echo "$allnow" | awk -F = '/iBMC=[0-9]/{print $2}')" – muru Dec 15 '15 at 08:47
  • Also, in sed: BMC="$(echo "$allnow" | sed -n 's/^iBMC=//g;T;P')" – Mel Dec 15 '15 at 09:12
  • @muru That's exactly what I typed though. –  Dec 15 '15 at 10:30
  • 1
    FYI, the code you posted should have worked, even though it could be improved by removing the duplicate regex per line (see my answer). Since your code didn't work, I suspect that the problem is with the setting of the $allnow variable. For instance, if you set allnow from the interactive shell and are trying to access it from a script, you would need to use export (or rethink your strategy). What does echo "$allnow" in the script show? – Wildcard Dec 15 '15 at 10:32
  • @Wildcard the last few lines was the output of "echo "$allnow"". –  Dec 15 '15 at 10:36
  • Run from the script, though? Or from the command line? To put it another way: Where and how is the variable allnow set? Edit: Actually, I just looked again; @muru is right. If bash -x shows you are grepping for 'iBMC=[0-9] ', then that's your problem—you have more than one digit and no space in that line. – Wildcard Dec 15 '15 at 10:39
  • Inside the script, just before where I define $BMC. It looks like this allnow="$(cat "$1" | sed '/Vars/!d' | sed 's/\&/\(newline here) /g' | sed '/STOP=/d' | sed 's/.*\<.*iBMC/iBMC/g' | awk '!a[$0]++')" the (newline here) is a literal newline, it didn't show one in the comments here so I added that. –  Dec 15 '15 at 10:44
  • I'm sorry I asked...but it's more or less what I expected. Free advice: (1) UUOC (google it) (2) Just do it all in awk rather than making a huge pipeline iterating through the file 9+ times to set your variables. Also see http://unix.stackexchange.com/q/169716/135943. But, to make it work as written, find out why you have an extra space showing in the arg to grep; that's your immediate trouble. – Wildcard Dec 15 '15 at 10:48

4 Answers4

1

Don't need to use a grep (maybe for performance issue on huge info, but i don't think this is the case with data stored in a variable)

BMC="$(echo "${allnow}" | sed -e '/iBMC=[0-9]/!d' -e 's/iBMC=//;q' )"
1

I like @NeronLeVelu's answer, but I get suspicious when I see the same or nearly the same regex in two places in a sed script. ;)

Sure enough, it can be simplified:

sed -n 's/iBMC=//;T;/^[0-9]/p'

Or, if using BSD sed where there is no T, you can use the sloppy workaround:

sed -n -e 's/iBMC=//;ta' -e 'b' -e ':a' -e '/^[0-9]/p'

On the other hand, we could just get simple about the whole thing:

sed -n 's/iBMC=\([0-9]\)/\1/p'

which is the exact same length as my first version, but portable to BSD sed.

So wrap this like so:

BMC="$(echo "$allnow" | sed -n 's/^iBMC=\([0-9]\)/\1/p')"
Name="$(echo "$allnow" | sed -n 's/^stringName=//p')"
Wildcard
  • 36,499
1

The "space" after ] wasn't actually a space but  . It looked the same in BBEdit, but after opening it with HexFiend, it showed me that character. There is some shortcut in BBEdit that makes   instead of space.

0
<<IN \
grep '^iBMC=[0-9]\|^stringName=' |\
sed 's/^[[:lower:]]*//'
$allnow
IN

You also might want to check your file for clean line-endings, but I don't know about that.

mikeserv
  • 58,310