shell 抽取几个文件的匹配到的多行中某一列内容展示成多列。
时间: 2024-03-27 22:39:26 浏览: 24
可以使用 `grep` 和 `awk` 命令来抽取多个文件中匹配到的多行中某一列内容并展示成多列。假设我们有三个文件 `file1.txt`、`file2.txt`、`file3.txt`,每个文件有三列数据,我们想要匹配到包含 `keyword` 的多行,并抽取它们的第二列内容并展示成多列,可以使用如下命令:
```
grep -r "keyword" file1.txt file2.txt file3.txt | awk '{print $2}' | pr -ts" "
```
其中,`-r` 表示递归查找,`"keyword"` 表示需要匹配的关键字,`$2` 表示输出每行的第二列内容,`pr -ts" "` 表示将输出结果按空格分隔展示成多列。
如果需要将结果保存到一个文件中,可以使用重定向符号 `>`,例如:
```
grep -r "keyword" file1.txt file2.txt file3.txt | awk '{print $2}' | pr -ts" " > output.txt
```
这样会将结果保存到 `output.txt` 文件中。
相关问题
shell 抽取几个文件的匹配到的多行中某一列内容并列展示
可以使用 `grep` 和 `awk` 命令来抽取多个文件中匹配到的多行中某一列内容并列展示。假设我们有三个文件 `file1.txt`、`file2.txt`、`file3.txt`,每个文件有三列数据,我们想要匹配到包含 `keyword` 的多行,并抽取它们的第二列内容并列展示,可以使用如下命令:
```
grep -r "keyword" file1.txt file2.txt file3.txt | awk '{print $2}'
```
其中,`-r` 表示递归查找,`"keyword"` 表示需要匹配的关键字,`$2` 表示输出每行的第二列内容。
如果需要将结果保存到一个文件中,可以使用重定向符号 `>`,例如:
```
grep -r "keyword" file1.txt file2.txt file3.txt | awk '{print $2}' > output.txt
```
这样会将结果保存到 `output.txt` 文件中。
shell 抽取几个文件的匹配到的多行中某一列内容 输出成多行多列
可以使用 `grep` 和 `awk` 命令来抽取多个文件中匹配到的多行中某一列内容并输出成多行多列的格式。假设我们有三个文件 `file1.txt`、`file2.txt`、`file3.txt`,每个文件有三列数据,我们想要匹配到包含 `keyword` 的多行,并抽取它们的第二列内容并输出成多行多列的格式,可以使用如下命令:
```
grep -r "keyword" file1.txt file2.txt file3.txt | awk '{print $2}' | xargs -n3
```
其中,`-r` 表示递归查找,`"keyword"` 表示需要匹配的关键字,`$2` 表示输出每行的第二列内容,`xargs -n3` 表示将输出按每3列分隔成多行多列的格式。
如果需要将结果保存到一个文件中,可以使用重定向符号 `>`,例如:
```
grep -r "keyword" file1.txt file2.txt file3.txt | awk '{print $2}' | xargs -n3 > output.txt
```
这样会将结果保存到 `output.txt` 文件中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)