简体   繁体   English

使用“sed命令”在多输出文件中使用“F服务..”启动打印行

[英]print line started by “F served..” in a multiple output file using “sed command”

I developed a script using sed to retrieve lines starting by text F servedM.. from a given multilines file : 我使用sed开发了一个脚本来从给定的多行文件中检索以文本F servedM..开头的行:

  • I have an input.txt file of 100000 line 我有一个100000行的input.txt文件
  • I want to generate an output.txt containing all line starting by F servedMyd and all lines starting by F servedPuid . 我想生成一个output.txt包含由开始的所有行F servedMyd并启动所有线路F servedPuid

I am using ksh on Red Hat 7.4. 我在Red Hat 7.4上使用ksh。

Below I give the code I tried. 下面我给出了我试过的代码。

#!/bin/ksh
input.txt=$1
while read line
do
sed "/^F servedMyd/p" $input.txt >> output.txt
sed "/^F servedPuid/p" $input.txt >> output.txt
done < $input.txt
exit 0

With this code, I didn't get any output. 有了这段代码,我没有得到任何输出。 I expected having an output file with the following structure: 我希望有一个具有以下结构的输出文件:

在此输入图像描述

A script is unnecessary, a single line of sed should be sufficient: 脚本是不必要的,单行sed就足够了:

sed -n '/^F served\(My\|Pui\)d/p' foo > bar

Or grep : 或者grep

grep '^F served\(My\|Pui\)d' foo > bar

Or: 要么:

grep -E '^F served(My|Pui)d' foo > bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM