如何在unix中的文件中匹配和删除它之前的内容

时间:2015-06-16 15:11:25

标签: mysql perl sed solaris

我有一个mysql转储文件,我希望在&#34之后删除文件的内容; - 查看view_oss_user"的最终视图结构使用sed / perl。 输入文件是这样的: 内容:

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 */;

    --

2 个答案:

答案 0 :(得分:2)

使用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'

答案 1 :(得分:0)

#!/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 */;
',
          '
',
          '--
'
        ];