The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 11:00 AM
doubt about awk soujanya_srk UNIX for Dummies Questions & Answers 2 01-10-2008 05:26 PM
SDL doubt royalibrahim High Level Programming 0 11-22-2007 01:49 AM
awk doubt.. esham Shell Programming and Scripting 11 10-11-2007 02:24 AM
ftp doubt ranj@chn Shell Programming and Scripting 0 01-04-2006 08:47 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-15-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
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
  #2 (permalink)  
Old 04-15-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

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


#
  #3 (permalink)  
Old 04-15-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
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
  #4 (permalink)  
Old 04-16-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
Am still not getting it solved ..
  #5 (permalink)  
Old 04-16-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0