0

This is related to a question i asked in overflow, It remains unanswered so i though I'd try asking it in segments

My objective is to return the special \n function, If i were to run

echo -e "My name is \nnick rock and i \nlive by the sea shore." > file

Then when i cat that file you get something like this

My name is
nick rock and i
live by the sea shore.

What I'm looking to do is reverse the \n return function, so when i echo the file with something like this

echo -aoE '([a-zA-Z]){1,5}' file

output
My 
name 
is
nick 
rock 
and 
i
live 
by 
the 
sea 
shore.

I'll get this instead

My name is nick rock and i live by the sea shore
muru
  • 197,895
  • 55
  • 485
  • 740

4 Answers4

1

you can use this command line, it's an improvement of @Sergiv Kolodyazhnyy which print the \n at the right place

while IFS= read -r line || [ -n "$line" ]; do printf "%s " "$line" ; printf "\n" ; done < test.txt 

(sorry to not be able to comment)

damadam
  • 2,833
1

You can also echo unquoted command-substitution:

echo $(<infile)
αғsнιη
  • 35,660
0

What you're looking for is

$ tr  '\n' ' ' < file
My name is  nick rock and i  live by the sea shore. 

tr utility is for translating sets of characters, in this there's only one character \n in SET1 and space in SET2. As for < file, that just redirects the file into program's stdin stream.

But if for some reason you need a shell-only way, then this is sufficient:

$ while IFS= read -r line || [ -n "$line" ]; do printf "%s " "$line" ; done < file
My name is  nick rock and i  live by the sea shore. 
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
0

Here are some options:

  1. using awk, with an unset record separator (aka 'paragraph mode') and the default field separator

    awk -vRS= '{$1=$1} 1' file
    My name is nick rock and i live by the sea shore.
    
  2. using xargs with no explicit command

    $ xargs < file
    My name is nick rock and i live by the sea shore.
    
  3. using fmt from GNU coreutils:

    $ fmt file
    My name is nick rock and i live by the sea shore.
    
steeldriver
  • 136,215
  • 21
  • 243
  • 336