partial word replacement using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting partial word replacement using sed
# 1  
Old 07-16-2008
partial word replacement using sed

Example contents of a file:

001FILE
002FILE
003FILE
099FILE
111FILE

Is it possible to search for [000-111]FILE and still keep the number part of the result in tact? The number portion is a changing variable but the FILE part stays the same. I want to search for all [000-111]FILE and space them out to "001 FILE" "002 FILE" etc.

If I use sed 's/[000-111]FILE/&/g' It will basically replace with the same variable as found in search.

I know how to search for the variable but don't know how to keep a portion of the result. The cut function would be nice but I can't seem to be able to call it in through sed.

Searching for FILE and adding a space is not an option as some of the other contents have the same names and cannot be effected.

Any ideas?
# 2  
Old 07-16-2008
Code:
sed -e 's/^[0-1][0-9][0-9]/& /'  file

# 3  
Old 07-16-2008
Quote:
Originally Posted by fpmurphy
Code:
sed -e 's/^[0-1][0-9][0-9]/& /'  file

Thank you but this file has other numbered variables too so I cannot search for numbers alone or characters alone. I must be able to search for 001FILE as a whole and then split the output into 001 FILE.

On another note, it doesn't have to be sed. I'm working on Sun Solaris 10 so which ever utility available by default will work too.

Any other ideas?
# 4  
Old 07-17-2008
Not sure I fully understand the requirements, but try this variation:

Code:
sed -e '/^[0-1][0-9][0-9]FILE/s/^[0-1][0-9][0-9]/& /'  file

If that isn't what you want, give us some more sample input data (including the lines you don't want to change) and the output you expect from that data.
# 5  
Old 07-17-2008
Quote:
Originally Posted by Annihilannic
Not sure I fully understand the requirements, but try this variation:

Code:
sed -e '/^[0-1][0-9][0-9]FILE/s/^[0-1][0-9][0-9]/& /'  file

If that isn't what you want, give us some more sample input data (including the lines you don't want to change) and the output you expect from that data.

Hi. I was trying to keep the question simple but I guess I can't anymore so here is the actual data.

Code:
TRUE 6.8785794488e-05 0.000 0.000 FALSE 6.8785794488e-05 0.000 0.000TRUE 1 FALSE 1 TRUE
TRUE 1.8093516870e-02 0.999 0.030 TRUE 1.8093255236e-02 0.999 0.057TRUE 1 FALSE 1 TRUE

The above is just 2 line of a very large file. The problem is, I need a space between the 0.000 and TRUE so I can import it correctly into excel.

The numbers are not constant and is a changing variable. I need to be able to target just the lines containing [0-1].[0-9][0-9][0-9]TRUE and put a space/tab to separate the number and the TRUE.

Thanks.
# 6  
Old 07-17-2008
sed -e '/[0-1][0-9][0-9]TRUE/s/[0-1][0-9][0-9]/& /g' file
# 7  
Old 07-17-2008
MySQL

Quote:
Originally Posted by sudhamacs
sed -e '/[0-1][0-9][0-9]TRUE/s/[0-1][0-9][0-9]/& /g' file
That worked!
Thank you all very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to remove partial text in one line only

I have test.xml XML file like <Report account="123456" start_time="2014-09-08T00:00:00+00:00" end_time="2014-09-10T23:59:59+00:00" user="Dollar Tree" limit="1000000" more_sessions="some text "> <Session ......rest of xml............... I need output like <Report> <Session ......rest of... (3 Replies)
Discussion started by: kumars1331@gmai
3 Replies

2. Shell Programming and Scripting

Replacing a particular word with another word in all the xml's under a particular directory with sed

Hi Folks, Could you please advise what will be the SED command to replace a word in all xml's under a particular directory for example let say I rite now at the following below location $ cd /ter/rap/config now under config directory there will be lots of xml file , now my objective is to... (1 Reply)
Discussion started by: punpun66
1 Replies

3. Shell Programming and Scripting

Word replacement in Perl

I have the following string : Cat dog fox catepillar bear foxy I need to replace "cat" and "fox" words from this sentence to word "animal" I do the following: $Str1="cat"; $Str2="fox"; $NewStr="animal"; open(F1, "<$inputFile") or die "Error: $!"; open(F2, ">$outputFile") or... (1 Reply)
Discussion started by: AIX_30
1 Replies

4. Shell Programming and Scripting

SED replacement

Hi, i have a file with lines, file.txt ------- test is fun testing is better I need to replace 'test' to 'develop' and i used, a=test b=develop sed "s,$a,$b,g" -------- but i see the word 'testing' is also replaced. Need some solution. Is there any way i could replace only 'test' ? (4 Replies)
Discussion started by: giri_luck
4 Replies

5. Shell Programming and Scripting

awk/sed to extract column bases on partial match

Hi I have a log file which has outputs like the one below conn=24,196 op=1 RESULT err=0 tag=0 nentries=9 etime=3,712 dbtime=0 mem=486,183,328/2,147,483,648 Now most of the time I am only interested in the time ( the first column) and a column that begins with etime i.e... (8 Replies)
Discussion started by: pkabali
8 Replies

6. Shell Programming and Scripting

AWK - Print partial line/partial field

Hello, this is probably a simple request but I've been toying with it for a while. I have a large list of devices and commands that were run with a script, now I have lines such as: a-router-hostname-C#show ver I want to print everything up to (and excluding) the # and everything after it... (3 Replies)
Discussion started by: ippy98
3 Replies

7. Shell Programming and Scripting

How to use sed to replace space partial

source "PUT 810 712 0001 ILC AK4 00 0 00 00" It needs to be changed to "PUT,810,712,0001,ILC,AK4 00 0 00 00" Thanks in advance. (6 Replies)
Discussion started by: janex
6 Replies

8. Shell Programming and Scripting

Replacement with sed

I am trying to replace the line which has string "tablespace" not case senstive.... with below simple script: mysrcipt.sh sed "s/.*/TABLESPACE USERS/g" create_table > tmp mv tmp create_table Is there any better way to do it? If Search string tooooooo long it will be tough to code in... (4 Replies)
Discussion started by: ganeshd
4 Replies

9. Shell Programming and Scripting

Help with sed replacement

This seems like it should be an easy problem, but I'm a noob and I can't figure it out. I'm trying to use sed, but would be happy to use anything that does the job. I am trying to trim off a fixed number of unknown characters from 2 different : delimited fields while keeping the intervening... (4 Replies)
Discussion started by: helix_w
4 Replies

10. UNIX for Dummies Questions & Answers

Replacement using sed

Hi I have the following file that i need to run a sed command on 1<tab>running 2<tab>running 3<tab>running 4<tab>running I want to be able to replace a line i.e the second one with '2<tab>failed'. As the first number is unique that can be used to search for the relevant line (using ^2 i... (5 Replies)
Discussion started by: handak9
5 Replies
Login or Register to Ask a Question