如何从unix中的文件中匹配和删除它前面的内容
How to match and remove the content preceding it from a file in unix
我有一个 mysql 转储文件,我想使用 sed/perl 删除“-- 视图 view_oss_user
的最终视图结构”之后的文件内容。
输入文件是这样的:
内容:
rom `target` */;
/*!50001 SET character_set_client = @saved_cs_client */;
/*!50001 SET character_set_results = @saved_cs_results */;
/*!50001 SET collation_connection = @saved_col_connection */;
--
-- Final view structure for view `view_oss_user`
--
/*!50001 DROP VIEW IF EXISTS `view_oss_user`*/;
/*!50001 SET @saved_cs_client = @@character_set_client */;
/*!50001 SET @saved_cs_results = @@character_set_results */;
/*!50001 SET @saved_col_connection = @@collation_connection */;
/*!50001 SET character_set_client = latin1 */;
/*!50001 SET character_set_results = latin1 */;
输出应该如下所示:
rom `target` */;
/*!50001 SET character_set_client = @saved_cs_client */;
/*!50001 SET character_set_results = @saved_cs_results */;
/*!50001 SET collation_connection = @saved_col_connection */;
--
使用 GNU sed
sed -n '1,/-- Final view structure for view `view_oss_user`/p'
这将打印从 1 到找到模式的行,其他不会打印
或者,如果您想排除模式行,则
sed -n '1,/-- Final view structure for view `view_oss_user`/p' | sed '$d'
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @new_dump;
my $flag = 0;
while(<DATA>)
{
if ( m/-- Final view structure for view `view_oss_user`/ )
{
$flag++;
}
push @new_dump, $_ unless $flag > 0;
}
print Dumper( \@new_dump );
__DATA__
rom `target` */;
/*!50001 SET character_set_client = @saved_cs_client */;
/*!50001 SET character_set_results = @saved_cs_results */;
/*!50001 SET collation_connection = @saved_col_connection */;
--
-- Final view structure for view `view_oss_user`
--
/*!50001 DROP VIEW IF EXISTS `view_oss_user`*/;
/*!50001 SET @saved_cs_client = @@character_set_client */;
/*!50001 SET @saved_cs_results = @@character_set_results */;
/*!50001 SET @saved_col_connection = @@collation_connection */;
/*!50001 SET character_set_client = latin1 */;
/*!50001 SET character_set_results = latin1 */;
运行它:
./perl
$VAR1 = [
'rom `target` */;
',
'/*!50001 SET character_set_client = @saved_cs_client */;
',
'/*!50001 SET character_set_results = @saved_cs_results */;
',
'/*!50001 SET collation_connection = @saved_col_connection */;
',
'
',
'--
'
];
我有一个 mysql 转储文件,我想使用 sed/perl 删除“-- 视图 view_oss_user
的最终视图结构”之后的文件内容。
输入文件是这样的:
内容:
rom `target` */;
/*!50001 SET character_set_client = @saved_cs_client */;
/*!50001 SET character_set_results = @saved_cs_results */;
/*!50001 SET collation_connection = @saved_col_connection */;
--
-- Final view structure for view `view_oss_user`
--
/*!50001 DROP VIEW IF EXISTS `view_oss_user`*/;
/*!50001 SET @saved_cs_client = @@character_set_client */;
/*!50001 SET @saved_cs_results = @@character_set_results */;
/*!50001 SET @saved_col_connection = @@collation_connection */;
/*!50001 SET character_set_client = latin1 */;
/*!50001 SET character_set_results = latin1 */;
输出应该如下所示:
rom `target` */;
/*!50001 SET character_set_client = @saved_cs_client */;
/*!50001 SET character_set_results = @saved_cs_results */;
/*!50001 SET collation_connection = @saved_col_connection */;
--
使用 GNU sed
sed -n '1,/-- Final view structure for view `view_oss_user`/p'
这将打印从 1 到找到模式的行,其他不会打印
或者,如果您想排除模式行,则
sed -n '1,/-- Final view structure for view `view_oss_user`/p' | sed '$d'
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @new_dump;
my $flag = 0;
while(<DATA>)
{
if ( m/-- Final view structure for view `view_oss_user`/ )
{
$flag++;
}
push @new_dump, $_ unless $flag > 0;
}
print Dumper( \@new_dump );
__DATA__
rom `target` */;
/*!50001 SET character_set_client = @saved_cs_client */;
/*!50001 SET character_set_results = @saved_cs_results */;
/*!50001 SET collation_connection = @saved_col_connection */;
--
-- Final view structure for view `view_oss_user`
--
/*!50001 DROP VIEW IF EXISTS `view_oss_user`*/;
/*!50001 SET @saved_cs_client = @@character_set_client */;
/*!50001 SET @saved_cs_results = @@character_set_results */;
/*!50001 SET @saved_col_connection = @@collation_connection */;
/*!50001 SET character_set_client = latin1 */;
/*!50001 SET character_set_results = latin1 */;
运行它:
./perl
$VAR1 = [
'rom `target` */;
',
'/*!50001 SET character_set_client = @saved_cs_client */;
',
'/*!50001 SET character_set_results = @saved_cs_results */;
',
'/*!50001 SET collation_connection = @saved_col_connection */;
',
'
',
'--
'
];