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
256 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
[X]
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
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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 navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

622 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