我有一堆我最近搜索过的PHP代码,SQL语法也有很多不同之处。我一直在经历它并修复它以符合可读性。
我使用的当前命令是:
sed -i 's/select/SELECT/g;s/from/FROM/g;s/where/WHERE/g;s/limit/LIMIT/g;s/and/AND/g' /master/dashboard.php
然而问题是这些单词中的一些弹出html表单错误消息等,而且我正在节省修复这些错误的时间。我宁愿做得更聪明,也更努力。
假设每个mysql的格式都类似于
mysqli_query($mysqli_con,"select * from contacts where username='$username' and password='$password' and active='1'");
如何让sed的正则表达式搜索首先在其中执行替换的查询部分?
高级冰雹玛丽我会永远爱你的问题:如果我可以让正则表达式使用查询作为替换区域,可以输入/管道来自一个单词列表要包含在`characters?
中的文件即:我有一个包含联系人,用户名,密码,公司等的文件;和sed - > `contacts`,`username`,`password`,`etc`。
示例文件输入剪辑:
session_start();
$username = preg_replace("%[^-_.0-9A-Za-z@]%","",$_SESSION['username']);
$password = preg_replace("%[^-! _.0-9A-Za-z@]%","",$_SESSION['password']);
$date = date("r");
$ip = $REMOTE_ADDR;
$query = mysqli_query($mysqli_con,"select * from contacts where username='$username' and password='$password' and active='1'");
$data = mysqli_fetch_array($query);
if((mysqli_num_rows($query) == "0"))
{
$_SESSION['message'] = "<font color='red'><b>" . _("Invalid login") . ":</b> " . _("The username and password you entered are not valid.") . "</font>";
header('Location: /index.php');
}
else
{
$_SESSION['username'] = $data['username'];
$_SESSION['password'] = $data['password'];
$_SESSION['accountid'] = $data['accountid'];
$_SESSION['role'] = $data['role'];
$getCompany = mysqli_query($mysqli_con,"select * from companies where companyid='" . $data['accountid'] . "'");
$dataCompany = mysqli_fetch_array($getCompany);
include('/master/html/header.inc');
?>
<script type="text/javascript">
$(document).ready(function()
{
resizeDashboard();
$(window).resize(function() {
答案 0 :(得分:1)
假设您的查询停留在一行,您可以限制sed只查找这些行:
/mysqli_query/
第一部分sed -i -E '/mysqli_query/s/\b(select|from|where|limit|and)\b/\U\1/g' /master/dashboard.php
是address,只有匹配的行会受到substitution command的影响。
您也可以用一个替换所有sed命令:
\U
\1
告诉sed使用大写字母直到另行通知为止; sed
是搜索括号中捕获的值的backreference。
案例更改为a GNU extension,如果您正在使用BSD sed(例如在macOS上)或其他一些异常DROP DATABASE IF EXISTS DasBus;
CREATE DATABASE DasBus;
use DasBus;
BEGIN;
CREATE TABLE Path (
PATH_NUM VARCHAR(10) PRIMARY KEY,
START_POINT VARCHAR(10) NOT NULL,
END_POINT VARCHAR(10) NOT NULL,
INTER DECIMAL(8,2) NOT NULL,
TOT_DURATION TIME NOT NULL,
PATH_DELAY_NUM VARCHAR(10) NOT NULL,
BUS_NUM VARCHAR(10) NOT NULL,
PATH_STOP_NUM VARCHAR(10) NOT NULL,
SCHED_NUM VARCHAR(10) not null
);
CREATE TABLE Path_Delay (
PATH_DELAY_NUM VARCHAR(10) PRIMARY KEY,
ISDELAYED BOOLEAN NOT NULL DEFAULT 0,
PATH_NUM VARCHAR(10) NOT NULL,
DELAY_NUM VARCHAR(10) NOT NULL
);
CREATE TABLE Delay (
DELAY_NUM VARCHAR(10) PRIMARY KEY,
DELAY_TIME INTEGER NOT NULL DEFAULT 0,
DESCRIPTION VARCHAR(100) NOT NULL,
PATH_DELAY_NUM VARCHAR(10) NOT NULL
);
CREATE TABLE Path_Stop (
PATH_STOP_NUM VARCHAR(10) PRIMARY KEY,
PATH_NUM VARCHAR(10) NOT NULL,
STOP_NUM VARCHAR(10) NOT NULL,
TIMEBETWEEN INTEGER NOT NULL
);
CREATE TABLE BusStop (
STOP_NUM VARCHAR(10) PRIMARY KEY,
STOP_NAME VARCHAR(100) NOT NULL,
ISACTIVE BOOLEAN NOT NULL DEFAULT 1,
STOPCONDITION VARCHAR(100) NOT NULL,
PATH_STOP_NUM VARCHAR(10) NOT NULL,
SCHED_NUM VARCHAR(10) not null
);
CREATE TABLE BusSchedule (
SCHED_NUM VARCHAR(10) PRIMARY KEY,
INTER TIME NOT NULL,
START_TIME TIME NOT NULL,
END_TIME TIME NOT NULL,
PATH_NUM VARCHAR(10) NOT NULL,
STOP_NUM VARCHAR(10) NOT NULL
);
CREATE TABLE Bus (
BUS_NUM VARCHAR(10) PRIMARY KEY,
BUS_CAPACITY INTEGER(4),
DRIVER_NUM VARCHAR(10) NOT NULL,
PATH_NUM VARCHAR(10) NOT NULL
);
CREATE TABLE Driver (
DRIVER_NUM VARCHAR(10) PRIMARY KEY,
DRIVER_LNAME VARCHAR(25) NOT NULL,
DRIVER_FNAME VARCHAR(25) NOT NULL,
DRIVER_STREET VARCHAR(25) NOT NULL,
DRIVER_CITY VARCHAR(25) NOT NULL,
DRIVER_ZIP CHAR(6),
DRIVER_PHONE VARCHAR(15),
BUS_NUM VARCHAR(10) NOT NULL
);
<p>ALTER TABLE PATH ADD CONSTRAINT Path1 FOREIGN KEY (BUS_NUM) REFERENCES Bus(BUS_NUM);
<p>ALTER TABLE PATH ADD CONSTRAINT Path2 FOREIGN KEY (PATH_DELAY_NUM) REFERENCES Path_Delay (PATH_DELAY_NUM);
<p>ALTER TABLE PATH ADD CONSTRAINT Path3 FOREIGN KEY (PATH_STOP_NUM) REFERENCES Path_Stop(PATH_STOP_NUM);
<p>ALTER TABLE PATH ADD CONSTRAINT Path4 FOREIGN KEY (SCHED_NUM) REFERENCES BusSchedule(SCHED_NUM);
<p>ALTER TABLE Path_Delay ADD CONSTRAINT Path_Delay1 FOREIGN KEY (PATH_NUM) REFERENCES Path(PATH_NUM);
<p>ALTER TABLE Path_Delay ADD CONSTRAINT Path_Delay2 FOREIGN KEY (DELAY_NUM) REFERENCES Delay(DELAY_NUM);
<p>ALTER TABLE DELAY ADD CONSTRAINT Delay1 FOREIGN KEY (PATH_DELAY_NUM) REFERENCES Path_Delay(PATH_DELAY_NUM);
<p>ALTER TABLE BusStop ADD CONSTRAINT BusStop1 FOREIGN KEY (PATH_STOP_NUM) REFERENCES Path_Stop(PATH_STOP_NUM);
<p>ALTER TABLE BusStop ADD CONSTRAINT BusStop2 FOREIGN KEY (SCHED_NUM) REFERENCES BusSchedule(SCHED_NUM);
<p>ALTER TABLE BUS ADD CONSTRAINT Bus1 FOREIGN KEY (DRIVER_NUM) REFERENCES Driver(DRIVER_NUM);
<p>ALTER TABLE BUS ADD CONSTRAINT Bus2 FOREIGN KEY (PATH_NUM) REFERENCES Path(PATH_NUM);
<p>ALTER TABLE DRIVER ADD CONSTRAINT Driver1 FOREIGN KEY (BUS_NUM) REFERENCES Bus(BUS_NUM);
<p>ALTER TABLE Path_Stop ADD CONSTRAINT Path_Stop1 FOREIGN KEY (PATH_NUM) REFERENCES Path(PATH_NUM);
<p>ALTER TABLE Path_Stop ADD CONSTRAINT Path_Stop2 FOREIGN KEY (STOP_NUM) REFERENCES BusStop(STOP_NUM);
<p>ALTER TABLE BusSchedule ADD CONSTRAINT BusSchedule1 FOREIGN KEY (STOP_NUM) REFERENCES BusStop (STOP_NUM);
<p>ALTER TABLE BusSchedule ADD CONSTRAINT BusSchedule2 FOREIGN KEY (PATH_NUM) REFERENCES Path (PATH_NUM);
,则无法使用。