LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-06-2008, 04:52 AM   #1
sph90457
Member
 
Registered: Oct 2003
Location: UK
Posts: 52

Rep: Reputation: 15
Global search and replace in vi


Hello,

Does anyone know how I can perform a search and replace for the following in vi:-

I have 01483012302080818220208 on lines 3, 1031 & 2065 of my file and I would like to change the first 5 characters from 01483 to 01036 on those lines.

cheers...

Last edited by sph90457; 03-06-2008 at 07:28 AM.
 
Old 03-06-2008, 05:13 AM   #2
niceguy_81333
Member
 
Registered: Mar 2008
Location: India
Distribution: RHEL5
Posts: 34

Rep: Reputation: 15
open the file in VI editor and type
:g/01483/s//01036
This substitutes first occurrence of 01483 on every line of the file with 01036
rgds
bil
 
Old 03-06-2008, 05:14 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

If the string is unique (only present at the beginning of the line and only on lines 3,1013 and 2065), you can do something like this:

sed 's/^01483\(.*\)/01036\1/' infile

Hope this helps.
 
Old 03-06-2008, 06:54 AM   #4
sph90457
Member
 
Registered: Oct 2003
Location: UK
Posts: 52

Original Poster
Rep: Reputation: 15
Does this work with HPUX

Hello,

Would this work with HPUX to ?

I tried what you suggested below on my file 0036

sed -e 's/^01483\(.*\)/01036\1/' 0036

or is the above wrong ?

Also - how do I specify the lines ? Sorry for all the questions
 
Old 03-06-2008, 08:46 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Quote:
Would this work with HPUX to ?
On most (all?) HP boxes/OS's I've worked with sed is available/installed. It could be that you use an older version, but the basic functionality (which is used in the above example) is the same. So, yes it should work on HPUX.

Quote:
I tried what you suggested below on my file 0036

sed -e 's/^01483\(.*\)/01036\1/' 0036

or is the above wrong ?
Did you get any error messages?

the -e option isn't needed, but the command looks correct (scan all lines of the file, if a line starts with 01483 replace it with 01036 and do this for the file called 0036).

Quote:
Also - how do I specify the lines ?
Sed can work with ranges (or a single line):

sed '4s/X/Y/g' infile => changes all X's to Y's on line 4
sed '1000,2000s/A/B' infile => Change the first A in a line to B and do this for lines 1000 to 2000 (both are included)

Hope this clears things up.
 
Old 03-06-2008, 10:59 AM   #6
sph90457
Member
 
Registered: Oct 2003
Location: UK
Posts: 52

Original Poster
Rep: Reputation: 15
Worked once..

Well I managed to get it to work once but I was logged in as root and couldn't recall the command and it has not worked since. The sed commands seems to run through the file and when you check the lines starting with 01483 then you find none of them have changed.

is there any way I could run the :g/01483/s//01036 command from the shell as that works fine.

What I am after is shell script that changes lines 01483 and lines 17483 to 01XXX and 17XXX (where the xxx is my required number, this is also the file name to).
 
Old 03-06-2008, 11:31 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The sed command we have been talking about will not change the infile, all it will do is print the, changed, content to screen.

If you have sed version 4.x you can use the -i flag to change 'in place' (do make sure the command does what you want it to do first). If you use a sed version that is older then 4.x you need 2 steps:

sed '17483s/^01483\(.*\)/01036\1/' 0036 > /tmp/0036.tmp
mv /tmp/0036.tmp 0036


The first step will put the output of the sed command into a new file (/tmp/0036.tmp) and the second step moves that file to 0036 (overwriting the original!!).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to share a ZFS file system between a global zone and a non global zone? crisostomo_enrico Solaris / OpenSolaris 7 11-28-2007 08:20 AM
LXer: Shameful Human Toll As Search Engines Go Global LXer Syndicated Linux News 0 07-03-2006 02:33 AM
global search and replace? jkcunningham Linux - General 5 03-15-2006 02:43 PM
Quick VIM question (unhighlighting search terms after search) lrt2003 Linux - Newbie 5 05-08-2004 05:21 PM
linking downloaded c library to global search path syoh Programming 1 10-17-2003 01:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration