Help with awk in a loop/shellscript


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk in a loop/shellscript
# 1  
Old 07-09-2009
Help with awk in a loop/shellscript

I am trying to write a shell script using awk which will format the output of the "ps -eaf" command by sorting it by user and then listing all of the processes for each user. Here is an example of the output I want:

Quote:
USER = 68
PID PROGRAM TTY
1583 hald (?)
1674 hald-addon-acpi: (?)

USER = 826
PID PROGRAM TTY
15317 SCREEN (?)
15318 /bin/bash (pts/20)
Here is the code I have:
Code:
#! /bin/sh

for I in `ps -eaf h | awk '{print $1}' | sort | uniq`
do
    echo "USER = $I"
    ps -eaf h | awk '/$I/ {print $2 $8 $6}'
done

The first command(`ps -eaf h | awk '{print $1}' | sort | uniq`) gets the list of user names. The second command using awk is where I'm having trouble. It is replacing "$I" with the appropriate username but it does not print anything. I've tried a few variations using awk, but have not had any luck. Any help is appreciated. Smilie
# 2  
Old 07-09-2009
for passing variable in awk, the method is...
Code:
awk -v var="$i" '{print var}' file
or
awk '{print var}' var="$i" file

# 3  
Old 07-09-2009
Quote:
Originally Posted by rakeshawasthi
for passing variable in awk, the method is...
Code:
awk -v var="$i" '{print var}' file
or
awk '{print var}' var="$i" file

This is what I tried to use:

Code:
ps -eaf h |awk -v var="$I" '/var/{print $2 $8 $6}'

And this is the output I got from that..
Quote:
USER = 68
16740:00?
17140:00?
17190:00?
17600:01?
Nothing like what I need. Smilie
# 4  
Old 07-09-2009
Please try this ...it works fine

#!/bin/bash

for i in `ps -eaf | awk '{ print $1 }' | sort | uniq`
do
echo "USER = $i"
echo
ps -eaf | awk -v var="$i" 'BEGIN { print "PID PROGRAM TTY" } /var/ { print $2,$8,$6 }'
echo
done
# 5  
Old 07-09-2009
see if this works...

Code:
 
ps -eaf  | grep $I | awk '{print $2 $8 $6}'

# 6  
Old 07-09-2009
Quote:
Originally Posted by oky
Please try this ...it works fine

#!/bin/bash

for i in `ps -eaf | awk '{ print $1 }' | sort | uniq`
do
echo "USER = $i"
echo
ps -eaf | awk -v var="$i" 'BEGIN { print "PID PROGRAM TTY" } /var/ { print $2,$8,$6 }'
echo
done
This works however, all users have the same processes as far as I can tell, it does not list the processes just for that user. Thanks for the input, i'm going to play around with this and see if I can get it working.
# 7  
Old 07-09-2009
This oneliner worked for me.Tested in SUSE Linux

Code:
 ps -ef --noheader --sort -uid | awk ' { if ( $1 == x ) {print $2,$8,$6} else { print "USER="$1 "\nPID PROGRAM TTY\n"  $2,$8,$6 } x=$1}'


Last edited by honglus; 07-09-2009 at 11:17 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk !seen[$]++ in else loop

Hi all, I was searching the net for a solution for my problem... unfortunately nothing so far. I want to sort on more than on column tab delimited file and keep the line if in the column I sort there is no value, but for those who have a value I want them only unique. I have tried the... (5 Replies)
Discussion started by: ksenia
5 Replies

2. UNIX for Beginners Questions & Answers

How to add field to diffrent file using shellscript? or awk

hi, would you help me? i have file total.csv "a","e23","f" "b,"34d","g" "c","45f","f" "d","45s","f" count.csv 3 i do this : paste -d',",' total.csv count.csv but the result like this: "a,"e23","f" 3 "b,"34d","g" (1 Reply)
Discussion started by: kivale
1 Replies

3. Shell Programming and Scripting

Using awk within a for loop

Hello, I currently have managed to get an awk function working inside a for loop that allows me to combine two files based on their headings but what I have not been able to do is print the output to files with variable names. awk ' NR==FNR {a=$0; next} /^>/ {$0 = $0" "a;} ... (2 Replies)
Discussion started by: Allie_gastrator
2 Replies

4. Shell Programming and Scripting

awk in shellscript

Dear Members, I have the following situation I do not understand: I have a large json encoded file which I need to grep and afterwards want to extract specific information. I use this command to to that: cat /tmp/file | awk -F '"fields":' '{print $2}' | awk -F '"description":' '{print $4}'... (6 Replies)
Discussion started by: crumble
6 Replies

5. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

6. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

7. Shell Programming and Scripting

For Loop in shellscript - Printing Output for every iteration

for VGLIST in `lsvg -o` do CLOSED_OUT=`echo $VGLIST | lsvg -l $VGLIST | awk '{print $6 " " $7}' | grep closed` if ]; then echo "Filesystems $CLOSED_OUT in VG that are in Closed status" else echo "\n Some message" fi Above Code is working fine, but echo "Filesystems $CLOSED_OUT... (8 Replies)
Discussion started by: chandu123
8 Replies

8. Shell Programming and Scripting

awk loop and using shell in awk

Hi, everyone! I have a file, when I print its $1 out it show several strings like this: AABBCC AEFJKLFG FALEF FAIWEHF What I want to do is that, after output of each record, search the string in all files in the same folder, print out the record and file name. This is what I want... (4 Replies)
Discussion started by: xshang
4 Replies

9. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

10. Shell Programming and Scripting

awk for-loop and NR

Hey, I know this is a stupid question, but it doesn't work. I have a file with 10 lines and I want to pipe the content to awk and then print line 1 til 2 into another file and then line 3-4 ... So my script looks like that, but doesn't work: cat grid_ill.pts | awk '{ for (NR=1;NR<3;NR++)... (8 Replies)
Discussion started by: ergy1983
8 Replies
Login or Register to Ask a Question