LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2018, 10:32 AM   #1
tombro
LQ Newbie
 
Registered: Mar 2009
Location: Sunny Florida, USA
Distribution: Fedora 30
Posts: 13

Rep: Reputation: 1
Specifying file for awk to parse.


Good Folks,
I have a gawk script that parses a file created in the BEGIN section.
The script works perfectly, but I have to include the dynamically created file on the command line.
An example begin section:
Code:
BEGIN {
  system("ls -1 > /tmp/foo.tmp")
}
If the script name is foo.awk, the command line must be:
Quote:
foo.awk /tmp/foo.tmp
I know I can write a shell script that fires off the above command line, but is there a way to somehow specify the input file within the gawk script itself?
 
Old 01-18-2018, 02:08 AM   #2
Stéphane Ascoët
Member
 
Registered: Feb 2004
Location: Fleury-les-Aubrais, 120 km south of Paris
Distribution: Devuan, Debian, Mandrake, Freeduc (the one I used to work on), Slackware, MacOS X
Posts: 251

Rep: Reputation: 49
I don't understand what you want to do exactly. The obvious for me would be to get the $1 coming from the command line, but since you're asking I guess I probably miss the whole problem...
 
Old 01-18-2018, 06:31 AM   #3
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,517

Rep: Reputation: Disabled
I agree, better to use a replacable parameter, as then you can use any input file just by naming it when you run awk.
 
Old 01-18-2018, 06:45 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,030

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
you do not need a file at all, use:
Code:
awk '
.....
cmd = "ls -l"
while ( ( cmd | getline ) > 0 )
 ....'
but if you want to specify anything, use:
Code:
awk -v var=filename '
......
system("ls -l > " filename)
....
'
 
Old 01-19-2018, 01:45 PM   #5
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,156

Rep: Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266
What's wrong with not having a file?

Code:
ls -1 |awk ...
 
Old 01-21-2018, 11:54 PM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.
First of all, I agree with all opinions above and suggest you to consider them carefully.
But, it is indeed possible to specify a file to process from the awk script itself. Here's an example:
Code:
$ awk 'BEGIN{ARGV[1]="/proc/cpuinfo"; ARGC=2} {print}'
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 58
model name      : Intel(R) Core(TM) i7-3517U CPU @ 1.90GHz
stepping        : 9
microcode       : 0x15
cpu MHz         : 1291.218
cache size      : 4096 KB
physical id     : 0
siblings        : 4
core id         : 0
.....
And, of course, one can also read a file manually using getline.
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] AWK - How to parse a Web log file to count column and the last occurrence of that column Alvin88 Linux - Newbie 10 06-23-2017 05:59 AM
[SOLVED] awk to parse time and file transfer size jzoudavy Linux - Newbie 36 10-14-2015 09:38 AM
parse file with awk and sum totals master-of-puppets Programming 20 09-30-2014 01:06 AM
Help needed for using awk to parse a file to make array for bash script tallmtt Programming 12 04-14-2012 01:16 PM
ssimple shell script to parse a file ~sed or awk stevie_velvet Programming 7 07-14-2006 03:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration