LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-07-2009, 12:15 PM   #1
dots
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Rep: Reputation: 0
Using variables with awk


I need to use the ps -eaf command and awk to provide a report that will look like this:
USER = dbus
PID PROGRAM TTY
1538 dbus-daemon (?)

USER = rangel
PID PROGRAM TTY
9531 SCREEN (?)
13434 /bin/bash (pts/18)
13502 /bin/bash (pts/22)

USER = root
PID PROGRAM TTY
1 /sbin/init (?)
2 [kthreadd] (?)
3 [migration/0] (?)
I understand that I'm going to need a while loop and I think I can do the formatting, but I'm not sure how to get the user id stored as a variable. Am I even looking at going about this the right way? What is the best way to do this? Thanks for the help!
 
Old 07-07-2009, 12:51 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
if you want to use awk you wouldn't use a while loop, as awk is for processing text streams, so naturally iterates over its inputs. Additionally using ps will sort by uid with the right options so it's actually really simple from then on, to just keep track of the current uid and print a new header each time the uid of the current record changes.

Code:
{
  if (userid != olduserid) {
    # print the headers out
  }
  # print the process data
  
  olduserid = userid
}
I'm a bit concerned that this is homework though, so i'm not going into any more detail at the moment. Should be fairly simple from here though.

Last edited by acid_kewpie; 07-07-2009 at 12:52 PM.
 
Old 07-07-2009, 02:35 PM   #3
dots
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Original Poster
Rep: Reputation: 0
It is homework, I'm just stuck! I'm not understanding how to save the user id in a variable. I tried several different ways. Our book has an example and I tried it with what I'm doing and get an error. This is how I tried:

ps -eaf | sort -k1 | awk '\
$1~ { user = $1; print "USER = " user }'
echo $USER #just to make sure it was storing the user id

We have lab time in class tonight so it isn't a big deal. I just thought I could get it done and not have to go tonight.
 
Old 07-07-2009, 02:38 PM   #4
dots
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Sorry, the following line was from trying something different(it's no longer in my script):
echo $USER #just to make sure it was storing the user id
 
Old 07-07-2009, 03:00 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
And what's the ~ supposed to be operating on?
 
  


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
using variables in awk vgr12386 Programming 14 06-24-2009 04:19 AM
Does awk depend on the environment variables ? first_linux Linux - Newbie 6 05-08-2009 03:17 AM
Assigning variables by awk(?) bioinformatics_guy Linux - Newbie 2 02-19-2009 12:01 PM
Can you use grep / awk on variables instead of files? zest n zeal Linux - Newbie 2 02-11-2008 12:37 PM
awk: /matching/ variables passed with -v aunquarra Linux - General 2 02-17-2005 06:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:54 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