如果我运行命令cat文件| grep pattern,我将得到许多行输出。如何将所有行连接成一行,有效地将每个“\n”替换为“\”(以“”结尾,后面加空格)?
Cat文件| grep模式| xargs sed s/\n/ /g 对我没用。
如果我运行命令cat文件| grep pattern,我将得到许多行输出。如何将所有行连接成一行,有效地将每个“\n”替换为“\”(以“”结尾,后面加空格)?
Cat文件| grep模式| xargs sed s/\n/ /g 对我没用。
当前回答
粘贴-sd ` ~ `出错。
下面是我在使用bash的mac上使用的方法
cat file | grep pattern | paste -d' ' -s -
从男人膏。
-d list Use one or more of the provided characters to replace the newline characters instead of the default tab. The characters
in list are used circularly, i.e., when list is exhausted the first character from list is reused. This continues until
a line from the last input file (in default operation) or the last line in each file (using the -s option) is displayed,
at which time paste begins selecting characters from the beginning of list again.
The following special characters can also be used in list:
\n newline character
\t tab character
\\ backslash character
\0 Empty string (not a null character).
Any other character preceded by a backslash is equivalent to the character itself.
-s Concatenate all of the lines of each separate input file in command line order. The newline character of every line
except the last line in each input file is replaced with the tab character, unless otherwise specified by the -d option.
If ‘-’ is specified for one or more of the input files, the standard input is used; standard input is read one line at a time,
循环地,对于每个' - '的实例。
其他回答
粘贴-sd ` ~ `出错。
下面是我在使用bash的mac上使用的方法
cat file | grep pattern | paste -d' ' -s -
从男人膏。
-d list Use one or more of the provided characters to replace the newline characters instead of the default tab. The characters
in list are used circularly, i.e., when list is exhausted the first character from list is reused. This continues until
a line from the last input file (in default operation) or the last line in each file (using the -s option) is displayed,
at which time paste begins selecting characters from the beginning of list again.
The following special characters can also be used in list:
\n newline character
\t tab character
\\ backslash character
\0 Empty string (not a null character).
Any other character preceded by a backslash is equivalent to the character itself.
-s Concatenate all of the lines of each separate input file in command line order. The newline character of every line
except the last line in each input file is replaced with the tab character, unless otherwise specified by the -d option.
If ‘-’ is specified for one or more of the input files, the standard input is used; standard input is read one line at a time,
循环地,对于每个' - '的实例。
下面是另一个使用awk的简单方法:
# cat > file.txt
a
b
c
# cat file.txt | awk '{ printf("%s ", $0) }'
a b c
此外,如果你的文件有列,这提供了一个简单的方法来连接某些列:
# cat > cols.txt
a b c
d e f
# cat cols.txt | awk '{ printf("%s ", $2) }'
b e
这是一个用逗号分隔输出的示例。您可以用任何分隔符替换逗号。
cat <<EOD | xargs | sed 's/ /,/g'
> 1
> 2
> 3
> 4
> 5
> EOD
生产:
1,2,3,4,5
我喜欢xargs的解决方案,但如果不折叠空格很重要,那么可以这样做:
sed ':b;N;$!bb;s/\n/ /g'
这将为空格替换换行符,而不像tr '\n' ' ' '那样替换最后一行结束符。
这也允许你使用除空格之外的其他连接字符串,比如逗号等,这是xargs无法做到的:
$ seq 1 5 | sed ':b;N;$!bb;s/\n/,/g'
1,2,3,4,5
我所知道的解决这个问题最快最简单的方法是:
当我们想用空格替换新的行字符\n时:
xargs < file
Xargs对每行字符数和所有字符的总和有自己的限制,但我们可以增加这些限制。详细信息可以通过运行命令:xargs——show-limits和手册:man xargs找到
当我们想用一个字符替换另一个字符时:
tr '\n' ' ' < file
当我们想用多个字符替换一个字符时:
tr '\n' '~' < file | sed s/~/many_characters/g
首先,我们将换行符\n替换为波浪号~(或选择文本中没有出现的另一个唯一字符),然后我们将波浪号替换为任何其他字符(many_characters),并对每个波浪号执行此操作(标记g)。