Extract character from string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract character from string
# 8  
Old 03-13-2009
Try the below code:

Caveat: Ensure to test this before implementing as I have NOT tested it. Before running the kill command, maybe replace with a echo command just to be on the safer side.

Code:
 
export UNIX95=<WHITESPACE>
ps -eo pid,etime,comm | grep "oracleTRLV (LOCAL=NO)" | while read line
do
  time_taken=$(echo ${line} | awk '{print $2}')
  if [[ $(echo ${time_taken} | grep "-" | wc -l) -gt 0 ]]; then
    time_taken_days=$(echo ${time_taken} | cut -d "-" -f1)
  else
    time_taken_days=0
  fi
  time_taken_hrs=$(echo ${time_taken} | cut -d "-" -f2 | cut -d ":" -f1)
  time_taken_min=$(echo ${time_taken} | cut -d "-" -f2 | cut -d ":" -f2)
  total_time_taken=$(( (${time_taken_days} * 24 * 60) + (${time_taken_hrs} * 60) + ${time_taken_min} ))
  if [[ ${total_time_taken} -gt 15 ]]; then
    pid_to_be_killed=$(echo ${line} | awk '{print $1}')
    kill -9 ${pid_to_be_killed}
  fi
done

HTH,Smilie

Regards,

Praveen
# 9  
Old 03-13-2009
shell

Praven,
there is an error message:
ps: illegal option -- o
usage: ps [-edaflPx] [-u ulist] [-g glist] [-p plist] [-t tlist] [-R prmgroup]

Smilie
# 10  
Old 03-13-2009
Quote:
Originally Posted by NicoMan
I'm on HP-Unix 10 and the above doesn't work.

Which "above" are you talking about? More than one script has been posted.

What does "doesn't work" mean exactly? What does happen?

Please post exactly what you tried and exactly what output there was and copy any error messages exactly.
Quote:
Stupid questions:

These questions have nothing to do with HP-UX itself, but with the shell you are using. Make sure that you are using a POSIX shell. That may or may not be /bin/sh.

They are all very basic shell scripting questions, so basic, in fact, that I wonder if they are really what you want to ask.
Quote:
1) How do I assign a value to a variable in hp-unix?
2) How do I concatenate 2 variables in hp-unix?
3) How do I write an "if" statement in hp-unix?
4) How to I extract a piece of a string inside an "if" statement?
# 11  
Old 03-13-2009
Quote:
Originally Posted by NicoMan
Chris, I don't see in the script how do you build the number of minutes.

I don't "build", I extract. I used awk's substr() function, but it have been "cut -c13,14". Better still is the method below.
Quote:
Let me explain what I want to do: I have an application that creates processes in HP-Unix that for some reason don't "die".
The result of:

Please use [code] tags rather than changing font or size.
Quote:

Code:
ps -eaf|grep "oracleTRLV (LOCAL=NO)"

are rows like this:
Code:
oracle 1769 1 0 15:24:13 ? 0:00 oracleTRLV (LOCAL=NO)


Whoa! Which characters do you want? I don't think it's 13 and 14. And can you be sure that what you want is always in the same columns?

It would be better to read fields rather than characters. Which field (or part of a field) do you want?

Code:
a: oracle
b: 1769
c: 1
d: 0
e: 15:24:13
f: ?
g: 0:00
h: oracleTRLV (LOCAL=NO)

Presumably you want part of either e or g. I'd guess it's g.

You can read each field into a separate variable:

Code:
ps -eaf |
 while read user pid c d e f g command
 do
    ## get the minutes from $g
    temp=${g%:*}
    mins=${temp##*:}
    if [ ${mins#0} -gt 12 ]
    then
      kill $pid  ## you may need to add a signal
    fi
 done

Quote:
I want to make a shell (I will run it every 15 minutes) that will take each row and "kill" all the processes that are older then 15 minutes.

By the way, I bought your book "Shell Scripting Recipes" but is way to advanced for me.
temp=${g%:*}
mins=${g##*:}

It's not a tutorial, but there are many simple scripts you should be able to understand.

Last edited by cfajohnson; 03-13-2009 at 03:48 PM.. Reason: Fixed incorrect variable name: g => temp
# 12  
Old 03-13-2009
shell

Thank you.
# 13  
Old 03-13-2009

Note the edit I just made to the script.
Code:
  mins=${temp##*:}  ## was: mins=${g##*:}

# 14  
Old 03-14-2009
Quote:
Originally Posted by NicoMan
Praven,
there is an error message:
ps: illegal option -- o
usage: ps [-edaflPx] [-u ulist] [-g glist] [-p plist] [-t tlist] [-R prmgroup]

Smilie
Nicoman,

ps -o does NOT work in HP-UX unless the env variable UNIX95 is set to null or whitespace. That is the reason why the first line of my code was:

Code:
export UNIX95=<enter whitespace here>

Moreover, the script (which initially was erroring out with ps --o is an invalid option) is working fine for me, when I added the export UNIX95=<whitespace> at the beginning of the script.

Anyway, Chris has given u a much simpler option!! As usual, Chris rocks!!Smilie


Regards, Smilie

Praveen

Last edited by sunpraveen; 03-14-2009 at 05:59 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

Extract all first numeric character from a string

Hello, I have a file of strings a below:- 4358RYFHD9845 28/COC/UYF984 9834URD 98HJDU I need to extract all the first numeric character of every sting as follows:- 4358 28 9834 thanks to suggest ASAP Regards, Jasi Use code tags, thanks. (7 Replies)
Discussion started by: jassi10781
7 Replies

3. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

4. Shell Programming and Scripting

How to extract the certain position's character in a string

Suppose there are two files: A, format is like: line1 12 line2 33 line3 6 ... B, format is like: >header taaccctaaccctaaccctaacccaaccccaccccaaccccaaccccaac ccaaccctaaccctaaccctaacccaaccctaaccctaaccctaacccaa ccctcaccctcaccctcaccctcaccctcaccctcaccctcaccctaacc... (1 Reply)
Discussion started by: bioinflix
1 Replies

5. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Korn: How to loop through a string character by character

If I have a string defined as: MyString=abcde echo $MyString How can I loop through it character by character? I haven't been able to find a way to index the string so that I loop through it. shew01 (10 Replies)
Discussion started by: shew01
10 Replies

7. Shell Programming and Scripting

Extract the last character of a string

How can I extract the last character of a string (withou knowing how many characters are in that string ! ) (8 Replies)
Discussion started by: annelisa
8 Replies

8. Shell Programming and Scripting

extract character + 1

Hi, I would like extract from a file a character or pattern after ( n + 1) a specific pattern (n) . ( i supposed with awk) how could i do ? Thanks in advance. (1 Reply)
Discussion started by: francis_tom
1 Replies

9. UNIX for Advanced & Expert Users

Extract a character

HI Guys, Have a Doubt...... I have a pattern "abcdef" and i need to extract the third character..ie(c) How to achieve it? (10 Replies)
Discussion started by: aajan
10 Replies

10. Shell Programming and Scripting

Need help to extract a string delimited by any special character

I have a string as follows IS*blahblah TED~blahblah etc. I want to list down only IS and TED Can someone help me? (24 Replies)
Discussion started by: kumariak
24 Replies
Login or Register to Ask a Question