我有一个文件:
$ cat dd.txt
*************************** 1. row ***************************
File: log-bin.00005
Position: 120614936
Binlog_Do_DB:
Binlog_Ignore_DB:
我只想要来自文件log-bin.000055, 120614936
现在我想在我的奴隶表中更新file,position
。
update db.position set binlog_file='log-bin.000055',position='120614936' where master_host='master.tb.com';
如何为它编写简单的脚本?
答案 0 :(得分:2)
使用GNU Grep
和positive lookbehind:
$ grep -Po "(?<=File: )[^ ]+|(?<=Position: )[0-9]+" dd.txt
log-bin.00005
120614936
# Store file in variable
$ file=$(grep -Po "(?<=File: )[^ ]+" dd.txt)
# Store position in variable
$ pos=$(grep -Po "(?<=Position: )[0-9]+" dd.txt)
# Generate query using variable and store in variable
$ query="update db.position set binlog_file='"$file"',position='$pos' where master_host='master.tb.com';"
$ echo $query
update db.position set binlog_file='log-bin.00005',position='120614936' where master_host='master.tb.com';
# Pipe query to mysql
$ echo $query | mysql -u user -ppassword
作为剧本:
#!/bin/bash
file=$(grep -Po "(?<=File: )[^ ]+" dd.txt)
pos=$(grep -Po "(?<=Position: )[0-9]+" dd.txt)
$ query="update db.position set binlog_file='"$file"',position='$pos' where master_host='master.tb.com';"
$ echo $query | mysql -u user -ppassword
答案 1 :(得分:1)
要获取指定的数据,您可以执行以下操作:
cat dd.txt | egrep "File|Position" | cut -d: -f2