Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2006-10-31
Medium Priority
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.

Question by:kasiencja
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 48

Expert Comment

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

Accepted Solution

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

Author Comment

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

LVL 48

Expert Comment

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

Author Comment

ID: 17845867
Awesome! Thanks!

Featured Post

[Webinar] Protection from Cyberattacks

In this session, we’ll dive into the complexities of modern cyber threats and why only multi-vector protection can keep today’s businesses secure through the various stages of a cyberattack, across multiple vectors. Thursday September 14, 2017 10:00 A.M. PDT

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…
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 several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

730 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