Sed to replace second number in a random string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed to replace second number in a random string
# 1  
Old 12-28-2009
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.
# 2  
Old 12-28-2009
what have you tried till now??
# 3  
Old 12-28-2009
Quote:
Originally Posted by vidyadhar85
what have you tried till now??
You asked for it!

Code:
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[000-999]/STLM[060-969]/g'
STLM[060-969]11
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM***/STLM*6*/g'
sed: 1: "s/STLM***/STLM*6*/g": RE error: repetition-operator operand invalid
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM*/STLM?6?/g'
STLM?6?011
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM*[0-9]/STLM*6/g'
STLM*611
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[0-9]/STLM*6/g'
STLM*611
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[0-9]/STLM6/g'
STLM611
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[0-9]*/STLM6/g'
STLM6
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[0-9]*/STLM[0-9]6/g'
STLM[0-9]6
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[0-9]*/STLM[0-9]6/g'
STLM[0-9]6
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[0-9]/STLM6/g'
STLM611
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[00-99]/STLM6/g'
STLM611
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[00-99]/STLM*6/g'
STLM*611
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[00-99]*/STLM6/g'
STLM6
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[00-99]/STLM6/g'
STLM611
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM^[00-99]/STLM6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM?[00-99]/STLM6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[00-99]/STLM6/g'
STLM611
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[0-9]*/STLM1
> 
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[0-9]*/STLM1/g'
STLM1
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[000-999]*/STLM1/g'
STLM1
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[000-999]*/STLM*~*/g'
STLM*~*
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[0-9]*[0-9]/STLM6/g'
STLM6
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[0-9]*[0-9]/STLM[0-9]6[0-9]/g'
STLM[0-9]6[0-9]
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[0-9]*[0-9]/STLM\[0-9]6\[0-9]/g'
STLM[0-9]6[0-9]
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM[0-9]*[0-9]/STLM"[0-9]6[0-9]"/g'
STLM"[0-9]6[0-9]"
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM"[0-9]*[0-9]"/STLM"[0-9]6[0-9]"/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/STLM"[0-9]*[0-9]"/STLM6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/[0-9]*[0-9]"/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/[0-9]/6/g'
STLM666
STLM011:Desktop localadmin$ hostname -s |sed 's/[0-9]*[0-9]/6/g'
STLM6
STLM011:Desktop localadmin$ hostname -s |sed 's/[00-99]*/6/g'
6S6T6L6M6
STLM011:Desktop localadmin$ hostname -s |sed 's/[00-99]/6/g'
STLM666
STLM011:Desktop localadmin$ hostname -s |sed 's/*[00-99]/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/[00-99]*/6/g'
6S6T6L6M6
STLM011:Desktop localadmin$ hostname -s |sed 's/[000-999]/6/g'
STLM666
STLM011:Desktop localadmin$ hostname -s |sed 's/[000-999]/6/g'
STLM666
STLM011:Desktop localadmin$ hostname -s |sed 's/[0-9]*[0-9]/6/g'
STLM6
STLM011:Desktop localadmin$ hostname -s |sed 's/[0-9][0-9]/6/g'
STLM61
STLM011:Desktop localadmin$ hostname -s |sed 's/[0-9][0-9][0-9]/6/g'
STLM6
STLM011:Desktop localadmin$ hostname -s |sed 's/[0-9][0-9]/6/g'
STLM61
STLM011:Desktop localadmin$ hostname -s |sed 's/[0-9]/6/g'
STLM666
STLM011:Desktop localadmin$ hostname -s |sed 's/^^[0-9]/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/^[0-9]/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/^[0-9][0-9]/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/^[0-9][0-9]*/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/^[0-9]$[0-9]*/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/[^0-9]$[0-9]*/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/^[^0-9]$[0-9]*/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/[AAAA-ZZZZ 0-9][0-9]*/6/g'
6666
STLM011:Desktop localadmin$ hostname -s |sed 's/^[AAAA-ZZZZ 0-9][0-9]*/6/g'
6TLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/[^AAAA-ZZZZ 0-9][0-9]*/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/[^AAAA-ZZZZ 0-9][0-9]/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/[^AAAA-ZZZZ 0-9]/6/g'
STLM011
STLM011:Desktop localadmin$ echo 123 |sed 's/[000-999]/9/g'
999
STLM011:Desktop localadmin$ echo 123 |sed 's/[0-9]/9/g'
999
STLM011:Desktop localadmin$ echo 123 |sed 's/[0-9]/*\9/g'
sed: 1: "s/[0-9]/*\9/g": \9 not defined in the RE
STLM011:Desktop localadmin$ echo 123 |sed 's/[0-9]/\*9/g'
*9*9*9
STLM011:Desktop localadmin$ echo 123 |sed 's/[0-9]/9\/g'
sed: 1: "s/[0-9]/9\/g": unterminated substitute in regular expression
STLM011:Desktop localadmin$ echo 123 |sed 's/[0-9]/9*\/g'
sed: 1: "s/[0-9]/9*\/g": unterminated substitute in regular expression
STLM011:Desktop localadmin$ echo 123 |sed 's/[0-9]/9*\*/g'
9**9**9**
STLM011:Desktop localadmin$ hostname -s |sed 's/[0-9]\*/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/[00-99]\*/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/[0-9]/6/g'
STLM666
STLM011:Desktop localadmin$ hostname -s |sed 's/\*[0-9]/6/g'
STLM011
STLM011:Desktop localadmin$ hostname -s |sed 's/[0-9]/\*6/g'
STLM*6*6*6

# 4  
Old 12-28-2009
Try this:
Code:
sed 's/\(.*STDM.\).\(.*\)/\16\2/'

# 5  
Old 12-28-2009
Franklin, it seems to mostly work, could you please explain it a little?
# 6  
Old 12-28-2009
Quote:
Originally Posted by glev2005
Franklin, it seems to mostly work, could you please explain it a little?
Explanation:
Code:
sed 's/\(.*STDM.\).\(.*\)/\16\2/'

With sed you can save substrings with \(.*\) and recall them back with \1, \2, \3 etc.

\(.*STDM.\).\(.*\)

The 1st portion \(.*STDM.\) is from the start of the line till the character after STDM (hence the dot)
The 2st portion .\(.*\) is from one character after the 1st portion (hence the dot before the substring) till the end of the line

\16\2

\1 -> print the 1st portion
6 -> print a "6"
\2 -> print the 2nd portion
# 7  
Old 12-28-2009
Or use:
Code:
sed 's/[0-9]/6/2'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

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. Shell Programming and Scripting

Replace column by random number addition

Here is my problem:- I have a file with pipe separated values. CR|20121021|079|ABC|N|DLS|00038|DLS|04750|1330597704|634234|634|0 CR|20121021|079|ABC|N|DLS|00038|DLS|05118|2071690102|354|351|3 CR|20121021|079|ABC|N|DLS|00038|DLS|05140|960051505|1088|1088|0... (4 Replies)
Discussion started by: Yoda
4 Replies

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 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

Replace x Number of String Occurrence with Sed

Ok, So I have a huge file that has over 12000 lines in it. in this file, there are 589 occurrences of the string "use five-minute-interval" spread in various areas in the file. How can i replace the the last 250 of the occurrences of "use five-minute-interval" with "use... (10 Replies)
Discussion started by: SkySmart
10 Replies

6. Shell Programming and Scripting

Using SED to replace a random number?

Hi all, I need to run a number of scripts which have a certain phrase on them so I have identified these by; grep -l 100 *script | sort -u Normally I could just run something along the lines of; for i in `grep -l 100 *script | sort -u`; do ./${i}; done However before I run each of... (0 Replies)
Discussion started by: JayC89
0 Replies

7. Shell Programming and Scripting

Replace a random string of numbers

Hi Can someone help me with this one? I have string.. (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. ... (5 Replies)
Discussion started by: ryandegreat25
5 Replies

8. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

9. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

10. 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
Login or Register to Ask a Question