?
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
Medium Priority
?
262 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.
Suggested Courses

571 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