jostber
1
I have a vim outliner file like this:
Title
title 2
:Testing now
:testing 2
:testing 3
title 3
:testing
:ttt
:ttg
Is there a way to use a script or command to remove the ":" in front of the the text blocks and join these lines to give this output?
Title
title 2
Testing now testing 2 testing 3
title 3
testing ttt ttg
Try this,
awk '{if(/:/) {sub(/:/,"");printf "%s",$0}else{printf "\n%s\n",$0}} END {print "\n"}' inputfile
Try this:
awk '/:/{if(s){$1=$1};s=s?s FS $0:$0; next}
s{gsub(":", "", s); print s; s=""}1
END{if(s)gsub(":", "", s);print s}
' file
1 Like
Try...
awk '{ORS=RS}/:/{sub(/:/,"");ORS=" "}1' infile
jostber
5
Thanks for the suggestions! This works perfectly as it also removes the extra whitespace between the words.
danmero
6
awk 'END{f(s)}function f(s){if(s)gsub(":",x,s);print s}/:/{if(s){$1=$1};s=((s)?s FS:x)$0;next}s{f(s);s=""}1' file
A spin-out of Franklin solution
awk -F: '/:/{p=(p)?p OFS $2:$1$2;next}p{print p,p=x}1;END{print p}' file
Or with Perl -
$
$
$ cat f5
Title
title 2
:Testing now
:testing 2
:testing 3
title 3
:testing
:ttt
:ttg
$
$
$ perl -lne 'if(/^(\s*):(.*)$/){if(!$x){printf("%s%s ",$1,$2);$x=1}else{printf("%s ",$2)}}elsif($x){print "\n",$_;$x=0}else{print}END{print}' f5
Title
title 2
Testing now testing 2 testing 3
title 3
testing ttt ttg
$
$
$
tyler_durden