特殊字符

时间:2017-09-27 08:47:16

标签: linux shell ksh

我有一个主文件如下:

/* ------------- AAAAAAAA ------------- */
some
lines 
here
/* ------------- BBBBBBBB ------------- */
more
things
/* ------------- CCCCCCCC ------------- */
there
a 
few
more
lines

我的最终目标是创建一个仅包含特定字符串的块的文件,例如,如果该字符串为lines,那么我将有一个这样的输出文件:

/* ------------- AAAAAAAA ------------- */
some
lines 
here
/* ------------- CCCCCCCC ------------- */
there
a 
few
more 
lines

为了实现我的目标,我首先尝试通过bock将我的主文件拆分为子文件以获得类似

的内容
  • 文件-1
  • 文件-2
  • 文件-3

然后我计划检查每个文件,然后是否包含搜索到的字符串,然后将它们追加到我的新主文件中。

我不知道这是否是最诚实的方法,而且我的主文件中有30139行超过1600个块,所以要解析很多。

但是,如果我按照这种方式完成工作,我的代码仍然存在问题:

#!/bin/ksh
i=0
while IFS=\| read -r "line"; do
        if [ `echo $line | grep '/* ------' | wc -l` -eq 1 ]; then
                i=$((i+1))
        fi
        echo $line > "file-$i"
done < $1

由于每个块由/* --------分隔,如果我执行echo $line,则输出将是我的根目录(/etc/tmp等)而不是{ {1}}本身。

所以我知道这是一个2个问题的帖子,但是因为第二个问题可以通过不同的方式使用脚本来绕过它,它肯定是链接的。

编辑:

解决方案必须在korn shell中,因为我无法在这台机器上安装任何东西

4 个答案:

答案 0 :(得分:1)

如果您不介意使用 Perl ,那么有一个很好的单行可以让您的成就变得轻松。

你唯一需要的是添加这样的一行:

/* ------------- END ------------- */

在文件的最后。这就成了这个:

/* ------------- AAAAAAAA ------------- */
some
lines 
here
/* ------------- BBBBBBBB ------------- */
more
things
/* ------------- CCCCCCCC ------------- */
there
a 
few
more
lines
/* ------------- END ------------- */

现在借助此模式:

\/\*.*?(?=\/\*)

您可以单独匹配每个部分。例如这部分:

/* ------------- AAAAAAAA ------------- */
some
lines 
here

因此,如果您将结果存储在最后的数组中,那么您将拥有一个包含 3 部分的数组。最终,您可以在每个部分申请lines。如果找到了,那么将打印该部分。

单行

perl -ne 'BEGIN{$/=undef;}push(@arr,$&) while/\/\*.*?(?=\/\*)/smg;END{for (@arr){print if /lines/g }}' file

,输出为:

/* ------------- AAAAAAAA ------------- */
some
lines 
here
/* ------------- CCCCCCCC ------------- */
there
a 
few
more
lines

如果您申请more

/* ------------- BBBBBBBB ------------- */
more
things
/* ------------- CCCCCCCC ------------- */
there
a 
few
more
lines

基于@batMan解决方案

命令行解决方案:

tr '\n' ';' < file | grep -Po '\/\*.*?(?=\/\*)' | grep lines | tr ';' '\n'

其输出:

/* ------------- AAAAAAAA ------------- */
some
lines 
here

/* ------------- CCCCCCCC ------------- */
there
a 
few
more
lines

答案 1 :(得分:1)

awk中的另一个:

$ awk '
function dump() {         # define a function to avoid duplicate code in END
    if(b~/lines/)         # if buffer has "lines" in it
        print b           # output and ...
    b="" }                # reset buffer
/^\/\*/ { dump() }        # at the start of a new block dump existing buffer
{ b=b (b==""?"":ORS) $0 } # gather buffer
END{ dump() }             # dump the last buffer also
' file
/* ------------- AAAAAAAA ------------- */
some
lines 
here
/* ------------- CCCCCCCC ------------- */
there
a 
few
more
lines

答案 2 :(得分:0)

使用 awk

awk -v RS="/[*]" '/lines/{printf "/*"$0}' file

输出:

/* ------------- AAAAAAAA ------------- */
some
lines
here
/* ------------- CCCCCCCC ------------- */
there
a
few
more
lines

答案 3 :(得分:0)

如果您真的想使用while read构造,请尝试避免其他文件和系统调用。

matched=0
all=
while IFS= read -r line; do
  if [[ ${line} =~ "/* ----"* ]]; then
      if [ ${matched} -eq 1 ]; then
         printf "%s\n" "${all}"
      fi
      all=
      matched=0
  fi
  all="${all}${line}
"
  if [[ "${line}" =~ line ]]; then
    matched=1
  fi
done < <(cat mainfile; echo "/* ---- The End --- */" )
相关问题