如何使用sed用文本替换空行?我尝试了一些早些时候发布的技巧,但它不起作用。有人能帮我吗?
示例文件内容-
Linux is an os.
Linux is opensource
Linux has various flavors like fedora,centos etc.
我想用像hello这样的文字来代替这些空白行。
这是标准的ls -l输出。
user@linux:~$ ls -l
total 0
-rw-rw-r-- 1 user user 0 Nov 1 00:00 file01
-rw-rw-r-- 1 user user 0 Nov 1 00:00 file02
-rw-rw-r-- 1 user user 0 Nov 1 00:00 file03
user@linux:~$
可以增加这样的新行吗?
-rw-rw-r-- 1 user user 0 Nov 1 00:00 file01
-rw-rw-r-- 1 user user 0 Nov 1 00:00 file02
我在搜索文件的特定部分并将其存储到bash shell的变量中时遇到了问题。
这是我的一个文件示例:
From root@machine2.com Mon Jan 7 16:56:50 2013
Return-Path: <root@machine2.com>
X-Original-To: smsto+9121403571@machine2.com
Delivered-To: smsto+9121403571@machine2.com
Received: by machine2.com (Postfix, from userid 0)
id 43C191A1EC
好的,谷歌几分钟后,这似乎是用sed在每一行输出前加上前缀的常规方法。
但我犯了一个我不明白的错误。
这意味着什么,我该如何解决这个问题?
$ sed 's/^/#/' test.txt
sed: -e expression #1, char 0: no previous regular expression
我的test.txt是什么样子的-这真的只是个测试
### test.txt
a
b
c
d
e
f
g
h
哦是的..。版本
$ sed --version
GNU sed version 4.2.1
Copyright (C) 2009 Free Software F
这是我的样本文件
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
我有一个包含名称列表的文件(称为文件1):
Apple
Bat
Cat
我有另一个文件(参见文件2),其中包含名称和详细信息的列表,请参阅:
Apple bla blaa
aaaaaaaaaggggggggggttttttsssssssvvvvvvv
ssssssssiiuuuuuuuuuueeeeeeeeeeennnnnnnn
sdasasssssssssssssssssssssswwwwwwwwwwww
Aeroplane dsafgeq dasfqw dafsad
vvvvvvvvvvvvvvvvuuuuuuuuuuuuuuuuuuuuuus
fcsadsssssssssssssss
我发现了几个相关的问题,但这些问题都不符合我的需要,因为我是一个真正的初学者,我无法理解它。
我有一个文本文件,其中有这样的条目,用空行分隔:
example entry &with/ special characters
next line (any characters)
next %*entry
more words
我希望输出合并行,在中间放置逗号,并删除空行。也就是说,这个例子应该如下所示:
example entry &with/ special characters, next line (any characters)
next %*entry, more w
我想删除包含关键字的行之后的换行符,例如private:、public:或protected:,以满足我们的编码标准。我需要一个命令行工具(Linux)为此,所以请没有Notepad++,Emacs,VS或Vim解决方案,如果他们需要用户交互。换句话说,我想做一个:
sed -i 's/private:\s*\n\s*\n/private:\n/g'
我见过这个,但无法将其扩展到我的需要。
在linux中,我需要删除带有sed的C程序中的注释行,假设每个注释行都包含开始和结束标记,前后没有任何其他语句。
例如,下面的代码:
/* a comment line in a C program */
printf("It is /* NOT a comment line */\n");
x = 5; /* This is an assignment, not a comment line */
[TAB][SPACE] /* another empty comment line here */
/* another weird line, but not a comme
是否有一种简单的方法在每个句子之间添加四个空行。
例如,这一段:
This combination of vitamins and selected minerals is used to prevent or treat vitamin
deficiency due to poor eating habits, problems that affect the body's ability to absorb nutrition from food, or increased need for vitamins and minerals due to stress
or illn
我一直试图找出如何删除重复的行,仅使用Sed,我有困难,找出如何做它。
到目前为止,我已经试过了,但效果不佳。
sed '$!N; /^\(.*\)\n\1$/!P; D'
文件:
APPLE
ORANGES
BANANA
BANANA
COOKIES
FRUITS
我得到的是:
APPLE
ORANGES
BANANA
BANANA
COOKIES
FRUITS
我想要的:
APPLE
ORANGES
BANANA
COOKIES
FRUITS
我一直在努力弄清楚如何做到这一点,这样我就不必手动遍历文件中的每一行,并告诉它手动删除重复的内容。
我的
在linux中使用这里的脚本在远程服务器上运行命令时,所有的行都会打印出来。怎样才能压制评论呢?
以下代码的输出应该是:
ls
... (whatever is in this folder)
echo -e this is a test\ndone
this is a testndone
exit
这个是可能的吗?这样做的原因是命令和注释更加复杂,使输出难以阅读。那应该更漂亮些。
#!/bin/bash
ssh -tt hogan@123.123.123.123 <<EOF
# this line get printed
ls
# and this comme
我对sed非常陌生,我发现的每一件事都在这里,有一点在那里。
我有一个文本文件,其中包含如下所示的块:
#start
a
b
c
#whatever
…
显然,这是一个简化的版本。我想在#start块的末尾附加一行给我:
#start
a
b
c
d
#whatever
…
我可以用以下方法来定位这个块:
sed -n '/^#\s*start/,/^$/ p' data.txt
所以我认为这是正确的方向。然而:
所选内容包括空行,我不想这样
我想不出怎么在比赛后再加一行
sed命令是否能够删除空格,下一行?
我希望得到以下输出(参见下面的输出)
TESTING The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
TESTING02 The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
TESTING03 The quick brown fox j
我有一个类似这样的文件:
A
B
C
D
E
F
G
H
I
J
K
L
我想让它像这样
A,B,C,D
E,F,G,H
我假设我会使用sed,但实际上我甚至不确定这是否是最好的工具。我对使用Linux系统上常见的任何东西都持开放态度。
在perl中,我是这样做的。它可以工作,但它很脏,而且后面有一个逗号。希望有更简单的东西:
$ perl -ne 'if (/^(\w)\R/) {print "$1,";} else {print "\n";}' test
A,B,C,D,
E,F,G,H,
I,J,K,L,
我有点卡住了..。我有一个包含以下数据的文件:
Group: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
POM Lorem ipsum dolor sit amet, consectetur adipiscing elit.
POM Lorem ipsum dolor sit amet, consectetur adipiscing elit. https://www.lorem.org/
Embedded Lorem ipsum dolor sit amet, consectetur adipiscing eli
我希望使用sed或其他解决方案修改文本文件中的每一行。
含input.txt
line 1
line 2
line 3
...
output.txt应该添加两个字符串
<before> line 1 and <after each line>
<before> line 2 and <after each line>
<before> line 3 and <after each line>
...
我能用sed还是别的什么吗?
我正在尝试在MacOSX终端上运行这个命令,它最初是打算在Linux上运行的。
sed '1 i VISPATH=/mnt/local/gdrive/public/3DVis' init.txt >> ~/.bash_profile
但它给了我一个错误:
command i expects \ followed by text.
有没有办法修改上面的命令,使其在MacOSX终端上工作
我试图编写一个相当简洁的sed脚本,仅在文件的前两行为空时才删除它们,因此下面的文件如下:
> cat myfile.in
Line 3
Line 5
将产生一个三行输出文件:
> cat myfile.out
Line 3
Line 5
这涉及到合并行范围和模式匹配,我只是找不到任何这样的例子。如果有人能建议并同样(或更多)考虑Perl替代方案,我也会感到兴奋。非常感谢。
脚注
我应该补充一点,我尝试了1,2{/^$/d},它在Linux上工作得非常好,但是在AIX中我得到了:
sed: 0602-404 Function 1,2{/^$/d} cannot be par