Computing.Net > Forums > Unix > Search and replace string in files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Search and replace string in files

Reply to Message Icon

Name: AndrewG1976
Date: June 21, 2007 at 19:29:08 Pacific
OS: Solaris 9
CPU/Ram: 440Mhz/512MB
Comment:

Hi,

Can anyone help me with the command to do the following search and replace in the following files:

Files: file1 file2 file3
Search String: "GET /thisfile?id=
Replace String: "GET /thisfile?source=1&id=

I think 'sed' can do this but I don't know the syntax.

Thanks,
Andrew



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: June 21, 2007 at 20:09:14 Pacific
Reply:

the first thing to do when you don't know something is to look it up, books, online search, you name it. Then try to do it, and if still don't get it, you can post to forums to ask. I've thought of not posting, but because today is a friday (coming to the weekends) and i am happy, i can give you a head start. :-)
[code]
sed 's/<search string>/<replacement>/g' "file"
[/code]


0

Response Number 2
Name: AndrewG1976
Date: June 21, 2007 at 20:57:28 Pacific
Reply:

I should have mentioned that I did research this and I came across the sed command but it doesn't work for me. I think because of the ? and = characters in my string.

I tried the following and neither worked:

sed 's/"GET /thisfile?id=/"GET /thisfile?source=1&id=/g' file1

I also tried escaping the " and ? and =

sed 's/\"GET \/thisfile\?id\=/\"GET \/thisfile\?source\=1\&id\=/g' file1


0

Response Number 3
Name: ghostdog
Date: June 21, 2007 at 23:04:21 Pacific
Reply:

ok..this is trivial..provide a sample input file content.


0

Response Number 4
Name: AndrewG1976
Date: June 21, 2007 at 23:38:58 Pacific
Reply:

The file is an Apache log file. Sample input file content:

10.0.0.1 - - [02/Jun/2007:00:08:19 +1000] "GET" "GET /thisfile?id=1 HTTP/1.1" "?id=1" 301 337 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4"

10.0.0.2 - - [02/Jun/2007:00:08:39 +1000] "GET" "GET /thisfile?id=2 HTTP/1.1" "?id=2" 301 365 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4"


0

Response Number 5
Name: ghostdog
Date: June 22, 2007 at 00:44:57 Pacific
Reply:

sorry, i use awk.
[code]
awk '/GET \/thisfile\?id=/ {
replacement="thisfile?source=\\&id="
gsub(/GET \/thisfile\?id=/ ,replacement,$0)
print $0
}' "file"
[/code]


0

Related Posts

See More



Response Number 6
Name: nails
Date: June 22, 2007 at 08:50:52 Pacific
Reply:

Check out this 'gres' discussion:

http://www.computing.net/unix/wwwbo...


0

Response Number 7
Name: Shell_Life
Date: June 25, 2007 at 12:28:55 Pacific
Reply:

sed 's;"GET /thisfile?id=;"GET /thisfile?source=1\&id=;' input_file

Informix/4GL


0

Response Number 8
Name: nails
Date: June 25, 2007 at 12:54:16 Pacific
Reply:

Shell_Life:

Does 'Informix/4GL' mean you are an Informix 4GL developer? Me, too. There are not many of us left.

Nails


0

Response Number 9
Name: AndrewG1976
Date: June 25, 2007 at 20:00:18 Pacific
Reply:

Thanks Shell_Life, that works for me.

Just need to add the output file:

sed 's;"GET /thisfile?id=;"GET /thisfile?source=1\&id=;' input_file > output_file


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Search and replace string in files

Help search and replace www.computing.net/answers/unix/help-search-and-replace/6886.html

Find and Change String in File www.computing.net/answers/unix/find-and-change-string-in-file/4628.html

Search And Replace With Sed www.computing.net/answers/unix/search-and-replace-with-sed/3845.html