在输出流中查找常见字符

时间:2012-09-18 05:56:37

标签: linux unix sed awk grep

我输出的命令如下:

/user/bin/this_text_area_islands[0]
/user/bin/this_text_area_seas[0]
/user/bin/this_text_area_volcanoes[0]
/user/bin/this_text_area_continents[0]

我想管道这个并在输出流中获取常用字符:

/user/bin/this_text_area_[0]

又一个例子:

/user/bin/this_text_area_islands
/user/bin/this_drawing_area_seas
/user/bin/this_paint_area_volcanoes
/user/bin/this_play_area_continents

应输出如下内容:[只匹配所有行中的所有常用字符]

/user/bin/this_area_

1 个答案:

答案 0 :(得分:0)

不确定它是否适用于所有情况,但以下内容适合您的示例:

#! /bin/bash

FIRST=$(head -1 $1)
LNB=$(wc -l < $1)

keepright () {
  M=$FIRST
  while [ -n "$M" ]
  do
    M=${M%?}
    [ $(fgrep -c "$M" "$1") -eq $LNB ] && break
  done
}

keepleft () {
  M=$FIRST
  while [ -n "$M" ]
  do
    M=${M#?}
    [ $(fgrep -c "$M" "$1") -eq $LNB ] && break
  done
}

keepright $1; printf "%s" "$M"
keepleft $1; printf "%s\n" "$M"