有這個 mySQL 轉儲檔案(file.sql),其中包含資料庫的表結構和插入陳述句。
--DO NOT COPY
--DO NOT COPY
-- Table structure for table `address`
--
DROP TABLE IF EXISTS `address`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `address` (
`ENCODEDKEY` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`ADDRESSTYPE` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT
NULL,
`CITY` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `address`
--
LOCK TABLES `address` WRITE;
/*!40000 ALTER TABLE `address` DISABLE KEYS */;
INSERT INTO `address` VALUES ( 'ENCODEDKEY','ADDRESSTYPE','CITY');
/*!40000 ALTER TABLE `address` ENABLE KEYS */;
UNLOCK TABLES;
--DO NOT COPY
--DO NOT COPY
我想從起點到終點的兩點之間復制此 file.sql 中的文本。
起點應該是 = 的確切字串
表 'find_tablename' 的表結構
結束點應該是 = 的確切字串
解鎖表;
我想復制/讀取這兩點之間的所有文本并寫入一個新的 .sql 檔案
這個使用 SED 的 Unix 命令執行以下任務:
sed -n -e '/-- Table structure for table `address`/,/UNLOCK TABLES/p' file.sql > new_file.sql
但是,我正在使用命令列(cmd)尋找等效的 Windows。我開始使用 @type 命令執行此操作,但我需要某種 IF 陳述句來告訴行程僅復制這 2 個點之間的所有文本并寫入新的 .sql 檔案。
@type C:\Users\Documents\file.sql > C:\Users\Documents\new_file.sql
uj5u.com熱心網友回復:
我認為沒有針對這種特定情況的命令。我希望你不要在 cmd.exe 中嘗試這個。
在 PowerShell 中,您可以這樣做:
$write = $false
$(switch -CaseSensitive -File ("file.sql") {
'-- Table structure for table `address`' { $write = $true; $_ }
'UNLOCK TABLES;' { if ($write) { $_; break; } }
default { if ($write) { $_ }}
}) | Set-Content "new_file.sql"
如果您更頻繁地需要這樣的東西,您可以為它撰寫自己的函式。
uj5u.com熱心網友回復:
您也可以在 powershell 中使用正則運算式來獲得類似的結果
$pattern = '(?s)(-- Table structure. UNLOCK TABLES;\r?\n)'
$rawcontent = Get-Content -Path C:\Users\Documents\file.sql -Raw
if($rawcontent -match $pattern){
Set-Content -Path C:\Users\Documents\new_file.sql -Value $matches.1
}
請注意,需要-Rawon 選項Get-Content才能將所有內容收集為單個字串。(?s)是表示單個字串的正則運算式修飾符。
為了便于閱讀,我把命令分開了,你也可以壓縮成
if((Get-Content -Path C:\Users\Documents\file.sql -Raw) -match '(?s)(-- Table structure. UNLOCK TABLES;\r?\n)'){
Set-Content -Path C:\Users\Documents\new_file.sql -Value $matches.1
}
或者
Set-Content -Path C:\Users\Documents\new_file.sql -Value $(if((Get-Content -Path C:\Users\Documents\file.sql -Raw) -match '(?s)(-- Table structure. UNLOCK TABLES;\r?\n)'){$matches.1})
甚至
Set-Content -Path C:\Users\Documents\new_file.sql -Value (Get-Content -Path C:\Users\Documents\file.sql -Raw | Select-String -Pattern '(?s)(-- Table structure. UNLOCK TABLES;\r?\n)').matches.value
uj5u.com熱心網友回復:
如果 sed 削減它??
然后從
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/339018.html
