How to convert multi-line text to one line?

I am trying to make a txt file with a generated key in 1 line. Example:

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

I want it to look like this:

 lkdjasdjskdjaskdjasdkjskdhfjlkdfjlkdsfjsdlfkkldshfjlsdhjfksdhfksdjjdhsfkjsdhfksdjfhskdfhjhdfkjsdhfkjsdhfkjsdhf 

Please note: I also want to delete the lines <----- key start -----> and <----- key stop -----> . How can i do this? Will this be done with sed ?

+12
source share
11 answers

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

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

But this is not very easy to read :) If you do not mind laying a couple of commands, you can use the grep, tr, sed, etc. sentences. in the rest of the answers you received.

+5
source

To convert multi-line output to a separate dividing line, use

 tr '\n' ' ' < key.txt 

I know this does not answer a detailed question. But this is one of the possible answers to the title. I need this answer and my google search found this question.

+4
source
 grep '^[^<]' test.txt | tr -d '\n' 
+2
source

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

+1
source
 awk '/ key (start|stop) / {next} {printf("%s", $0)} END {print ""}' filename 
+1
source

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

I use this all the time to create comma-separated lists of strings. For sed or awk, check out many of the solutions here:

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

Example:

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

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

You can use man 1 ed to concatenate strings:

 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 
0
source

This may work for you (GNU sed):

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

Collect the lines between key start and key stop . Then delete the first and last lines and delete all new lines.

0
source
 tail -n +2 key.txt | head -n -1 | tr -d '\n' 

Tail to delete the first line, head to delete the last line, and tr to delete new lines.

0
source

Source: https://habr.com/ru/post/888471/


All Articles