linux - shell 脚本 : How to cut part of a string

标签 linux bash shell sed

我有以下字符串

â   â³ eGalax Inc. USB TouchController          id=9    [slave  pointer  (2)]
â   â³ eGalax Inc. USB TouchController          id=10   [slave  pointer  (2)]

并想获得 id 列表?如何使用 sed 或其他工具完成此操作?

最佳答案

我将您示例的内容粘贴到名为 so.txt 的文件中。

$ cat so.txt | awk '{ print $7 }' | cut -f2 -d"="
9
10

解释:

  1. cat so.txt 会将文件的内容打印到 stdout
  2. awk '{ print $7 }' 将打印第七列,即包含 id=n
  3. 的列
  4. cut -f2 -d"=" 将使用 = 作为分隔符剪切步骤 #2 的输出并获取第二列 (-f2)

如果您还想获得 id=,那么:

$ cat so.txt | awk '{ print $7 }' 
id=9
id=10

关于linux - shell 脚本 : How to cut part of a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3742292/

相关文章:

python - 如何通过给定参数使用shell脚本为python程序制作linux命令?

shell - if 语句检查正则表达式不起作用

java - 为什么 Java 应用程序在运行应用程序的多个实例而不是一个实例时占用的总体 CPU 更少?

linux - 在压缩文件上使用 sed

bash - 使用 SSH 在远程服务器上运行命令,但遇到引号问题

bash - 在 while 循环中读取 2 个文件 BASH 脚本

mysql - 如何在 mysql 命令行工具中更改目录?

linux - linux下如何添加posix hooks?

linux - 解决部分链接中的相对重定位

Python 等同于 perl -pe?