我试图在同一行中搜索不同的图案,然后以这种方式打印它们。
这是文本:
`voices` VALUES (409663,1957,2594,'mech Brands LC - BO GB6: pic - LST September 2017',NULL,1,0,'2017-09-24 07:35:18',NULL,NULL,1957,0,0,NULL,'2017-09-24 07:35:18');
`others` VALUES (409664,1957,2595,'mech Brands LC - BO NZ6: pic - LST September 2017',NULL,1,0,'2017-09-24 07:35:18',NULL,NULL,1957,0,0,NULL,'2017-09-24 07:35:18');
`voices` VALUES (409684,628,2622,'Pic Productions Com - pic: perV things September 2017',NULL,1,0,'2017-09-24 07:35:22',NULL,NULL,628,0,0,NULL,'2017-09-24 07:35:22');
我想这样输出:
`voices` - mech Brands LC
`others` - mech Brands LC
`voices` - Pic Productions Com
这是我尝试过的方法,但它以不同的行显示输出
cat file.txt | grep -Pzo "mech Brands LC|Pic Productions Com|\`.*\`"
`invoices`
mech Brands LC
`invoices`
mech Brands LC
`invoices`
Pic Productions Com
有人可以帮我吗?
谢谢!
答案 0 :(得分:1)
我建议为此使用company_slug = LowerSlugField(max_length=255, unique=True)
sed
或者,使用POSIX ERE语法
sed 's/^\(`[^`]*`\).*\(mech Brands LC\|Pic Productions Com\).*/\1 - \2/' file.txt > newfile.txt
详细信息
sed -E 's/^(`[^`]*`).*(mech Brands LC|Pic Productions Com).*/\1 - \2/' file.txt > newfile.txt
-字符串的开头^
-第1组:反引号,除反引号和反引号外的0+个字符(`[^`]*`)
-任意0个以上的字符.*
-第2组:(mech Brands LC|Pic Productions Com)
或mech Brands LC
子字符串Pic Productions Com
-任意0个以上的字符.*
-将匹配项替换为组1 \1 - \2
和组2值。