The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK doubt Rafael.Buria Shell Programming and Scripting 2 02-06-2008 07:00 AM
doubt about awk soujanya_srk UNIX for Dummies Questions & Answers 2 01-10-2008 01:26 PM
SDL doubt royalibrahim High Level Programming 0 11-21-2007 09:49 PM
awk doubt.. esham Shell Programming and Scripting 11 10-10-2007 11:24 PM
ftp doubt ranj@chn Shell Programming and Scripting 0 01-04-2006 04:47 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-15-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 128
Arrow doubt in awk

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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-15-2008
cfajohnson's Avatar
Registered User
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 487
Code:
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"


#
Reply With Quote
  #3 (permalink)  
Old 04-15-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 128
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
Reply With Quote
  #4 (permalink)  
Old 04-15-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 128
Am still not getting it solved ..
Reply With Quote
  #5 (permalink)  
Old 04-16-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,111
This puts the closing quote on a separate line, maybe that's acceptable?

Code:
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.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0