String replace that has spaces


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

wow...thank you again for your patience and ideas.

while the last idea works from the command line, on my solaris 8 server, changing the /g to /2g, or /g to /2 (since I want the 2nd pattern in the file to be changed) throws no error, and the pattern in the file is not changed.

the errors thrown from the previous idea to this are here, as is the command used:

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/, "0x1"); } 1' rf > rf2

ld.so.1: printf: fatal: libiconv.so.2: open failed: No such file or directory
awk: syntax error near line 1
awk: bailing out near line 1
ksh: 11696 Killed


Last edited by methyl; 03-23-2012 at 02:06 PM.. Reason: please use code tags
# 9  
Old 03-23-2012
I think that the awk needs to be nawk on your system.



As for the sed idea we just need an expert to advise how to change only the second occurance of the string.

For my benefit, how big is each file?

Reference post #3. What exactly and precisely defines the second occurance of the string? Is it an exact match for the whole line (including all space characters) or a fuzzy match where the number of space characters varies? We need a better definition of the problem, complete with sample input data and matching expected output data.

Last edited by methyl; 03-23-2012 at 02:21 PM..
# 10  
Old 03-23-2012
string replace that has spaces

This is exactly the contents of the file (the edit post is taking out the extra spaces, in this problem there are 10 spaces between SSL= and 0;, and 15 spaces between 0; and REG_DWORD):

Code:
Use SSL=          0;               REG_DWORD
Use SSL=          0;               REG_DWORD

The expected result is as follows:

Code:
Use SSL=          0;               REG_DWORD
Use SSL=          0x1;            REG_DWORD

Maintaining the format of each line.

Last edited by methyl; 03-23-2012 at 06:34 PM.. Reason: please use code tags then we can see the space characters! Now please re-edit the post so the format is consistent.
# 11  
Old 03-23-2012
Still not clear to me. Lines with different amount of spaces, but otherwise identical values, are they considered equal?
Code:
/usr/xpg4/bin/awk '++A[$1,$2,$3,$4]==2{sub("0;","0x1;")}1' infile

or unequal?
Code:
/usr/xpg4/bin/awk '++A[$0]==2{sub("0;","0x1;")}1' infile

Is it for every line (above) or only for lines that start with "Use SSL=" ?
Code:
/usr/xpg4/bin/awk '/^Use SSL=/ && ++A[$1,$2,$3,$4]==2{sub("0;","0x1;")}1' infile

and
Code:
/usr/xpg4/bin/awk '/^Use SSL=/ && ++A[$0]==2{sub("0;","0x1;")}1' infile

respectively...

And is it only the 2nd occurrence that needs the replacement (like above) and not the 3rd, 4th and 5th, or do they? (in that case then use A[..]++ instead of ++A[..]==2 ). Otherwise please post a larger proper sample of you input file with several lines and the desired output...

Last edited by Scrutinizer; 03-27-2012 at 04:15 AM.. Reason: Added spaces for clarity..
# 12  
Old 03-26-2012
SOLVED: string replace that has spaces

There are few words of praise...brilliant...thank you very much.

The goal being to only change the 2nd instance of the pattern (that begins with "Use SSL=" was solved using either of these commands:

Code:
/usr/xpg4/bin/awk '/^Use SSL=/&&++A[$1,$2,$3,$4]==2{sub("0;","0x1;")}1' infile

and

Code:
/usr/xpg4/bin/awk '/^Use SSL=/&&++A[$0]==2{sub("0;","0x1;")}1' infile


Can you reply with an explanation on what is happening here? Why is /usr/xpg4/bin/awk used? What does the &&++A do? The [$0]? The ==2?, and the 1?

Last edited by Scrutinizer; 03-26-2012 at 04:06 PM.. Reason: Code tags
# 13  
Old 03-26-2012
Hi, /usr/xpg4/bin/awk is used because you mentioned you are on Solaris 8 and the standard awk is either broken or ancient, any case it usually not functioning as expected, whereas /usr/xpg4/bin/awk adheres to POSIX specification...

/^Use SSL=/&&++A[$0]==2 means if a line starts with "Use SSL=" and it is the second time that the same line ($0 means the entire line) is encountered, then:
{sub("0;","0x1;")} substitute "0;" by "0x1;" but otherwise leave the line intact.
1 Perform the default action, which is {print $0}, i.e. print the record...
# 14  
Old 03-26-2012
string replace that has spaces

Thanks.

I've never seen &&++A, how is that defined?

How is that different from:

++A[$1,$2,$3,$4]==2

Very interesting actions.
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