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
File exists routine kamathg Shell Programming and Scripting 9 12-16-2008 04:56 AM
how to cp files to dir,using routine? wrapster Shell Programming and Scripting 4 05-21-2008 10:41 PM
how to cp files to dir,using routine? wrapster UNIX for Advanced & Expert Users 1 05-21-2008 02:18 PM
sub routine call sekar sundaram Shell Programming and Scripting 2 11-10-2005 08:30 PM
Entry Points Routine S.P.Prasad High Level Programming 9 10-21-2002 11:34 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-21-2009
fwellers fwellers is offline
Registered User
  
 

Join Date: Mar 2008
Location: Northern, VA
Posts: 23
awk routine help

Hi,
I use awk but not as a programming language. Just generally in piplelines to split things out by fields.
I am trying to accomplish this one thing that I think a short awk routine would do great for, but can't figure it out.

Lets say I have a file that contains database columns. The file looks like this:
col1
col2
col3
..

I want awk to go through that file and turn each line into an array element so that I can do a printf statement that would contain all the lines in the file in one statement. The statement would look something like this:

printf("%s,%s,%s\n", array[0],array[1],array[2])

Actually, I guess the array elements would be variablized also.

Is this a simple thing i can get some help with ?

Thanks,
Floyd
  #2 (permalink)  
Old 04-21-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
Code:
tr -s '\n' ' ' < oldfile > newfile
  #3 (permalink)  
Old 04-21-2009
fwellers fwellers is offline
Registered User
  
 

Join Date: Mar 2008
Location: Northern, VA
Posts: 23
That's pretty cool. thanks. It doesn't fully solve for me though.
I should have mentioned I have to add some to it, and add commas.
So I can use the tr command to go to newfile, then:
cat newfile|awk '{printf("mydata,moredata,%s,%s,%s\n",$1,$2,$3) }'

but, even that is not a full solution yet, because how will the script know how many fields are in the file, and be able to decide how many elements to put on the right side of the printf statement ?

For eg, if the file had 10 columns that the tr command turned into one line with 10 fields, how will I create a print statement that will know there were 10 fields so I can use them ?

Thanks,
floyd
  #4 (permalink)  
Old 04-21-2009
colemar colemar is offline
Registered User
  
 

Join Date: Apr 2009
Location: Trento, Italy
Posts: 116
Can you state the requirements in natural language?

Perhaps you want to transpose a column of unknown length to a row whose elements are separated by commas, and a fixed number of additional elements are to be added at the row start.
Code:
colemar@deb:~$ cat inputfile 
one
two
three
four
five
colemar@deb:~$ echo red,blue,white,$(tr '\n' ',' < inputfile)
red,blue,white,one,two,three,four,five,
  #5 (permalink)  
Old 04-21-2009
fwellers fwellers is offline
Registered User
  
 

Join Date: Mar 2008
Location: Northern, VA
Posts: 23
natural language ? Sorry I was doing my best to communicate my needs.

the script will parse a file that has an unknown number of lines. Each line contains a column name. For eg file1 looks like this:
firstname
lastname
email

...
I will have a hardcoded string or two. say, "token__a", and "audit_action__a"

My final output from parsing that file will look like this:
..... insert into $table ( token__a, audit_action__a, firstname,lastname,email ) ....

Thanks,
floyd
  #6 (permalink)  
Old 04-21-2009
fwellers fwellers is offline
Registered User
  
 

Join Date: Mar 2008
Location: Northern, VA
Posts: 23
Quote:
Originally Posted by colemar View Post
colemar@deb:~$ cat inputfile
one
two
three
four
five
colemar@deb:~$ echo red,blue,white,$(tr '\n' ',' < inputfile)
red,blue,white,one,two,three,four,five,[/code]
Ok Colemar, I just saw this latest change you did. It will work.
Thank you !

Obviously you were able to wade through my unatural language in order to determine what I was asking for. Good job :-)

Thanks again.

Floyd
  #7 (permalink)  
Old 04-21-2009
colemar colemar is offline
Registered User
  
 

Join Date: Apr 2009
Location: Trento, Italy
Posts: 116
Quote:
Originally Posted by fwellers View Post
Obviously you were able to wade through my unatural language in order to determine what I was asking for.
Of course I didn't mean that your language was unnatural, just that you were partly using code to suggest how to solve the problem instead of explaining the problem in plain english.
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 09:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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