Solved

sed command.

Posted on 2010-11-27
1
470 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 78

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Fine Tune your automatic Updates for Ubuntu / Debian
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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