AWK Filename as variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AWK Filename as variable
# 1  
Old 04-16-2004
AWK Filename as variable

Need some help.

I need to load data into some Oracle tables and one of the pieces of data that I need to load is the filename. This filename is distinct every single time. Basically the last 6 characters will be different with no pattern.

ex. testfile_041504_003567

To load the filename into Oracle I thought about using awk to place the filename before every record in the file and then I can load it based on the position. This portion works ok, but I'm stuck with the hard part.

awk ' {print FILENAME,$0} ' filename> filename.dat

I tested this process manually, by typing in the filename so that AWK could utilize the print feature. I want to automate this thing, but the distinct filename has stumped me.

How can I feed the filename to awk as a variable?

I will have numerous files I need to load on a daily basis, so it will need to loop through all of the files?

Sorry for the long post. Appreciate all and any help.
# 2  
Old 04-16-2004
Here is a pretty good AWK Tutorial. I have had good luck with it anyway.

As for your question, to pass a variable into Awk, try using the -v switch.

awk -v FILE=$FILE_NAME '{print FILE, $0}'

You can get all of the filenames into single file using the following command sequence. Assuming the directory only has the list of files in it, no other sub directories.

Code:
DIRECTORY=/some/directory
FILE_NAME="my-file-list"

for i in `ls -1 $DIRECTORY`
 do 
   echo $i >> $FILE_NAME
done


Last edited by google; 04-16-2004 at 01:42 AM..
# 3  
Old 04-16-2004
Thanks google.

I don't want to put all of the files into the same file though. I'm using the initial awk command to place the filename as the first couple characters of every row. If I place all the files into one file I will lose the original filename. Right?

The second piece looks good because it looks like you are listing all of the files in the directory. How does awk know to grab each file and then output the results via the print command?
# 4  
Old 04-16-2004
In awk the FILENAME varable is built-in, i.e. you don't need to define it.

Try this....

awk '{print FILENAME, $0 > FILENAME ".dat"}' file*
# 5  
Old 04-16-2004
Thanks so much, this worked like a charm
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue when printing filename through cygwin using a variable with awk

Hi, If i were to do this an print out the file, it will show as it is in the command $ awk '/Privilege Use/ {P=0} /Object Access/ {P=1} P' AdvancedAudit.txt Object Access File System No Auditing Registry No Auditing Kernel... (1 Reply)
Discussion started by: alvinoo
1 Replies

2. Shell Programming and Scripting

Call a awk script with variable and input filename

HI, MY question is a very simple one: if i want to call an awk script with the input file name and also pass a variable value , then how to do it. #>awk -f my_script.awk -v variable=value my_inputfile.txt I can't do it like this. throws error: awk: my_script.awk:18:... (0 Replies)
Discussion started by: Onkar Banerjee
0 Replies

3. Shell Programming and Scripting

Passing gunzipped filename to a variable

Hi , Am trying to gunzip a file and pass the gunzipped file name to a variable , but its not taking up the value. Am trying to execute the command f=`gunzip <filename>`;echo $f. Here the file is getting gunzipped but the file name is not assigned to the variable. Any help on this will be useful.... (5 Replies)
Discussion started by: rogerben
5 Replies

4. Shell Programming and Scripting

Change the filename variable value

Hi guys, I have a variable where i am storing the filename (with full path). I just need the value before ".txt". But instead of getting the filename i am getting the contents of the filename. FileName=/appl/data/Input/US/Test.txt a=`awk -F"." '{print $1}' ${FileName}` echo $a... (3 Replies)
Discussion started by: mac4rfree
3 Replies

5. Shell Programming and Scripting

Set the last 4 letter in the filename as a variable

Hi all, I want to set the last 4 letter in the filename as a variable. For example, i have AB1234.txt file and i need to have last 4 letter as a variable. It should be like ; get last four letter set var = 1234 How can i write this in C shell?? Thanks, zibi (3 Replies)
Discussion started by: zibi
3 Replies

6. Shell Programming and Scripting

Inserting variable value into filename

Greetings, people of UNIX/Linux forums. I am having a problem with a script, where I am trying to create a new variable. The value of this variable would be dependent on the value in a couple other previous variables (all variables are 2-digit integers). Here is my code: #set the stations... (3 Replies)
Discussion started by: TheSMan5
3 Replies

7. Shell Programming and Scripting

variable used as filename

Hello, i'm fairly new to scripting, so please bear with me (I did try looking this up first, i figured it had to have been asked already). #!/bin/bash fileName=`date | sed -n 's/ /_/g p' | sed -n 's/^/Backup_/p' | sed -n 's/$/\.tar/p'`; #THIS SETS BACKUP_DATE echo $fileName #TEST OF VALUE ... (4 Replies)
Discussion started by: jzacsh
4 Replies

8. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

9. Shell Programming and Scripting

Variable value not being fetched in the filename..

Hi, In the code below, the $DD1, $DD2, $MM1, $MM2 are not fetching the values. Can anybody tell me where have i made wrong. Shift_date.sh is a script which gives the previous date if we give the current date in the format YYYYMMDD and +/- how many days past/future.. Like Shift_date.sh... (3 Replies)
Discussion started by: RRVARMA
3 Replies

10. Shell Programming and Scripting

mv Filename variable to another filename

Anyone who can assist : I am trying to pass the group vairiable to a filename: rpt_tsavegrp=/export/legato/scripts/$group_savegrp_rpt.$dat It will not pass to variable. Anyone have any ideas what I am doing wrong here. Thanks # This script sends email that save group completed.... (3 Replies)
Discussion started by: gzs553
3 Replies
Login or Register to Ask a Question