Visit Our UNIX and Linux User Community


Replace a random string of numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a random string of numbers
# 1  
Old 12-08-2009
Replace a random string of numbers

Hi Can someone help me with this one?

I have string..
Code:
(PROC_PROC_ID == 12183) <--PID is dynamic

and i want to replace the PID number with whatever PID from
/opt/hpws/apache32_2/logs/httpd.pid file.

i'm having problem since the PID on the string is dynamic. It may be 2-5 digits or more.

The PID is only string the numbers to the file i want to replace
# 2  
Old 12-08-2009
from this you will get the PID.. whatever may be its length.. then do the replacement
Code:
echo "(PROC_PROC_ID == 12183)"|awk -F"[==)]" '/PROC_PROC_ID/{print $3}'

# 3  
Old 12-08-2009
Code:
PROC_PROC_ID = $(echo /opt/hpws/apache32_2/logs/httpd.pid)

# 4  
Old 12-09-2009
thanks vidyar! your solution is working.. btw is there anyway i can replace it directly using sed?

here's the complete parm file
Code:
PROCESS LOOP
{
  //Get PID
  if (PROC_PROC_ID == 12183) then
  {
    //Start Processing
    PRINT "PROCESS: ",PROC_PROC_NAME,
        " PID: ",PROC_PROC_ID,
        " CPU: ",PROC_CPU_TOTAL_UTIL," %",
        " MEM: ",PROC_MEM_RES,
        " THREAD: ",PROC_THREAD_COUNT
  }
}

I'm trying to replace it with
Code:
sed "s|PROC_PROC_ID \=\= [0-9]+|PROC_PROC_ID \=\= $PID|" parm.file

but it doesn't seem to work

This one will work but unfortunately it won't be always a 5-digit number
Code:
sed "s|PROC_PROC_ID == [0-9]\{5\}|PROC_PROC_ID == $PID|" parm.file


Last edited by ryandegreat25; 12-09-2009 at 12:33 AM..
# 5  
Old 12-09-2009
Code:
echo "(PROC_PROC_ID == 12183)" | sed 's/\(PROC_PROC_ID ==\).*/\1 whatever you want here)/g'

Code:
mo@mo-laptop:~/scripts$ echo "(PROC_PROC_ID == 12183)" | sed 's/\(PROC_PROC_ID ==\).*/\1 whatever you want here)/g'
(PROC_PROC_ID == whatever you want here)
mo@mo-laptop:~/scripts$

# 6  
Old 12-09-2009
thanks!

Previous Thread | Next Thread
Test Your Knowledge in Computers #251
Difficulty: Easy
MILNET was designed for highly classified military communications.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace all string matches in file with unique random number

Hello Take this file... Test01 Ref test Version 01 Test02 Ref test Version 02 Test66 Ref test Version 66 Test99 Ref test Version 99 I want to substitute every occurrence of Test{2} with a unique random number, so for example, if I was using sed, substitution would be something... (1 Reply)
Discussion started by: funkman
1 Replies

2. Homework & Coursework Questions

Random numbers

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! Write a shell script that will take the sum of two random number? Ex: Random n1 +Random n2 = result i tries to write it but i had some dufficulties ... (3 Replies)
Discussion started by: renegade755
3 Replies

3. Shell Programming and Scripting

Replace string ids with unique numbers

Hello, I have a file with a 1000 ids in the form of strings. I want to replace each id with a unique numbers in the whole file. each id is repeating in all the columns. I know I can use sed command but there are many ids in file which are need to be converted example of input file B752... (4 Replies)
Discussion started by: ryan9011
4 Replies

4. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

5. Shell Programming and Scripting

Script to replace numbers by string

Hi! I need the following script: - All numbers in a filename (0-9) have to be replace by a String ("Zero"-"Nine") - The script has to go through all the files in the current directory and has to replace the numbers as described above... I have no idea how to do this... Thanks! Michael (5 Replies)
Discussion started by: Michi21609
5 Replies

6. UNIX for Dummies Questions & Answers

Sed to replace second number in a random string

I need a sed line that will take STDM111 and change it to STDM161 the STDM will always be constant but the 3 numbers after will be random, I just need it to always replace the middle number with 6 regardless of what the numbers are. (8 Replies)
Discussion started by: glev2005
8 Replies

7. Shell Programming and Scripting

Generating random numbers

Hi, I am having trouble with generating random numbers. can this be done with awk? So I have a file that looks like this: 23 30 24 40 26 34 So column1 is start and column2 is end. I want to generate 3 random #'s between start and stop: So the output will look like this: ... (9 Replies)
Discussion started by: phil_heath
9 Replies

8. Shell Programming and Scripting

Replace a certain string with string containing random rubbish

Hello, in my shell script i have some multi-line text in a variable $TEMP - f.e. blablahblah blah blah bla TARGET hgloglo And i need to replace TARGET with text from another variable ($REPLACE), which is containing some text with nasty characters (\n, ", :, etc.) And stuff the altered text... (2 Replies)
Discussion started by: MilanCZ
2 Replies

9. Shell Programming and Scripting

Random numbers from 0 to 1000

Hello All, I want to make a simple script which generate random number from 0 to 1000. and simply display it. Plz HELP!!!!!! Regards, Waqas Ahmed (2 Replies)
Discussion started by: wakhan
2 Replies

10. UNIX for Dummies Questions & Answers

Random numbers without repetition

Is anyone know some scripts to generate random number without repetition using bash; for example generate 10 different random numbers. Thanks (8 Replies)
Discussion started by: asal_email
8 Replies

Featured Tech Videos