Home > OS >  Get the data from specific entry
Get the data from specific entry

Time:12-14

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

CodePudding user response:

$ 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"
worker=5
$ echo "$b"
batch=100

CodePudding user response:

Using bash's =~ operator:

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