String replace that has spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String replace that has spaces
# 1  
Old 03-22-2012
String replace that has spaces

Code:
cat rf|nawk '/Use SSL=                0/{n+=1}{if (n==3){sub("Use SSL=                0","Use SSL=                0x1",$0)};print }' > rf2

Fails.

Code:
sed 's/Use SSL=              0/Use SSL=              0x1/g' rf > rf2

Fails.


In addition, the goal is to ONLY replace the 2nd occurence of the entire line, here's the line:

Code:
Use SSL=             0;          REG_DWORD

Want it changed to:
Code:
Use SSL=             0x1;          REG_DWORD

Notice it has all these spaces.

I've tried double, singe quotes, graves, no luck so far.

Any help is greatly appreciated.

Last edited by methyl; 03-22-2012 at 04:27 PM.. Reason: please use code tags. especially where space characters are important.
# 2  
Old 03-22-2012
Please post sample representative input from "rf" and sample matching output you would expect in "rf2". Please post in code tags so posters can see the space characters.
Though "nawk" says SunOS (or some older unix System V) to me, please post what Operating System and version you have and what Shell you prefer.

Ps "Fails" doesn't mention what actually happened !
# 3  
Old 03-22-2012
string replace that has spaces

The file 'rf' contains many lines of which several are identical, as follows:

Code:
Use SSL=                   0;            REG_DWORD

There may be 2-5 identical lines (to make it more complicated, the number of spaces between = and 0 and ; and REG_DWORD could be different on each line or they could be exactly the same.

The server os is solaris 8.

The several attempts made failed to replace the 0; with 0x1 while keeping the format intact;...by fail, nothing was replaced...as if the PATTERN was not recognized.

Thanks for helping.

Last edited by methyl; 03-23-2012 at 09:08 AM..
# 4  
Old 03-22-2012
Code:
printf "Use SSL= 0; REG_DWORD\nSomething\nUse SSL= 0; REG_DWORD\nUse SSL= 0; REG_DWORD\n" |
        awk '/Use SSL= *0 *; *REG_DWORD/ && ((++N)==2) { sub(/0/, "0x0"); } 1'

Use SSL= 0; REG_DWORD
Something
Use SSL= 0x0; REG_DWORD
Use SSL= 0; REG_DWORD

$


Last edited by Corona688; 03-22-2012 at 07:44 PM..
# 5  
Old 03-23-2012
string replace that has spaces

Thanks.

Can you explain what is happening with your recommendation?

It does not work as typed...what is 'something' supposed to be?
# 6  
Old 03-23-2012
In what way does it 'not work'? What does it do? Show exactly what you did, word for word, letter for letter, keystroke for keystroke.

'something' is a string which is not "Use SSL= 0; REG_DWORD", to demonstrate that it does as you ask, only counting matches of lines like "Use SSL= 0; REG_DWORD". It matches the first one and ignores it, matches the second one and changes it, and does nothing thereafter.
# 7  
Old 03-23-2012
In the "sed" in post #8 there is one too many space both sides of the replace.
If we correct the number of spaces in the "sed" it appears to work.
Code:
                        echo "Use SSL=             0;          REG_DWORD" | \
sed 's/Use SSL=             0/Use SSL=             0x1/g' 

./scriptname
Use SSL=             0x1;          REG_DWORD

c/f oribinal line
Code:
Use SSL=             0;          REG_DWORD


It will take a "sed" expert to replace the second occurrance (and subsequent?). Not really clear what is required without a better before and after example.

Last edited by methyl; 03-23-2012 at 12:21 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I replace a string in file that is in a certain position with spaces?

I am trying to replace the string in position 26 through 35 of the data file with 10 spaces and I want the remaining file to stay as is, the record length is over 900 characters? I am trying to use the AWK and substr but I am not getting it formatted correctly. Before... (6 Replies)
Discussion started by: fnwine1500
6 Replies

2. Shell Programming and Scripting

How to find and replace a string with spaces and / recursively?

Hi all, I wanted to find and replace an email id from entire directory structure on a Linux server. I found that find . -type f -print0 | xargs -0 sed -i 's/abc@yahoo.com/xyz@gmail.com/g' would do it perfectly. But my search criteria has extended and now I want to search for a string1 like... (2 Replies)
Discussion started by: pat_pramod
2 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 with spaces

Hi Guys file:///C:/DOCUME%7E1/c104058/LOCALS%7E1/Temp/moz-screenshot.pngsed 's///g' /source/filename.txt > /destination/filename.txt The above code deletes the characters which are not A-Z, a-z and 0-9, but I wanted to replace it with space without deleting them. Any help is... (2 Replies)
Discussion started by: gowrishankar05
2 Replies

5. Shell Programming and Scripting

replace 2 spaces by one

Dear Friends, I have a flat file from which I want to remove single "space". And, wherever two spaces are provided it should replace it by only one space. E.g. I have N A T I O N A L E D U C A T I O N F O R O R G AN I S A T I ON S I want NATIONAL EDUCATION FOR ORGANISATIONS Please... (5 Replies)
Discussion started by: anushree.a
5 Replies

6. Shell Programming and Scripting

Help with sed matching <tag1> newline spaces <tag2> and replace the value in the same string format

Hi, I'm very new to shell scripting and have searched google and this forum for quite some time now. I have the following in my xml file: <recipients> <member>value1</member> </recipients> I need to find a string <recipients> that follows with a new-line and bunch of spaces and... (5 Replies)
Discussion started by: mgharios
5 Replies

7. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

8. Shell Programming and Scripting

sed: replace string with another string (with spaces)

Hi I have an XML file with strings XABCD, XEFGHX and XIJKLX. I would like to replace XABCDX with "This is the first string", XEFGHX with "This is the second string" and XIJKLX with "This is the third string". What is the best way to implement this? Should I have a file with the data that is... (4 Replies)
Discussion started by: zmfcat1
4 Replies

9. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

10. Shell Programming and Scripting

Replace spaces

Hi guys, so I have another issue. Can I use sed to replace spaces in a string or variable with %20 I am having trouble with using curl on URL's containing spaces Thanks! (12 Replies)
Discussion started by: tret
12 Replies
Login or Register to Ask a Question