35

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

<----- 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?

Surya
  • 2,429
  • 1
  • 21
  • 42
john
  • 1,330
  • 3
  • 20
  • 34

12 Answers12

43
tr -d '\n' < key.txt

Found on http://linux.dsplabs.com.au/rmnl-remove-new-line-characters-tr-awk-perl-sed-c-cpp-bash-python-xargs-ghc-ghci-haskell-sam-ssam-p65/

Luke
  • 1,872
  • 20
  • 31
  • 1
    im also trying to remove the lines <----- key start -----> and <----- key stop -----> which is why i thought sed would work better.. – john May 18 '11 at 20:47
  • don't have access to console right now. I have found this however http://www.linuxquestions.org/questions/programming-9/delete-first-and-last-lines-of-a-file-586069/ Sorry didn't see that last line. – Luke May 18 '11 at 20:54
19

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.

Axel Bregnsbo
  • 3,773
  • 2
  • 22
  • 16
7
tail -n +2 key.txt | head -n -1 | tr -d '\n'

Tail to remove the first line, head to remove the last line and tr to remove newlines.

Javier Yáñez
  • 571
  • 5
  • 7
6

If you're 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 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.

Costa
  • 2,043
  • 1
  • 14
  • 27
3

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

kuriouscoder
  • 5,394
  • 7
  • 26
  • 40
  • im also trying to remove the lines <----- key start -----> and <----- key stop -----> which is why i thought sed would work better. – john May 18 '11 at 20:48
2

This might work for you (GNU sed):

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

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

potong
  • 55,640
  • 6
  • 51
  • 83
2
grep '^[^<]' test.txt | tr -d '\n'
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
1

In vim, it's just :%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:

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

Example:

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

Milimetric
  • 13,411
  • 4
  • 44
  • 56
1
grep  -v -e "key start" -e "key stop" /PATH_TO/key | tr -d '\n'
freethinker
  • 1,286
  • 1
  • 10
  • 17
1
awk '/ key (start|stop) / {next} {printf("%s", $0)} END {print ""}' filename
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1

Every other answer mentioned here converts the key to a single line, but the result that we get is not a valid key and hence I was running into problems. If you also have the same issue, please try

awk -v ORS='\\n' '1'  key.txt/file-name

Credit: https://gist.github.com/bafxyz/de4c94c0912f59969bd27b47069eeac0

Surya
  • 2,429
  • 1
  • 21
  • 42
0

You may use man 1 ed to join lines as well:

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
karl
  • 1