How can you incorporate all lines that end with a backslash personality?

Making use of an usual command line device like sed or awk, is it feasible to sign up with all lines that end with an offered personality, like a backslash?

As an example, offered the documents :

foo bar \
bash \
baz
dude \
happy

I would love to get this output :

foo bar bash baz
dude happy
0
2019-12-02 03:09:08
Source Share
Answers: 4

It is perhaps most convenient with perl (given that perl resembles sed and also awk, I wish it serves to you) :

perl -p -e 's/\\\n//'
0
2019-12-03 05:03:50
Source

Here is an awk remedy. If a line finishes with a \, strip the backslash and also publish the line without ending newline ; or else publish the line with an ending newline.

awk '{if (sub(/\\$/,"")) printf "%s", $0; else print $0}'

It's also not too bad in sed, though awk is obviously more readable.

0
2019-12-03 05:03:34
Source

This is not a solution thus. It is a side concern concerning sed.

Especially, I required to take Gilles sed command apart item by item to recognize it ... I began creating some notes on it, and afterwards assumed it might serve below to a person ...

so below it is ... Gilles' sed manuscript in recorded layout :


#!/bin/bash
#######################################
sed_dat="$HOME/ztest.dat"
while IFS= read -r line ;do echo "$line" ;done <<'END_DAT' >"$sed_dat"
foo bar \
bash \
baz
dude \
happy
yabba dabba 
doo
END_DAT

#######################################
sedexec="$HOME/ztest.sed"
while IFS= read -r line ;do echo "$line" ;done <<'END-SED' >"$sedexec"; \
sed  -nf "$sedexec" "$sed_dat"

  s/\\$//        # If a line has trailing '\', remove the '\'
                 #    
  t'Hold-append' # branch: Branch conditionally to the label 'Hold-append'
                 #         The condition is that a replacement was made.
                 #         The current pattern-space had a trailing '\' which  
                 #         was replaced, so branch to 'Hold-apend' and append 
                 #         the now-truncated line to the hold-space
                 #
                 # This branching occurs for each (successive) such line. 
                 #
                 # PS. The 't' command may be so named because it means 'on true' 
                 #     (I'm not sure about this, but the shoe fits)  
                 #
                 # Note: Appending to the hold-space introduces a leading '\n'   
                 #       delimiter for each appended line
                 #  
                 #   eg. compare the hex dump of the follow 4 example commands:  
                 #       'x' swaps the hold and patten spaces
                 #
                 #       echo -n "a" |sed -ne         'p' |xxd -p  ## 61 
                 #       echo -n "a" |sed -ne     'H;x;p' |xxd -p  ## 0a61
                 #       echo -n "a" |sed -ne   'H;H;x;p' |xxd -p  ## 0a610a61
                 #       echo -n "a" |sed -ne 'H;H;H;x;p' |xxd -p  ## 0a610a610a61

   # No replacement was made above, so the current pattern-space
   #   (input line) has a "normal" ending.

   x             # Swap the pattern-space (the just-read "normal" line)
                 #   with the hold-space. The hold-space holds the accumulation
                 #   of appended  "stripped-of-backslah" lines

   G             # The pattern-space now holds zero to many "stripped-of-backslah" lines
                 #   each of which has a preceding '\n'
                 # The 'G' command Gets the Hold-space and appends it to 
                 #   the pattern-space. This append action introduces another
                 #   '\n' delimiter to the pattern space. 

   s/\n//g       # Remove all '\n' newlines from the pattern-space

   p             # Print the pattern-space

   s/.*//        # Now we need to remove all data from the pattern-space
                 # This is done as a means to remove data from the hold-space 
                 #  (there is no way to directly remove data from the hold-space)

   x             # Swap the no-data pattern space with the hold-space
                 # This leaves the hold-space re-initialized to empty...
                 # The current pattern-space will be overwritten by the next line-read

   b             # Everything is ready for the next line-read. It is time to make 
                 # an unconditional branch  the to end of process for this line
                 #  ie. skip any remaining logic, read the next line and start the process again.

  :'Hold-append' # The ':' (colon) indicates a label.. 
                 # A label is the target of the 2 branch commands, 'b' and 't'
                 # A label can be a single letter (it is often 'a')
                 # Note;  'b' can be used without a label as seen in the previous command 

    H            # Append the pattern to the hold buffer
                 # The pattern is prefixed with a '\n' before it is appended

END-SED
#######
0
2019-12-03 02:46:33
Source

a much shorter and also less complex sed remedy :

sed  '
: again
/\\$/ {
    N
    s/\\\n//
    t again
}
' textfile

or one - lining if making use of GNU sed :

sed ':x; /\\$/ { N; s/\\\n//; tx }' textfile
0
2019-12-03 02:40:14
Source