Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

sed command.

Posted on 2010-11-27
1
Medium Priority
?
486 Views
Last Modified: 2012-05-10
Hi,

I have a web2 file that I would like to change all occurrences of coffee to chocolate  only on the lines starting with a 2. Using the sed command.
0
Comment
Question by:9334gonzalez
1 Comment
 
LVL 81

Accepted Solution

by:
arnold earned 2000 total points
ID: 34223415
sed '/^2\./ s/coffee/chocolate' filename

The following link includes examples that may enhance your current use of sed.
http://www.unix.com/shell-programming-scripting/59127-search-replace-restriction-awk-sed.html

the above example will output the results after processing the file.

You can use vi/vim with /^2\./ s/coffee/chocolate/ to achieve the same goal.
vi -c  '/^2\./ s/coffee/chocolate/|wq' filename
note that coffees will also be replaced with chocolate to end up with chocolates.

add g if there are multiple replacements per line (same applies to sed) i.e.
vi -c  '/^2\./ s/coffee/chocolate/g|wq' filename

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 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