LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 09-28-2004, 04:19 PM   #1
saiz66
Member
 
Registered: Apr 2003
Posts: 225

Rep: Reputation: 30
[cshell] awk help!


Hi. I have a file where I would like to put all the first word in every line into a an array.. how could this be done? thanks a lot!!!!
 
Old 09-28-2004, 05:45 PM   #2
kbcnetau
Member
 
Registered: Dec 2003
Location: South Australia (ex-Devon, UK)
Distribution: SuSE, Slackware, Fedora, Debian, Knoppix
Posts: 141

Rep: Reputation: 15
I will endeavour to explain:

Whitespace is recognised by awk as the default delimiter (if I remember correctly), so what you are trying to is to pick up the first field of a line. Variables in awk are $0 (whole line), $1 (first word), $2 (second word), etc., so that first word is going to be $1.

Try this:
cat /etc/hosts | awk '{print $1}'

...substituting your filename for /etc/hosts. This just prints the first word of each line.

No try:
cat /etc/hosts | awk '{ x[NR]=$1 } END { for (i=0; i<NR; i++) print x[i] }'

Looks the same? It should. The only thing that we've done different is to read the input into an array (x) and then print out the values of the array.

Looking closer, the assignment to the array is this part:
{ x[NR]=$1 }
NR is an internal variable in awk indicating the number of the row being processed. So, what we are doing is pushing that first word ($1) into position NR of array x.

The next part:
END ....

The END means "do this after you've finished everything else". So, once the array has been populated, start the next block. The structure of the remaining part should be familiar as it's just a for loop, printing every value of an array from x[0] to x[NR].

If you're looking to do some serious awk'ing, I'd suggest you have a look at "The AWK programming Language" by Aho, Kernighan and Weinberger - the guys who invented the awk language. Brian Kernighan is the Kernighan of Kernighan and Ritchie (C) fame.

Hope this helps.
 
Old 09-28-2004, 06:26 PM   #3
saiz66
Member
 
Registered: Apr 2003
Posts: 225

Original Poster
Rep: Reputation: 30
wow thanks a lot!!!... I am having a lot of problems getting all my ( ) { } ` ` ' ' and " " mixed up... do you have any programming books or tutorials you can recommend???

I wanted to modify it so it would write a message to the usernames.. but I got a syntax error

Code:
cat users.txt | awk '{ x[NR]=$1 } END { for (i=0; i<=NR; i++) write x[i] < test.txt}'

Last edited by saiz66; 09-28-2004 at 06:41 PM.
 
Old 09-28-2004, 06:55 PM   #4
kbcnetau
Member
 
Registered: Dec 2003
Location: South Australia (ex-Devon, UK)
Distribution: SuSE, Slackware, Fedora, Debian, Knoppix
Posts: 141

Rep: Reputation: 15
Hmm - for starters, I don't think that there's a 'write' command in awk...

I can't say I've used awk so much that I could give you an instant solution to the problem. The book I recommended in my previous post has lots of examples, which would be a good way to learn.

Personally, I only use awk for little one-liners. To do what you are trying to do, I'd be inclined to tackle it using Perl. See below for very rough (and untested) example.

An awk-xpert may be able to give you further awk guidance ;-)

#!/usr/bin/perl -wT
use strict;
my $message<<EOT; # Your message goes between this line and EOT
hello, world
EOT
open(FOO, "users.txt"); # open the users file
while (<FOO>) # for each line of the users file...
{
my @ary=split(/ /,$_); # chop line into words
print FOO "$ary[0] $message\n";
}
close FOO;
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
About awk script sachin_keluskar Linux - Software 2 06-24-2005 03:19 AM
awk newbie eremit Programming 2 03-27-2005 07:08 AM
Cshell querying two files... HiOctane21 Programming 1 05-01-2004 07:19 PM
using awk wedgeworth Linux - Newbie 9 02-20-2004 07:48 AM
awk question dilberim82 Programming 2 10-01-2003 07:12 PM

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

All times are GMT -5. The time now is 03:13 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration