-2

I need to get the tail count from flat file. The tail row will look like below:

"TC=10"

I have used:

cnt=${trcnt#*=}
cnt=${cnt%%,*}

but I am getting cnt = "10".

Please tell the right command to get the count 10.

I need to extract 10 from "TC=10"

Asensi
  • 159
  • 7

2 Answers2

0

You can use awk, and its -F option to define a "field separator":

| awk -F'=' '{print $2}' | awk -F'"' '{print $1}'

which is easier to understand than other alternatives, and very customizable. It's useful for two similar entries:

echo "TC=10" | awk -F'=' '{print $2}' | awk -F'"' '{print $1}'
echo "\"TC=10\"" | awk -F'=' '{print $2}' | awk -F'"' '{print $1}'

Another option, related to what @shellter wrote:

@oguzismail : your self deleted answer looks right to me.

that answer was:

    $ trcnt='"TC=10"'
    $ cnt=${trcnt#*=} # remove everything till first =
    $ cnt=${cnt%\"}   # remove trailing "
    $ echo "$cnt"
    10
Asensi
  • 159
  • 7
0

It is a better practice to match the number over removing what is not the number:

echo $trcnt | gawk '{match($0, /[0-9]+/); print substr($0, RSTART, RLENGTH)}'

or How to extract a number from a string using Bash example

Eran Ben-Natan
  • 2,515
  • 2
  • 16
  • 19