bash 如何将多行文本转换为一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6050766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:32:14  来源:igfitidea点击:

How do I convert multi line text into one line?

bashsedtext-processing

提问by john

I'm trying to make a txt file with a generated key into 1 line. example:

我正在尝试将一个带有生成密钥的 txt 文件分成 1 行。例子:

<----- key start ----->
lkdjasdjskdjaskdjasdkj
skdhfjlkdfjlkdsfjsdlfk
kldshfjlsdhjfksdhfksdj
jdhsfkjsdhfksdjfhskdfh
jhdfkjsdhfkjsdhfkjsdhf
<----- key stop ----->

I want it to look like:

我希望它看起来像:

lkdjasdjskdjaskdjasdkjskdhfjlkdfjlkdsfjsdlfkkldshfjlsdhjfksdhfksdjjdhsfkjsdhfksdjfhskdfhjhdfkjsdhfkjsdhfkjsdhf

Notice I also want the lines <----- key start ----->and <----- key stop ----->removed. How can I do this? Would this be done with sed?

请注意我也想行<----- key start -----><----- key stop ----->删除。我怎样才能做到这一点?这会完成sed吗?

回答by Axel Bregnsbo

To convert multi line output to a single space separated line, use

要将多行输出转换为单个空格分隔的行,请使用

tr '\n' ' ' < key.txt

I know this does not answer the detailed question. But it is one possible answer to the title. I needed this answer and my google search found this question.

我知道这不能回答详细的问题。但这是标题的一种可能答案。我需要这个答案,我的谷歌搜索找到了这个问题。

回答by Costa

If you're looking for everything you asked for in one sed, I have this...

如果你在一个 sed 中寻找你想要的一切,我有这个......

sed -n '1h;2,$H;${g;s/\n//g;s/<----- key \(start\|stop\) ----->//g;p}' key.txt

sed -n '1h;2,$H;${g;s/\n//g;s/<----- key \(start\|stop\) ----->//g;p}' key.txt

But it's not exactly easily readable :) If you don't mind piping a couple of commands, you could use the piped grep, tr, sed, etc. suggestions in the rest of the answers you got.

但这并不是很容易阅读 :) 如果您不介意管道化几个命令,则可以在您得到的其余答案中使用管道化的 grep、tr、sed 等建议。

回答by Daniel Haley

grep '^[^<]' test.txt | tr -d '\n'

回答by potong

This might work for you (GNU sed):

这可能对你有用(GNU sed):

sed -r '/key start/{:a;N;/key stop/!ba;s/^[^\n]*\n(.*)\n.*//;s/\n//g}' file

Gather up lines between key startand key stop. Then remove the first and last lines and delete any newlines.

收集key start和之间的线key stop。然后删除第一行和最后一行并删除所有换行符。

回答by glenn Hymanman

awk '/ key (start|stop) / {next} {printf("%s", 
str='
aaaaa
<----- key start ----->
lkdjasdjskdjaskdjasdkj
skdhfjlkdfjlkdsfjsdlfk
kldshfjlsdhjfksdhfksdj
jdhsfkjsdhfksdjfhskdfh
jhdfkjsdhfkjsdhfkjsdhf
<----- key stop ----->
bbbbb
'


# for in-place file editing use "ed -s file" and replace ",p" with "w"
# cf. http://wiki.bash-hackers.org/howto/edit-ed
cat <<-'EOF' | sed -e 's/^ *//' -e 's/ *$//' | ed -s <(echo "$str")
   H
   /<----- key start ----->/+1,/<----- key stop ----->/-1j
   /<----- key start ----->/d
   /<----- key stop ----->/d
   ,p
   q
EOF


# print the joined lines to stdout only
cat <<-'EOF' | sed -e 's/^ *//' -e 's/ *$//' | ed -s <(echo "$str")
   H
   /<----- key start ----->/+1,/<----- key stop ----->/-1jp
   q
EOF
)} END {print ""}' filename

回答by kuriouscoder

An easy way would be to use cat file.txt | tr -d '\n'

一个简单的方法是使用 cat file.txt | tr -d '\n'

回答by Milimetric

In vim, it's just :%s/^M//

在 vim 中,它只是 :%s/^M//

I use this all the time to generate comma separated lists from lines. For sed or awk, check out the many solutions at this link:

我一直使用它从行中生成逗号分隔的列表。对于 sed 或 awk,请查看此链接中的许多解决方案:

http://www.unix.com/shell-programming-scripting/35107-remove-line-break.html

http://www.unix.com/shell-programming-scripting/35107-remove-line-break.html

Example:

例子:

paste -s -d',' tmpfile | sed 's/,/, /g'

粘贴 -s -d',' tmpfile | sed 's/,/, /g'

回答by karl

You may use man 1 edto join lines as well:

您也可以使用man 1 ed连接线:

grep  -v -e "key start" -e "key stop" /PATH_TO/key | tr -d '\n'

回答by freethinker

##代码##