Solved

sed command.

Posted on 2010-11-27
1
462 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 77

Accepted Solution

by:
arnold earned 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to print sub line with  sed /awk /perl in bash file 11 48
Samba Question 11 76
EXCHANGE. LINUX, SYMANTEC MESSAGING GATEWAY 2 38
CentOS 7 Linux for HP DL380 G4 32Bits 7 47
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
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 …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

839 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