Operating System - Linux
1752570 Members
5261 Online
108788 Solutions
New Discussion юеВ

removing the last character from fuser

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

removing the last character from fuser

Hello,

I am working on AIX and run fuser, I get several procs with 1234c 234344c 34445c

how can I script to loose the last character, I'd like to know how to drop any final character for otheer scripting purposes.

Thanks for your help!

Chris.
hello
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: removing the last character from fuser

Hi Chris:

You could do something like this:

# echo "abc123"|awk '{print substr $0,0,length($0)-1)}'

abc12

This trims the last character of any string.

In Perl:

# echo "abc123"|perl -ple 'chop'
abc12

Regards!

...JRF...
Jeff_Traigle
Honored Contributor

Re: removing the last character from fuser

The following will work for your specific case. You can't just drop the last character because it's possible to have multiple letters after the PID in fuser output. The first regexp eliminates the alpha characters (leaving the PIDs and a mangled path) and the second regexp eliminates the mangled path (leaving only the PIDs as the final output).

fuser -c /data 2>&1 | sed -e 's/[a-z]//g' -e 's/[^0-9[:blank:]]//g'
--
Jeff Traigle
Ralph Grothe
Honored Contributor

Re: removing the last character from fuser

Funny, the others haven't observed this.
Simply redirect stderr to /dev/null.
This strips everything apart from the PIDs.

e.g.

# fuser -c /oracle 2>/dev/null
15385 15488 15392 15487 15405 15388 15407 12205 15418 15406 15410 15420
15372 2676 15404 15386


This is handy if you want to use the PIDs in situ.


# ps -p "$(fuser -c /oracle 2>/dev/null)"
ps: wrong PID number
PID TTY TIME COMMAND
15385 ? 430:55 disp+work
15488 ? 533:41 jlaunch
15392 ? 11:15 disp+work
15487 ? 84:04 jlaunch
15405 ? 0:05 disp+work
15388 ? 595:17 disp+work
15407 ? 0:03 disp+work
12205 ? 3:39 disp+work
15418 ? 6:15 disp+work
15406 ? 0:00 disp+work
15410 ? 0:00 disp+work
15420 ? 0:00 disp+work
15372 ? 7:00 disp+work
2676 ? 0:00 disp+work
15404 ? 1:35 disp+work
15386 ? 49:07 disp+work
Madness, thy name is system administration
Jeff_Traigle
Honored Contributor

Re: removing the last character from fuser

I almost did... I just wasn't awake enough yet to realize it. :)
--
Jeff Traigle
James R. Ferguson
Acclaimed Contributor

Re: removing the last character from fuser

Hi (again) Chris:

Thanks, Ralph, I too remembered that when I actually executed 'fuser'.

By the way, Chris, Perl allows a negative value for the length parameter to 'substr'. Using a negative value for length trims that many characters from the end. For example:

# echo abc123|perl -nle 'print substr($_,0,-1)'

abc12

Thus, there's yet another way to do this.

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: removing the last character from fuser

ok thanks again chaps,

as always plenty of ways of killing a bird with many stones!

:-)

Chris.
hello