Reese Knowledgebase

sed one liners

View Kristian Reese's profile on LinkedIn

If you like this article, please +1 or Recommend via FB with the provided buttons above:

Article ID: 95
by: Reese K.
Posted: 20 Mar, 2013
Last updated: 11 Sep, 2015
Views: 4458

sed one liners

Insert character at beginning of line

Example: comment out all nfs entries in /etc/fstab

sed -i '/nfs/s/^/#/' /etc/fstab

Uncomment out all nfs entries in /etc/fstab

sed -i '/nfs/s/^#//g' /etc/fstab

Substitute a character at a specific line

Change line 108 of /etc/sudoers by removing the comment and immediate space that follows from wheel group

sed -i -e '108s/# //g' /etc/sudoers

Insert a string at a specific line number

sed -i '14iparser = future' /etc/puppetlabs/puppet/puppet.conf

Make a copy of a file, then substitue a string in the original

perl -i.orig2 -p -e 's/' /etc/fstab

Print contents of a file between lines 1 and 609

sed -n '1,609'p /var/log/tspulsemail.log > index.html

Substitute line feed with a space

awk '{ print $1 }' | tr '\n' ' ' < file

perl -p -e 's/\n/ /' file  

To use BOTH Unix $environment_vars and sed /end-of-line$/ pattern matching

Excerpt taken from:

The most readable is to enclose the script in "double quotes" so the shell can see the $variables, and to prefix the sed metacharacter ($) with a backslash:

     sed "s/$user\$/root/" file

the shell interpolates $user and sed interprets \$ as the symbol for end-of-line.

Another method is to concatenate the script with 'single quotes' where the $ should not be interpolated and "double quotes" where variable interpolation should occur:

     sed "s/$user"'$/root/' file
This article was:   Helpful | Not Helpful
Also listed in
folder UNIX

Prev   Next
Installing Teradata Client 13.10 on RedHat 6 x86_64     Display top 10 processes eating up CPU