Pratik Dangore
Pratik Dangore

Reputation: 11

Get the data from specific entry

I have below one line entry stored in variable. I want to grep only worker and batch count from below entry and store in another two variables ts=13/12

/202213:07:34:|name=xyz|worker=5|batch=100|conf_file=/path/to/file|data_dir=/path/to/folder|logs_dir=/data/logs/

form above example worker=5 and batch=100 so I want to store 5 in a i.e a=5 and b=100 Note: The length of the entry is not fixed

echo $ENTRY | grep "worker" | cut -d "=" -f4
using above I am getting below output
5|batch

Upvotes: 0

Views: 39

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 185530

$ IFS='|' read _ _  w b _ <<< '/202213:07:34:|name=xyz|worker=5|batch=100|conf_file=/path/to/file|data_dir=/path/to/folder|logs_dir=/data/logs/'
$ echo "${w#*=}"
5
$ echo "${b#*=}"
100

Upvotes: 1

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10133

Using bash's =~ operator:

[[ $entry =~ '|worker='([^|]*) ]] && worker=${BASH_REMATCH[1]}
[[ $entry =~ '|batch='([^|]*)  ]] && batch=${BASH_REMATCH[1]}
echo "$worker/$batch"

Upvotes: 0

Related Questions