How to script / command this in UNIX?

Hello Guys,

From a given file I have to find out what are all values for "Application States :" and how many of them have same state.

eg :

If there are possible states in file :

Application States : Normal
Application States : Terminated
Application States : Pending

and if there are 10 Normal, 2 Terminated and 15 Pending applications then I want to have some script or unix grep command to show something like this :

Application States : Normal         : total = 10
Application States : Terminated   : total = 02
Application States : Pending        : total = 15

What would be the exact unix command or script to achieve above?

What have you tried to solve this problem on your own?

Are we to assume by your login name that you are using an AIX system?

I use below approach but its time consuming and not efficient :frowning:
So i want to know how to do it in one command :

grep "Application States :" filename | sort | uniq

then depending on states I grep as below for each state

grep "Application States : Normal" filename | wc -l

Try and see if that works for you. If it does not work, please, post why and how it fails.

awk -F' : ' '
$1 == "Application States" {app[$2]++}
END {for(a in app){printf "Application States : %s : total = %02d\n", a, app[a]}
}' aixusrsys.file

Thanks, but it didn't returned anything and I confirmed the input file has multiple "Application States". Please let me know is any other way.

You didn't provide any actual sample input, you didn't answer my question about what operating system you're using, and the application "states" you've shown us look more like possible values for "application status" than "application state". So, if Aia's suggestion doesn't work, you might also try:

awk '
/^Application Sta/ {
	c[$0]++
	if(length($0) > m)
		m = length($0)
}
END {	for(i in c)
		printf("%-*s   : total = %02d\n", m, i, c)
}' file

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

Is it possible to post a few copy and paste lines from the original file?
Either the white spacing or the "Application States" string is not accurate in your example.

removing confidential info

So try:

awk '
/Application State/ {
	c[$0]++
	if(length($0) > m)
		m = length($0)
}
END {	for(i in c)
		printf("%-*s   : total = %02d\n", m, i, c)
}' 1.txt

which with your given sample produces:

  Application State :      Normal       : total = 03
  Application State :      Pending      : total = 01
  Application State :      Terminated   : total = 01
1 Like

Thanks works gr8 !

With the latest example you posted, this is what I get:

awk '/Application State/ {app[$NF]++} END{for(a in app) printf "Application State : %-10s : total = %02d\n", a, app[a]}' aixusrsys.file
Application State : Normal     : total = 03
Application State : Terminated : total = 01
Application State : Pending    : total = 01

I'm very glad that we finally got something to work for you...

Note that the data you posted in message #8 (and deleted 37 minutes later) did not look anything like the data you included in posts #1 and #3. Leading spaces make a difference. Having Application State : instead of Application States : makes a difference. Having six spaces between the colon and the state instead of one space makes a difference.

You don't have to show us confidential data to get our help, but you do need to show us representative data. The commands you said you used in post #3 would have produced absolutely no output (just like Aia's awk script and my awk script) when given your real data instead of the data you showed us in post #1.