linux - 查找输出流中的常见字符

标签 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_

最佳答案

不确定它在所有情况下都有效,但以下内容适合您的示例:

#! /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"

关于linux - 查找输出流中的常见字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12471154/

相关文章:

bash - 删除字符串最后一个大小写之前的所有行

shell - 删除文本文件中不包含字符串的所有行

linux - Linux 阻塞 I/O 实际上是如何工作的?

php - 在 pdf 上生成未找到图像或键入未知 barryvdh\dompdf 错误

Linux 按顺序显示文件

c - 如何编译ELF二进制文件,使其可以作为动态库加载?

linux - unix - 文件的头部和尾部

linux - 在 Linux 上安装 Jenkins,执行 shell 命令给出权限被拒绝。

text - 使用大量替换对在文本文件中执行大量替换

linux - bash 将每隔一个逗号更改为指向