Parsing a file name with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing a file name with awk
# 1  
Old 07-23-2018
Parsing a file name with awk

I have a command to print out the top 5 most cpu intensive commands:

Code:
ps aux --sort=-%cpu --no-headers

I would like to make a printout containing only the parsed file name.

So, I want to turn this:
Code:
$ ps aux --sort=-%cpu --no-headers
user     24883  4.3  1.7 2010436 131440 ?      Sl   17:07   0:06 /usr/lib/x86_64-linux-gnu/qt5/libexec/QtWebEnginePr
user     25475  1.9  2.4 2048140 182668 ?      Sl   16:25   0:52 /usr/lib/x86_64-linux-gnu/qt5/libexec/QtWebEnginePr
user     20794  1.7  3.6 3393352 277388 ?      Sl   16:22   0:49 falkon
user       541  0.3  0.5 433640 38748 tty1     Sl   10:45   1:18 /usr/lib/xorg/Xorg -nolisten tcp :0 vt1 -keeptty -a
user     18867  0.2  0.0   4396  1020 ?        S    15:40   0:11 /bin/sh /home/user/scripts/./status.sh
root         8  0.1  0.0      0     0 ?        I    10:45   0:23 [rcu_sched]
root         1  0.0  0.1 216728  8760 ?        Ss   10:45   0:07 /sbin/init
root         2  0.0  0.0      0     0 ?        S    10:45   0:00 [kthreadd]
root         4  0.0  0.0      0     0 ?        I<   10:45   0:00 [kworker/0:0H]
root         6  0.0  0.0      0     0 ?        I<   10:45   0:00 [mm_percpu_wq]
root         7  0.0  0.0      0     0 ?        S    10:45   0:00 [ksoftirqd/0]
root         9  0.0  0.0      0     0 ?        I    10:45   0:00 [rcu_bh]
root        10  0.0  0.0      0     0 ?        S    10:45   0:00 [migration/0]
root        11  0.0  0.0      0     0 ?        S    10:45   0:00 [watchdog/0]
root        12  0.0  0.0      0     0 ?        S    10:45   0:00 [cpuhp/0]
...

Into this:
Code:
QtWebEnginePr
QtWebEnginePr
falkon
Xorg
sh

Note that '/usr/lib/xorg/Xorg -nolisten tcp :0 vt1 -keeptty -a' has been trimmed to say only 'Xorg'; I would like the file name itself with no command parameters at the end.


So far I have this:
Code:
$ ps aux --sort=-%cpu --no-headers | awk 'NR==1,NR==5{print $11}'
/usr/lib/x86_64-linux-gnu/qt5/libexec/QtWebEngineProcess
falkon
/usr/lib/x86_64-linux-gnu/qt5/libexec/QtWebEngineProcess
/usr/lib/xorg/Xorg
/bin/sh

I understand that I could just slap a cut command on the end of it and it's all fine:
ex.,
Code:
ps aux --sort=-%cpu --no-headers | awk 'NR==1,NR==5{print $11}' | cut -d "/" -f2)

but I don't want to use anymore commands to complete this task; this command will be repeated every 1 second or so and printed out in my status bar, so I want it to be efficient. And I know that awk is powerful enough to do this; but I just don't seem to be able to do this all in one. I could parse the beginning of the filename but the end out be out of control. And vise-versa. I have a feeling it's something to do with '-F/' but how to incorporate the two?

Any ideas?

I'm using sh shell and running Debian 9.
# 2  
Old 07-23-2018
wouldn't this suffice (or a variation of):
Code:
ps -axeo comm --no-header --sort=-%cpu

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 07-23-2018
Quote:
Originally Posted by vgersh99
wouldn't this suffice (or a variation of):
Code:
ps -axeo comm --no-header --sort=-%cpu

Indeed it would! This is perfect! Smilie I'm guessing that this is far more efficient than using awk.

I just added a 'head' command to chop off the rest of the lines after 5:
Code:
ps axeo comm --sort=-%cpu --no-header | head -5

SOLVED!!! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk parsing file

Looking to parse a file to remove the duplicates and get only few fields and uniq rows. input.tx Loc (TC) ID : ssfad_fs Serial : PIC002340098 ... (8 Replies)
Discussion started by: shunya
8 Replies

