这是我的样本文件
user@linux:~$ cat file.txt
Line 1
Line 2
Line 3
Line 4
Line 5
user@linux:~$
我可以用grep -A2 'e 2' file.txt打印第2-4行。
user@linux:~$ grep -A2 'e 2' file.txt
Line 2
Line 3
Line 4
user@linux:~$
我也可以用grep -n打印出行号。
user@linux:~$ grep -nA2 'e 2' file.txt
2:Line 2
3-Line 3
4
如何使用sed来查找linux这个词的行?稍后会用linux这个词显示第10行吗?前:
cat file | sed -e '/linux/!d' -e '10!d' ### I can not display the first 10 lines of the word linux
cat file | sed '/linux/!d' | sed '10!d' ### It is well
如何使它与一个sed一起工作?
cat file | sed -e '/linux/!d; ...?; 10!d'
.?-
我有一个文件(sample1.txt),格式如下:
1 a
2 b\
3 d
4 e\
5 k
6 hello\hi
7 stackoverflow\site\
现在,如果一行的最后一个字符是'\‘,那么我想用'’替换它,这样我就可以将sample1.txt转换成以下文件:
1 a
2 b
3 d
4 e
5 k
6 hello\hi
7 stackoverflow\site
我知道使用编程语言也可以这样做,但是否可以使用linux命令(如grep、sed等)进行同样的操作?
我得到了以下格式的数据:
comp.os.linux announce 0000002587 02190 m
comp.arch 00000 28874 y
utsa.cs.3423 00000000004 000000000001 y
我应该对它进行处理,使其看起来像:
comp.os.linux announce m
comp.arch y
utsa.cs.3423 y
我尝试过s/^[0-9]//g,它似乎工作得很好,但最后一行缺少4个数字
使用linux,我希望搜索字符串Blah的文本文件,然后返回包含字符串的行完整行和模式后面的所有行,直到包含单词Failed的一行为止。
例如,
Test Case Name "Blah"
Error 1
Error 2
Error 3
Failed
Test Case Name "Foo"
Pass
Test Case Name "Red"
Pass
在上面,我想搜索"Blah",然后返回:
Test Case Name "Blah"
Error 1
Error 2
Error 3
一直到线Failed。Blah
我试图用换行符替换每一行中的第一个空格。
例如
123 there is a monkey
567 there is also a tiger
会变成
123
there is a monkey
567
there is also a tiger
我试着用命令来做这件事:
sed -e 's/^\s\+/\n/g' output.txt > new.txt
没有运气。我也对linux中允许我这样做的任何其他程序开放,比如awk、perl或bash。
我需要多次删除包含某个字符串的文件中的所有行,例如,如果我的文件如下所示:
This is a test toRemove first line
This is a test toRemove second line toRemove
应该生成一个类似于只有第一行的文件
This is a test toRemove first line
我正在尝试从命令行在linux上执行此操作,并尝试使用grep或sed,如下所示
grep -d "toRemove.*toRemove" myFile > myOtherFile
sed '/\toRemove.*toRe
我想在一行(多行)的特定单词之间替换字符。例如:
first second third | first line
first second third | second line
first second third | third line
first second third | forth line
....
我想在linux中替换第三和第一/第二/第三/第四etc...using、sed或vi之间的字符。
如果这个问题已经回答了,你能给我提供链接吗?谢谢!
我想显示匹配正则表达式的所有行
如果我有一个文件
foo
bar123
baz12435
lorem
ipsum
dolor
sit
amet
这个display-from baz[0-9]* < file应该返回(它是否显示匹配的行并不重要)
lorem
ipsum
dolor
sit
amet
如何在Linux中完成此操作(使用sed、awk或grep)
我有一个文件beginning.txt,其中包含的行如下:
Disclaimer Here
This is a Notice
I have something liere
如何执行linux命令,将beginning.txt中的所有行添加到与扩展名" .customfile“匹配的每个文件的顶部(注意,.customfile只是一个文本文件,但这些文件可能位于我当前文件夹中的子目录中,我也希望更新这些文件)。
我有许多带有.customfile后缀的文件,我想要追加这些文件,所以我想找一种通过编程实现这个功能的方法。我看到了sed命令的示例,它仅限于一行。
sed -i '1s
我正在寻找一种解决方案来更改linux中列的位置。在我的特殊情况下,从第一个位置到最后一个位置。
qty | sender | recipient | some email subject
sender | recipient | some email subject | qty
如果我有一个具有以下内容的文件:
4 | one | two | the first subject
5 | one | four | other interesting subject
我希望有以下产出:
one | two | the first subject | 4
one | four | other i
如何使用sed用文本替换空行?我尝试了一些早些时候发布的技巧,但它不起作用。有人能帮我吗?
示例文件内容-
Linux is an os.
Linux is opensource
Linux has various flavors like fedora,centos etc.
我想用像hello这样的文字来代替这些空白行。