jisha
1
Hi ,
I have a file in the below format:
1.txt
awk 'BEGIN { printf ("%1s", "man" )} '
awk 'BEGIN { printf ("%9s", "women" )} '
awk 'BEGIN { printf ("%56s", "human")} '
##
###
##
echo "$!"
##
awk 'BEGIN { printf ("%1s", "aaa" )} '
awk 'BEGIN { printf ("%19s", "bbb" )} '
awk 'BEGIN { printf ("%24s", "ccc" )} '
### #
##
awk 'BEGIN { printf ("%1s", "rrr"} '
awk 'BEGIN { printf ("%31s", "ddd"}'
awk 'BEGIN { printf ("%43s","eeee"}'
I want to convert it to the below file:
2.txt
awk 'BEGIN {
printf ("%1s", "man" );
printf ("%9s", "women" );
printf ("%56s", "human")}'
##
###
##
echo "$!"
##
awk 'BEGIN {
printf ("%1s", "aaa");
printf ("%19s", "bbb");
printf ("%24s","ccc" )}'
### #
##
awk 'BEGIN {
printf ("%1s", "rrr");
printf ("%19s", "ddd");
printf ("%24s","eeee")}'
Can this be done?
Thanks in advance
JS
awk -v sq="'" -F "[}{]" '
/^awk/ && n == 0 { print "awk", sq, "BEGIN {" }
/^awk/ { print $2; n = 1 }
n == 1 && ! /^awk/ { n = 0; print "}" sq }
! /^#/ { print }
END { print "}" sq }
' "$FILE"
#
jisha
3
Hi Johnson ,
I got the out as below :
awk ' BEGIN {
printf ("%1s", "man" )
awk 'BEGIN { printf ("%1s", "man" )} '
printf ("%9s", "women" )
awk 'BEGIN { printf ("%9s", "women" )} '
printf ("%56s", "human")
awk 'BEGIN { printf ("%56s", "human")} '
}'
echo "$!"
awk ' BEGIN {
printf ("%1s", "aaa" )
awk 'BEGIN { printf ("%1s", "aaa" )} '
printf ("%19s", "bbb" )
awk 'BEGIN { printf ("%19s", "bbb" )} '
printf ("%24s", "ccc" )
awk 'BEGIN { printf ("%24s", "ccc" )} '
}'
echo "$!"
awk ' BEGIN {
printf ("%1s", "rrr")
awk 'BEGIN { printf ("%1s", "rrr")} '
printf ("%31s", "ddd")
awk 'BEGIN { printf ("%31s", "ddd")}'
printf ("%43s","eeee")
awk 'BEGIN { printf ("%43s","eeee")}'
}'
}'
Am trying to tune the code a bit so tat I can reach to the required o/p
Thanks a lot for ur time
JS
jisha
4
Am still not getting it solved ..
era
5
This puts the closing quote on a separate line, maybe that's acceptable?
awk -v sq="'" '
{ if (/^awk .BEGIN { /){
if (prev) gsub("awk " sq "BEGIN", "");
gsub ("} *" sq, "}");
prev = 1;
} else {
if (prev) printf ("%c\n", sq);
prev = 0;
}
print }
END { if (prev) printf "%c\n", sq }' 1.txt
You are missing a closing parenthesis in some of the printfs in the input file, but I guess we weren't supposed to solve that too.