Read input file with in awk script not through command line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read input file with in awk script not through command line
# 1  
Old 06-21-2013
Read input file with in awk script not through command line

Hi All,

Do we know how to read input file within awk script and send output toanother log file. All this needs to be in awk script, not in command line. I am running this awk through crontab.

Code:
 
Cat my.awk
#!/bin/awk -f
function test(var){
some code}
{
}
END
{
print"test code"
}

# 2  
Old 06-21-2013
What do you want to achieve?
Just some simple redirection for which you do not need awk at all, or
plain processing of a file by awk and redirecting the output, or
processing a file of input with awk while having a 2nd file being processed too?
# 3  
Old 06-21-2013
Processing a file of input and redirecting the output to another file. How Would I put input file name and out put file name in awk script, not thru command line
# 4  
Old 06-21-2013
I am not sure if I understand - the explanation seems so simple in regards to your former thread https://www.unix.com/shell-programmin...ent-lines.html

Anyway, you use awk in a shell script the same way as you do that in a shell.
In your code like example, you used -f to give awk a script file. The last argument is always the file name of the file to be processed (for example data, logs...).

Code:
awk '{print $1}' infile > outfile
#or
awk '{print $1 >> outfile}' infile

I am not sure if this hits what you are looking for - else a bit more detailed explanation could help, or maybe another poster understands it, sorry.
# 5  
Old 06-21-2013
Thanks, Assumes input file name is test.log on which my.awk script runs and it gives output to output.log

I can run this script by this command through command line
Code:
./my.awk test.log > output.log

How can I put this test.log and output.log in my.awk so that if I run below command it should work.
Code:
./my.awk


Code:
 
Cat my.awk
#!bin/awk -f
function(time){
}
{
action####
}
END{
print(""')
}

# 6  
Old 06-21-2013
I see no easy way to do that, that is not awkward. Neither do I see a reason why this has to be done. What is the reason if I may ask? Why not just use a shell script that calls awk?

Maybe someone else has an idea how to do that the way you want it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

Error to Read Input from command line

Team , I am trying to write a case condition for database backups.But I am unable to make the script to read input from command line . while true ;do read -p "Do You Wish To Take Database Backup ?? " yn case $yn in *) echo " YES take backup ";; *) echo " NO BACKUP " ;; ... (9 Replies)
Discussion started by: rocking77
9 Replies

3. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

4. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

5. Shell Programming and Scripting

Script to read command line input and change it to some form

Hi, I want to write a small code in which script changes command line input to some form. Example script.sh a1 a2 a3 a4 ..... output should be "a1|a2|a3|....." Number of inputs in command line can be any variable (2 Replies)
Discussion started by: Raza Ali
2 Replies

6. UNIX for Dummies Questions & Answers

Bash script to delete file input on command line

1) I wrote a script and gave the desired permissions using "chmod 755 scriptname". Now if i edit the script file, why do i need to set the permission again? Didn't i set the permission attribute.. or if i edit the file, does the inode number of file changes? 2) I am running my unix on a server... (1 Reply)
Discussion started by: animesharma
1 Replies

7. Shell Programming and Scripting

awk script file command line options

Being new to awk I have a really basic question. It just has to be in the archives but it didn't bite me when I went looking for it. I've written an awk script, placed it in a file, added the "#!/usr/bin/awk -f" at the top of the script and away I go. "% myAwk <inputfile>" gives me exactly what... (2 Replies)
Discussion started by: tomr2k
2 Replies

8. Shell Programming and Scripting

sed to read line by line and input into another file

I have two files. Fileone contains text string one text string two text string three Filetwo contains Name: Address: Summary: Name: Address: Summary: Name: Address: Summary: I would like to use sed to read each line of file one and put it at the end of the summary line of file... (3 Replies)
Discussion started by: dolacap
3 Replies

9. Shell Programming and Scripting

How can I send the input of a read line command through a shell script

Hi All, I wish to automate the unix command 'su' through a shell script. I would like to pass the content of a file as password to 'su' command. My script is as below, #! /bin/sh su userA while read line do rpm -ivh $line done < pwd.txt where pwd.txt contains the password of... (6 Replies)
Discussion started by: little_wonder
6 Replies

10. Shell Programming and Scripting

read a file as input and pass each line to another script

Hi, I am trying to write a ftp script which will read a file for filenames and ftp those files to another server. Here's my ftp script, but it's scanning the current directory for file names. My question is how can I pass multiple files (these files will have the name of data files that need to... (0 Replies)
Discussion started by: sajjad02
0 Replies
Login or Register to Ask a Question