awk routine help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk routine help
# 1  
Old 04-21-2009
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  
Old 04-21-2009
Code:
tr -s '\n' ' ' < oldfile > newfile

# 3  
Old 04-21-2009
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  
Old 04-21-2009
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  
Old 04-21-2009
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  
Old 04-21-2009
Quote:
Originally Posted by colemar
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  
Old 04-21-2009
Quote:
Originally Posted by fwellers
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to Parse An Inherited Command/Routine

I am am one of these people that it isn't good enough just to say, "Here, try this...". it is important for me to understand how and why something works (or doesn't work.) All that being said, I am trying to parse out a command that we use that was handed down to me by someone I can no longer... (3 Replies)
Discussion started by: he204035
3 Replies

2. Shell Programming and Scripting

PERL: Calling a sub routine from another module - help!!!

Hi, I am an occasional PERL user. I am trying to call a sub routine (passing parameters) in perl module from a driver .pl file. I have been "tinkering" for a while now and have confused myself. Could someone please look at the code below and spot where I am going wrong. testPerl.pl ... (0 Replies)
Discussion started by: chris01010
0 Replies

3. Shell Programming and Scripting

Paramerter pass for function(sub routine) need help

Hi, Please help me here while passing the paramert to fuction i am facing problem. i tryied passing 7 PARAMeter in side single quote,double quate even tried tild sign not working. how can assign it properly . usage () { typeset -i NumPARAMs=$1 typeset -i PARAM1=$2 typeset PARAM2=$3... (3 Replies)
Discussion started by: nitindreamz
3 Replies

4. Shell Programming and Scripting

Perl - Call a sub routine in a command

Hi all I have written a simple perl script that has different options i.e. myscript -l -p etc i have it so when it runs without any switches it runs a subroutine called nvrm_norm i want to be able to do a -p option and run pall -w -f and then called the subruotine pall is... (1 Reply)
Discussion started by: ab52
1 Replies

5. Shell Programming and Scripting

File exists routine

Hello experts, I need some help here.. I've written the following routine to check for existence of files. The routine does the following. It will look for a compressed ( .Z ) file and if it exists, then it will uncompress it, if it is already uncompressed, then it will just diplay a message... (9 Replies)
Discussion started by: kamathg
9 Replies

6. Shell Programming and Scripting

how to cp files to dir,using routine?

hi all, I wanted to know how we can copy files to dirs, through a routine and when the file and the dir are specified as parameters for that routine and explicitly called? Eg: suppose i want to copy file1 to /tmp then myproc() { . . } myproc /path/file1 /tmp/ These parameters when... (4 Replies)
Discussion started by: wrapster
4 Replies

7. UNIX for Advanced & Expert Users

how to cp files to dir,using routine?

hi all, I wanted to know how we can copy files to dirs, through a routine and when the file and the dir are specified as parameters for that routine and explicitly called? Eg: suppose i want to copy file1 to /tmp then myproc() { . . } myproc /path/file1 /tmp/ These parameters when... (1 Reply)
Discussion started by: wrapster
1 Replies

8. Shell Programming and Scripting

sub routine call

in windows machine... C:\2\test>perl -version This is perl, v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) ------------------------------------------ what is the difference b\w subroutine calls: sub_routine_name("-----"); and ... (2 Replies)
Discussion started by: sekar sundaram
2 Replies

9. UNIX for Dummies Questions & Answers

Routine Task being a Solaris Administrator

Hi, What are routine task being a solaris administrator ? Thanks, Far (1 Reply)
Discussion started by: Far
1 Replies

10. Programming

Entry Points Routine

How do we pronounciate bdevsw and cdevsw Kernel resources ? I presume it as block or charcter device software table. Am I Correct in my assumption ? Thanks in advance. (9 Replies)
Discussion started by: S.P.Prasad
9 Replies
Login or Register to Ask a Question