- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: AWK getline help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2007 11:20 PM
тАО10-23-2007 11:20 PM
I am using awk to filter a file, below is syntax that has been given to me in a previous thread however I cannot seem to get it to work:
awk 'BEGIN{while (getline LINE < "allservers.csv" > 0) {a[++$i]=LINE}}' serverlistfinal.txt
error message is:
awk: Field $() is not correct.
any help is greatly appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2007 11:42 PM
тАО10-23-2007 11:42 PM
Re: AWK getline help
So what is the "$i" supposed to do?
I suspect you just want 'i', no $
In PERL the variablea are $i and such
in AWK the $x referer to auto-split fields sucht as $1 for the first, $2 for teh second, $NF for the last, from the last line processed in $0.
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2007 11:47 PM
тАО10-23-2007 11:47 PM
Solution# awk 'BEGIN{while ((getline LINE < "/etc/hosts") > 0) {a[++i]=LINE}}' serverlistfinal.txt
...as for example:
# awk 'BEGIN{while ((getline LINE < "/etc/hosts") > 0) {a[++i]=LINE}};END{for (i=1;i<10;i++) {print a[i]}}' /dev/null
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2007 11:56 PM
тАО10-23-2007 11:56 PM
Re: AWK getline help
Yeah, that example was wrong in syntax and possibly also in intent.
What you really want os to build a use 'associative array' , once you found a good 'key' from the files involved.
Rigth not, the tables listed in the other topic do NOT show an obvious match fields.
But lets assume, that field #4 (just a random pick, not the right pick!) in the first input file is a lun number, and field#6 there provide useful data.
And assume that for example field 2 in the second file is lun
Now you want a STUCTURE (not exact code) like:
BEGIN {
while (getline f1_line < "file") {
split (f1_line,f1_fields,",");
f1_luns[f1_fields[2]] = f1_fields[6];
}
}
{
:
if ($2 in f1_luns) { ...
Clear?
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2007 12:09 AM
тАО10-24-2007 12:09 AM
Re: AWK getline help
Thanks for the input ...
Hein, I am using the same code for a different purpose - I could use grep etc but still attempting to improve my awk skills.
I have got James example to work ...
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2007 12:15 AM
тАО10-24-2007 12:15 AM
Re: AWK getline help
I have a file serverlistfinal.txt that contains 700 + servers in our environment.
the file allservers.csv is a file that needs updating and has over 1000 entries therefor I need to print all rows from allservers.csv that are in serverlistfinal.txt.
I figure I change the syntax "i=1;i<10;i++" as this displays the top 10 and also > 0 is not relevant in this code?
Chris.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2007 12:25 AM
тАО10-24-2007 12:25 AM
Re: AWK getline help
> I figure I change the syntax "i=1;i<10;i++" as this displays the top 10 and also > 0 is not relevant in this code?
You could count the elements read by the 'getline' and use that value as the upper bound for your later 'for' loop (of course).
As for the:
while (getline LINE < "file" > 0)
...'getline' returns one (1) if it successfully reads a record; A zero (0) is returned for EOF (end-of-file). A -1 is returned if for instance, the file cannot be opened.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2007 12:25 AM
тАО10-24-2007 12:25 AM
Re: AWK getline help
awk 'BEGIN{while (getline LINE < "allservers.csv") {a[++i]=LINE}};{print $0}' serverlistfinal.txt
is the solution
:-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2007 12:39 AM
тАО10-24-2007 12:39 AM
Re: AWK getline help
But that's so boring!
What are you going to do with the data from that array later? Run it through a loop 'looking' for stuff?
>> Hein, I am using the same code for a different purpose - I could use grep etc but still attempting to improve my awk skills.
Exactly! That's precisely why I hinted you towards picking a useful key for the array, not just a list of numbers 1..end.
With a good key/index you can do a direct lookup suing the 'i in array' construct. You can also readily 'delete' specif array elements which are (no longer) useful.
Check it out. It's useful and will bring you to a whole new level in awk usage, getting you ready for... perl
:-)
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2007 12:51 AM
тАО10-24-2007 12:51 AM
Re: AWK getline help
the solution I suggested above does not actaully do what i need it to ....
I have some good idea's here so will continue to work through my book and come up with what I need ...
I finally want all rows found in serverlistfinal.txt to be printed with an extra field (delimited with ",")supported to another file and if the line does not exist then print not supported ie:
Server,DNSNAME,Deployed,Active,United Kingdom,ADDRESS,REU-Unix,REU-Config,support
or
Server,DNSNAME,Deployed,Active,United Kingdom,ADDRESS,REU-Unix,REU-Config,unsupport
make sense?
anyhow I shall continue with this and let you know.
cheers
chris