sed to replace with backspace
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
Related videos on Youtube
![JigarGandhi](https://lh6.googleusercontent.com/-eAsu_l2ATZc/AAAAAAAAAAI/AAAAAAAAAEk/vyTxdPrBRPI/photo.jpg?sz=256)
JigarGandhi
VLSI Engg. currently working as VLSI RnD Engg. Enjoy TCL scripting & shell scripting. loves rubik cubing & cycling too
Updated on September 18, 2022Comments
-
JigarGandhi almost 2 years
I have
trail.txt
, which containsspice
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
+
withbackspace
but its not working out. Is there any alternate way to work on this? Are there any other alternatives available? -
JigarGandhi over 9 yearsI think I need to modify the above command for
tcsh
; currently it shows error as N: Event not found -
smw over 9 yearsYes in tcsh you may need to escape the ! (even though it is protected by single-quotes) i.e.
'$\!N;s/\n+/ /;ta'
-
JigarGandhi over 9 yearsNow it works perfectly
-
Costas over 9 yearsOr to provide your script as onliner
awk '!/^\+/&&NR!=1{print""}{sub(/^\+/,"");printf$0}END{print""}' file
-
Angel Todorov over 9 yearsDon't use
printf
without a format specifier in case the string contains%
--echo "foo%sbar" | awk '{printf $0}'
-
JigarGandhi over 9 yearswhat is meant by
-z
? I am usingtcsh
andman sed
orsed help
has nothing listed for-z
-
Costas over 9 years@JigarGandhi This is problem
sed
version not shell. -z, --null-data separate lines by NUL characters Trysed --version
- I havesed (GNU sed) 4.2.2
-
JigarGandhi over 9 years
GNU sed version 4.1.5
-
JigarGandhi over 9 yearsCan I convert this
sed
tosed -i
inline editor mode? I tried thissed -i :a -e '$\!N;s/\n+/ /;ta' -i 'P;D' trail.txt
-
Арсений Черенков almost 9 yearsthis doesn't do what OP asked.