sed to replace with backspace

11,172

Solution 1

I don't think you can use a backspace approach - however you could do something like

sed -e :a -e '$!N;s/\n+/ /;ta' -e 'P;D' trail.txt

See Famous Sed One-Liners Explained, Part I: File Spacing, Numbering and Text Conversion and Substitution, 40. Append a line to the previous if it starts with an equal sign "=" (with the obvious substitution of + for =).

Solution 2

A couple of ideas, since it'll be a bit tricky with sed:

perl -00 -pe 's/\n\+\s*/ /g' file
awk '
    {
        if (/^\+/) 
            sub(/^\+[[:blank:]]*/, " ", $0)
        else 
            if (NR > 1) print ""
        printf "%s", $0
    } 
    END {print ""}
' file 

Solution 3

FWIW, GNU awk

awk -vRS='\\n\\+?' '{ORS=RT == "\n+"? "":RT; print}' trail.txt
data net06706 net06707 net221 net222 net223
m1 net10 rwl vdda vss nch l="l1pg+0.005u" w=w1pg+0.105u
m0 vdda rwld net10 vss nch l="l1pg+0.005u" w=w1pg+0.105u
Share:
11,172

Related videos on Youtube

JigarGandhi
Author by

JigarGandhi

VLSI Engg. currently working as VLSI RnD Engg. Enjoy TCL scripting & shell scripting. loves rubik cubing & cycling too

Updated on September 18, 2022

Comments

  • JigarGandhi
    JigarGandhi almost 2 years

    I have trail.txt, which contains spice netlist. Now I want to replace all expanded parameters to one line only i.e. remove + and append that line to previous line.

    trail.txt

    data
    + net06706 net06707
    + net221 net222 net223 
    m1 net10 rwl vdda vss nch l="l1pg+0.005u" w=w1pg+0.105u 
    m0 vdda rwld net10 vss nch l="l1pg+0.005u" w=w1pg+0.105u 
    

    (Desired) trail.txt

    data net06706 net06707 net221 net222 net223 
    m1 net10 rwl vdda vss nch l="l1pg+0.005u" w=w1pg+0.105u 
    m0 vdda rwld net10 vss nch l="l1pg+0.005u" w=w1pg+0.105u 
    

    Command

    sed -e 's/^+/\b/g' trail.txt
    

    I tried to repalce + with backspace but its not working out. Is there any alternate way to work on this? Are there any other alternatives available?

  • JigarGandhi
    JigarGandhi over 9 years
    I think I need to modify the above command for tcsh; currently it shows error as N: Event not found
  • smw
    smw over 9 years
    Yes in tcsh you may need to escape the ! (even though it is protected by single-quotes) i.e. '$\!N;s/\n+/ /;ta'
  • JigarGandhi
    JigarGandhi over 9 years
    Now it works perfectly
  • Costas
    Costas over 9 years
    Or to provide your script as onliner awk '!/^\+/&&NR!=1{print""}{sub(/^\+/,"");printf$0}END{print""}' file
  • Angel Todorov
    Angel Todorov over 9 years
    Don't use printf without a format specifier in case the string contains % -- echo "foo%sbar" | awk '{printf $0}'
  • JigarGandhi
    JigarGandhi over 9 years
    what is meant by -z ? I am using tcsh and man sed or sed help has nothing listed for -z
  • Costas
    Costas over 9 years
    @JigarGandhi This is problem sed version not shell. -z, --null-data separate lines by NUL characters Try sed --version - I have sed (GNU sed) 4.2.2
  • JigarGandhi
    JigarGandhi over 9 years
    GNU sed version 4.1.5
  • JigarGandhi
    JigarGandhi over 9 years
    Can I convert this sed to sed -i inline editor mode? I tried this sed -i :a -e '$\!N;s/\n+/ /;ta' -i 'P;D' trail.txt
  • Арсений Черенков
    Арсений Черенков almost 9 years
    this doesn't do what OP asked.