Reputation: 1712
I get inputs of the form <an arbitrary number of words> <a list of ip addresses comma separated> <timestamp>
.
I tried to get ip_list=$(echo $i | sed 's/\([0-9\.,]\)+/\1/')
, where $i
is a line on the previously described form, but that matches the whole line for some reason.
The list of IPs does not have spaces in the middle, just commas.
I would like to get the ip list (as a block) and the timestamp in two bash variables.
Example input
asdf asaskf a aghjjsdf 127.0.0.1,10.0.1.1,8.8.8.8,124.125.163.124 2013-11-22 08:38:12
Output
ip_variable="127.0.0.1,10.0.1.1,8.8.8.8,124.125.163.124"
date_variable="2013-11-22 08:38:12"
Upvotes: 0
Views: 107
Reputation: 786091
This is much easier with grep -P (PCRE)
:
> s='asdf asaskf a aghjjsdf 127.0.0.1,10.0.1.1,8.8.8.8,124.125.163.124 2013-11-22 08:38:12'
> arr=( $(echo "$s"| grep -oP '(\d+\.){3}\d') )
> set | grep arr
arr=([0]="127.0.0.1" [1]="10.0.1.1" [2]="8.8.8.8" [3]="124.125.163.1")
> ts=$(echo "$s"| grep -oP '\d{4}-\d{1,2}-\d{1,2} .*')
> echo "$ts"
2013-11-22 08:38:12
Upvotes: 0
Reputation: 10039
echo "${i}" | sed 's/^.*[[:blank:]]\(\(\([012][0-9]\{1,2\}.\)\{3\}[012][0-9]\{1,2\}[.[:blank:]]\)\{1,\}\)[[:blank:]]*\(.*\)$/\1 \4/' | read ip_variable date_variable
failed if ip are not separate only by ,
Upvotes: 0
Reputation: 23394
Using bash (under default IFS)
read -a arr <<<'asdf asaskf a aghjjsdf 127.0.0.1,10.0.1.1,8.8.8.8,124.125.163.124 2013-11-22 08:38:12'
ip_variable="${arr[-3]}"
echo $ip_variable
127.0.0.1,10.0.1.1,8.8.8.8,124.125.163.124
date_variable="${arr[*]:(-2)}"
echo $date_variable
2013-11-22 08:38:12
Upvotes: 3