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 and shell scripting languages here.

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

Join Date: Jul 2007
Posts: 138
help on awk

Hi,

i have many lines (each line will have a number) in a text file as below:

111
222
333

and i need to format it as below

'111',
'222',
'333');

Please note that there is no comma (,) after last entry but closed with );

I guess it can be done using awk but could not succeed. Please advise.

TIA
Prvn
  #2 (permalink)  
Old 06-23-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,431
What have you tried so far and where exactly are you stuck?
  #3 (permalink)  
Old 06-23-2008
prvnrk prvnrk is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 138
/usr/local/bin/awk -v x="'" '{ s=s sprintf(x "%s" x "," "\n", $0) } END { sub(",$", "", s); print(s) }' <file>

gives me the below output:

'111',
'222',
'333',


Not able to get the last "," removed.
  #4 (permalink)  
Old 06-23-2008
NYankz NYankz is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 12
Here try this:
cat file.txt|wc -l| awk '{$1=$1-1;system("head -"$1" file.txt")}'| awk '{printf("%c%s%c\n",39,$1,39)}'; tail -1 file.txt | awk '{printf("%c%s%c);",39,$1,39)}'
  #5 (permalink)  
Old 06-23-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,431
Quote:
Originally Posted by NYankz View Post
Here try this:
cat file.txt|wc -l| awk '{$1=$1-1;system("head -"$1" file.txt")}'| awk '{printf("%c%s%c\n",39,$1,39)}'; tail -1 file.txt | awk '{printf("%c%s%c);",39,$1,39)}'
Wow, let's see:
The Useless Use Of cat, wc, awk, head, tail Award
Only sed and the job is done:
Code:
sed -e "s/.*/'&',/" -e '$ s/.$/);/' data.file
Regards,
  #6 (permalink)  
Old 06-24-2008
NYankz NYankz is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 12
Quote:
Originally Posted by danmero View Post
Wow, let's see:
The Useless Use Of cat, wc, awk, head, tail Award
Only sed and the job is done:
Code:
sed -e "s/.*/'&',/" -e '$ s/.$/);/' data.file
Regards,
i'm still learning too, thx for the tip, don't need to be rude though.
  #7 (permalink)  
Old 06-24-2008
prvnrk prvnrk is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 138
Thanks Danmero, NYankz and Marcus,

All your solutions worked.


Prvn
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 08:25 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