linux - 如何删除特定模式后的行并提取某些内容

标签 linux bash sed awk

更新 这是我的文件:

<department name="/fighters" id="123879" group="channel" case="none" use="no">
  <options index_name="index.html" listing="0" sum="no"  allowed="no" />
  <target prefix="ttp" suffix=".net" />
  <type="effort">
  <region="20491" readonly="fs1a" readwrite="fs1a" upload="yes" download="yes" repl="yes" hard="0" soft"0" prio="0" write="no" stage="yes" migrate="no" size="0" >
  <read="content" readwrite="content" hard="215822106624" soft="237296943104" prio="5" write="yes"  stage="yes" migrate="no" size="0" />
  <overflow name="20491-set-writable" />
</replicate>
<region="20576" readonly="fs1a" readwrite="fs1a" upload="yes" download="yes" repl="yes" hard="0" soft"0" prio="0" write="no" stage="yes" migrate="no" size="0" >
<read="content" readwrite="content" hard="215822106624" soft="237296943104" prio="5" write="yes"  stage="yes" migrate="no" size="0" />
<overflow name="20576-set-writable" />
</replicate>
</replication>
<user="T:106603" />
<user="T:123879" />
<user="test" />
<user="ele::123456" />
<user="company-temp" />
<user="companymw2" />
<user="bird" />
<user="coding11" />
<user="plazamedia" />
<allow go="123456=abcdefghijklmnopqrstuvwxyz" />
</department>

我写了一个像这样的bash:

awk < test.xml -Fuser= '{ print $2 }' | sed '/^$/d' |  cut -d" " -f1

结果类似于:

"T:106603"
"T:123879"
"test"
"ele::123456"
"company-temp"
"companymw2"
"bird"
"coding11"
"plazamedia"

但想象一下结果是:

"T:106603" />
"T:123879" />
"test" />
"ele::123456" />
"company-temp" />
"companymw2" />
"bird" />
"coding11" />
"plazamedia" />

首先,我怎么能说删除第二个之后的所有内容?

其次,我怎么说提取 "" 之间的所有内容?

我喜欢使用 sedawk

提前谢谢

最佳答案

试试这个:

awk -F'"' '/<user=/{ print $2 }' file

关于linux - 如何删除特定模式后的行并提取某些内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21821287/

相关文章:

linux - 为什么我的 asm 程序会无限运行

c - linux串口编程读取返回垃圾数据

bash 仅在运行时显示输出

linux - 在 cat << 'EOT' 之后使用 sed 仅替换生成的脚本中的一个变量

regex - 重复正则表达式替换为 SED

linux - 提取两个行号之间的文本

c++ - 如何在 Linux 平台的 Visual Studio c++ 中编写 GTK 应用程序?

linux - (宏语言处理器 m4)如何运行 resp。在 Ubuntu 13.4 上编译 .m4 程序?

linux - 使用 bash 脚本输入 ssh 密码,没有那个文件或目录

linux - 我需要创建一个使用 cat 命令的动态文件名的脚本