Solved

sed command.

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Guacamole and browser performance 1 81
AWS ELB 5 74
LINUX backups with VEEAM 8 74
Remote desktop Ubuntu from Windows 10 5 59
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
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.

813 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