Solved

sed command.

Posted on 2010-11-27
1
429 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 76

Accepted Solution

by:
arnold earned 500 total points
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now