0

我有一个命令的输出,如下所示:

/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_
4

1 回答 1

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"
于 2012-09-18T13:37:47.423 回答