![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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, |
|
||||
|
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 ... 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 |
|
||||
|
Quote:
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 |
|
||||
|
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 | ||
|
|