2. Shell Programming and Scripting

AWK? parsing /etc/passwd file.

Hello guys, please help me to make simple script for parsing passwd file. I have many passwd files from our servers, named server1.pass, server2.pass etc.. so for server in `ls *.pass` i need to print these rows: server1;root:!:0:0::/root:/usr/bin/ksh... (7 Replies)
Discussion started by: rubico
7 Replies

3. Shell Programming and Scripting

Help in file parsing with awk

Hi, I have a data set as shown below: 09e757fd,22727,2012-03-01,text1,text2,to 3fd0cae7,310,2012-03-01,text3,text4,to 3fd0cae7,310,2012-03-01,text3,text5,to 3fd0cae7,311,2012-03-01,text7,text10,cc 3fd0cae7,311,2012-03-01,text7,text11,to 3fd0cae7,312,2012-03-01,text8,text15,to... (3 Replies)
Discussion started by: shekhar2010us
3 Replies

4. Shell Programming and Scripting

Parsing a file with AWK

Input Group: Erecords: Copy: ADC R2: Replication volumes: Replication set: RSet 1 Replication size: 50.00GB SAN Info: 50.00GB DGC VRAID CX4-960 LUN 1040 (1040) 60,06,01,60,32,bb,21,00,84,a0,08,b1,da,ec,df,11... (2 Replies)
Discussion started by: greycells
2 Replies

5. UNIX for Advanced & Expert Users

Parsing through a file with awk/sed

I don't necessary have a problem, as I have a solution. It is just that there may be a better solution. GOAL: Part one: Parse data from a file using the "\" as a delimiter and extracting only the last delimiter. Part two: Parse same file and extract everything but the last delimited item. ... (8 Replies)
Discussion started by: OrangeYaGlad
8 Replies

6. Shell Programming and Scripting

awk/sed for parsing file

Hi All, I have a log file like this E Mon Oct 06 00:17:08 2008 xxx2 cm:10614 fm_pi2_svc_iptv_purchase.c:149 1:pin_deferred_act:10601:11:169:1223245028:16 pi2_op_svc_iptv_purchase error <location=PIN_ERRLOC_FM:5 class=PIN_ERRCLASS_SYSTEM_DETERMINATE:1... (10 Replies)
Discussion started by: subin_bala
10 Replies

7. Shell Programming and Scripting

Parsing a file (sed/awk?)

Hello people, newbie question. I'm trying to parse these type of file 1 "CAR " " C1 " " " 6 0 C1 2 "CAR " " O1A" " " 8 0 O1A 3 "CAR " " O1B" " " 8 -1 O1B 4 "CAR " " C2 " " " 6 0 C2 5 "CAR " " C3 " " " 6 ... (10 Replies)
Discussion started by: aristegui
10 Replies

8. Shell Programming and Scripting

awk and file parsing

Hi, I have a input file like this TH2TH2867Y NOW33332106Yo You Baby TH2TH3867Y NOW33332106No Way Out TH2TH9867Y NOW33332106Can't find it TJ2TJ2872N WOW33332017sure thing alas TJ2TJ3872N WOW33332017the sky rocks TJ2TJ4872N WOW33332017nothing else matters ... (4 Replies)
Discussion started by: devtakh
4 Replies

9. Shell Programming and Scripting

AWK and Magic with file parsing

Hi, I have a input file like this TH2TH2867Y NOW33332106Yo You Baby TH2TH3867Y NOW33332106No Way Out TH2TH9867Y NOW33332106Can't find it TJ2TJ2872N WOW33332017sure thing alas TJ2TJ3872N WOW33332017the sky rocks TJ2TJ4872N WOW33332017nothing else matters TJ2TJ5872N WOW33332017you know... (1 Reply)
Discussion started by: devtakh
1 Replies

10. Shell Programming and Scripting

parsing file through awk

hi, how can i achieve this in awk CON Controllers Department R abcuser usernamedesc1 R defuser usernamedesc2 R ... (13 Replies)
Discussion started by: bbeugie
13 Replies
Login or Register to Ask a Question