LinuxQuestions.org
Review your favorite Linux distribution.
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 05-17-2007, 03:35 PM   #1
jimyg10
LQ Newbie
 
Registered: May 2007
Posts: 14

Rep: Reputation: 0
Reading from file...


i want to read from a file and for every word to do a command.. I use a for loop inside awk for reading all the words inside the file.. Then i want to use grep, but it's a problem because the exit of awk are many results and not one..

for example if file has:
aaaa bbbb cccc dddd eeee
i want to execute:
grep aaaa, grep bbbb...e.t.c

Any idea??

Last edited by jimyg10; 05-18-2007 at 01:45 PM.
 
Old 05-17-2007, 05:49 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jimyg10
i want to read from a file and for every word to do a command.. I use a for loop inside awk for reading all the words inside the file.. Then i want to use grep, but it's a problem because the exit of awk are many results and not one..

for example if file has:
aaaa bbbb cccc dddd eeee
i want to execute:
grep aaaa, grep bbbb...e.t.c

Any idea??
I don't know if i was clear enough...
you don't need to use a for loop to read records from a file in awk. from what i see your requirement, you can do:
Code:
egrep "aaaa|bbbb" file
will be good if you post your code.(samples of input and output format as well if possible)
 
Old 05-17-2007, 06:00 PM   #3
jimyg10
LQ Newbie
 
Registered: May 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Basicly, what i want to do is: i have a file (users.txt) that has the words like this:
aaa bbb ccc ddd eee...
i want the script to take each word and do a command..

I used the command awk to read each word from the file...But then??

Last edited by jimyg10; 05-18-2007 at 01:47 PM.
 
Old 05-17-2007, 06:08 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You don't need to use awk for this. You could simply use:
for user in $(cat users.txt); do
...
done
 
Old 05-17-2007, 06:16 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Have you considered the who command?

Also, look at the system() function built in to gawk.
 
Old 05-17-2007, 06:20 PM   #6
jimyg10
LQ Newbie
 
Registered: May 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jschiwal
You don't need to use awk for this. You could simply use:
for user in $(cat users.txt); do
...
done
thnx a lot..

Last edited by jimyg10; 05-18-2007 at 01:45 PM.
 
Old 05-17-2007, 06:23 PM   #7
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Read info who and note the discussion re utmp and wtmp.
 
Old 05-17-2007, 06:43 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jimyg10
Basicly, what i want to do is: i have a file (.txt) that has the names of all the users like this:
aaa bbb ccc ddd eee...
i want the script to take each user and show if he is online or not and the last time he logged in...

I used the command awk to read each user from the file...But then??
Code:
who | awk 'BEGIN { 
	    getline line<"file" #assume only 1 line
	    n=split(line,user)	      
	   } 
END{ 
  for (i=1;i<n;i++) {
     if ( $1 == user[i]) { 
        print $1 "  logged in"
	break 
    }
  }
}
 
  


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
QT file reading example fuzzhead85 Programming 7 06-24-2012 02:24 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
Reading text file-writting binary file cdog Programming 5 06-13-2006 11:56 AM
awk: fatal:cannot open file for reading (no such file or Directory) in Linux sangati vishwanath Linux - Software 4 07-06-2005 12:59 AM
Script, Reading a file, When end of file? elibm Programming 2 07-16-2001 11:01 AM

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

All times are GMT -5. The time now is 01:24 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
Open Source Consulting | Domain Registration