Solved

What can be used in command line to replace a line starting with "something" in a text file.

Posted on 2006-10-31
5
253 Views
Last Modified: 2010-04-21
I have a text file that looks something like this:

118 => some text …
120 => some text …
121 => some text …
130 => some text …

What I’m trying to do is to replace a line starting with lets say “120” with different text for example “120 => some other text”.  Is there an easy way to do this, perhaps with sed, and what would the command look like.

Thanks,
Kate
0
Comment
Question by:kasiencja
  • 3
  • 2
5 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 17845565
sed "s/120 =>.*/120 => some other text/" file >newfile
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 17845576
or to be a little stricter

sed "s/^120 =>.*/120 => some other text/" file >newfile

The ^ means start of line
0
 

Author Comment

by:kasiencja
ID: 17845658
Is it possible to make the changes within the same file, instead of having to write to a newfile?

Thanks,
Kate
0
 
LVL 48

Expert Comment

by:Tintin
ID: 17845834
Depends on your Unix/Linux version.  If your sed version has the -i flag, you can do

sed -i "s/^120 =>.*/120 => some other text/" file

if not you can do

sed "s/^120 =>.*/120 => some other text/" file >/tmp/$$ && mv /tmp/$$ file

or you can use perl
0
 

Author Comment

by:kasiencja
ID: 17845867
Awesome! Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question