Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Search and replace string in files

Original Message
Name: AndrewG1976
Date: June 21, 2007 at 19:29:08 Pacific
Subject: Search and replace string in files
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


Report Offensive Message For Removal


Response Number 1
Name: ghostdog
Date: June 21, 2007 at 20:09:14 Pacific
Subject: Search and replace string in files
Reply: (edit)
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]

Report Offensive Follow Up For Removal

Response Number 2
Name: AndrewG1976
Date: June 21, 2007 at 20:57:28 Pacific
Subject: Search and replace string in files
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 3
Name: ghostdog
Date: June 21, 2007 at 23:04:21 Pacific
Subject: Search and replace string in files
Reply: (edit)
ok..this is trivial..provide a sample input file content.

Report Offensive Follow Up For Removal

Response Number 4
Name: AndrewG1976
Date: June 21, 2007 at 23:38:58 Pacific
Subject: Search and replace string in files
Reply: (edit)
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"


Report Offensive Follow Up For Removal

Response Number 5
Name: ghostdog
Date: June 22, 2007 at 00:44:57 Pacific
Subject: Search and replace string in files
Reply: (edit)
sorry, i use awk.
[code]
awk '/GET \/thisfile\?id=/ {
replacement="thisfile?source=\\&id="
gsub(/GET \/thisfile\?id=/ ,replacement,$0)
print $0
}' "file"
[/code]

Report Offensive Follow Up For Removal


Response Number 6
Name: nails
Date: June 22, 2007 at 08:50:52 Pacific
Subject: Search and replace string in files
Reply: (edit)
Check out this 'gres' discussion:

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


Report Offensive Follow Up For Removal

Response Number 7
Name: Shell_Life
Date: June 25, 2007 at 12:28:55 Pacific
Subject: Search and replace string in files
Reply: (edit)
sed 's;"GET /thisfile?id=;"GET /thisfile?source=1\&id=;' input_file

Informix/4GL


Report Offensive Follow Up For Removal

Response Number 8
Name: nails
Date: June 25, 2007 at 12:54:16 Pacific
Subject: Search and replace string in files
Reply: (edit)
Shell_Life:

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

Nails


Report Offensive Follow Up For Removal

Response Number 9
Name: AndrewG1976
Date: June 25, 2007 at 20:00:18 Pacific
Subject: Search and replace string in files
Reply: (edit)
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


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Search and replace string in files

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




XP Installed to G?

exessive internet traffic

ZoneAlarm Question. Blocked Connect

Windows Live Messenger Problem

Delete $Uninstall after SP3 updates


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC