Linux Classes
Linux Classes
Share This With a Friend

sed command help

 sed - a Stream EDitor
 

SYNOPSIS

 sed [-n] [-V] [--quiet] [--silent] [--version] [--help]
 [-e script] [--expression=script]
 [-f script-file] [--file=script-file]
 [script-if-no-other-script]
 [file...]
 

DESCRIPTION

 Sed is a stream editor. A stream editor is used to perュ
 form basic text transformations on an input stream (a file
 or input from a pipeline). While in some ways similar to
 an editor which permits scripted edits (such as ed), sed
 works by making only one pass over the input(s), and is
 consequently more efficient. But it is sed's ability to
 filter text in a pipeline which particularly distinguishes
 it from other types of editors.
 

OPTIONS

 Sed may be invoked with the following command-line
 options:
 
 -V
 
 --version
 Print out the version of sed that is being run and
 a copyright notice, then exit.
 
 -h
 
 --help Print a usage message briefly summarizing these
 command-line options and the bug-reporting address,
 then exit.
 
 -n
 
 --quiet
 
 --silent
 By default, sed will print out the pattern space at
 the end of each cycle through the script. These
 options disable this automatic printing, and sed
 will only produce output when explicitly told to
 via the p command.
 
 -e script
 
 --expression=script
 Add the commands in script to the set of commands
 to be run while processing the input.
 
 --file=script-file
 Add the commands contained in the file script-file
 to the set of commands to be run while processing
 the input.
 
 If no -e,-f,--expression, or --file options are given on
 the command-line, then the first non-option argument on
 the command line is taken to be the script to be executed.
 
 If any command-line parameters remain after processing the
 above, these parameters are interpreted as the names of
 input files to be processed. A file name of - refers to
 the standard input stream. The standard input will proュ
 cessed if no file names are specified.
 

Command Synopsis

 This is just a brief synopsis of sed commands to serve as
 a reminder to those who already know sed; other documentaュ
 tion (such as the texinfo document) must be consulted for
 fuller descriptions.
 
 Zero-address ``commands''
 : label
 Label for b and t commands.
 
 #comment
 The comment extends until the next newline (or the
 end of a -e script fragment).
 
 } The closing bracket of a { } block.
 
 Zero- or One- address commands
 = Print the current line number.
 
 a \
 
 text Append text, which has each embedded newline preュ
 ceeded by a backslash.
 
 i \
 
 text Insert text, which has each embedded newline preュ
 ceeded by a backslash.
 
 q Immediately quit the sed script without processing
 any more input, except that if auto-print is not
 diabled the current pattern space will be printed.
 
 r filename
 Append text read from filename.
 
 { Begin a block of commands (end with a }).
 
 b label
 Branch to label; if label is omitted, branch to end
 of script.
 
 t label
 If a s/// has done a successful substitution since
 the last input line was read and since the last t
 command, then branch to label; if label is omitted,
 branch to end of script.
 
 c \
 
 text Replace the selected lines with text, which has
 each embedded newline preceeded by a backslash.
 
 d Delete pattern space. Start next cycle.
 
 D Delete up to the first embedded newline in the patュ
 tern space. Start next cycle, but skip reading
 from the input if there is still data in the patュ
 tern space.
 
 h H Copy/append pattern space to hold space.
 
 g G Copy/append hold space to pattern space.
 
 x Exchange the contents of the hold and pattern
 spaces.
 
 l List out the current line in a ``visually unambiguュ
 ous'' form.
 
 n N Read/append the next line of input into the pattern
 space.
 
 p Print the current pattern space.
 
 P Print up to the first embedded newline of the curュ
 rent pattern space.
 
 s/regexp/replacement/
 Attempt to match regexp against the pattern space.
 If successful, replace that portion matched with
 replacement. The replacement may contain the speュ
 cial character & to refer to that portion of the
 pattern space which matched, and the special
 escapes 1円 through 9円 to refer to the corresponding
 matching sub-expressions in the regexp.
 
 w filename Write the current pattern space to fileュ
 Transliterate the characters in the pattern space
 which appear in source to the corresponding characュ
 ter in dest.
 

Addresses

 Sed commands can be given with no addresses, in which case
 the command will be executed for all input lines; with one
 address, in which case the command will only be executed
 for input lines which match that address; or with two
 addresses, in which case the command will be executed for
 all input lines which match the inclusive range of lines
 starting from the first address and continuing to the secュ
 ond address. Three things to note about address ranges:
 the syntax is addr1,addr2 (i.e., the addresses are sepaュ
 rated by a comma); the line which addr1 matched will
 always be accepted, even if addr2 selects an earlier line;
 and if addr2 is a regexp, it will not be tested against
 the line that addr1 matched.
 
 After the address (or address-range), and before the comュ
 mand, a ! may be inserted, which specifies that the comュ
 mand shall only be executed if the address (or address-
 range) does not match.
 
 The following address types are supported:
 
 number Match only the specified line number.
 
 first~step
 Match every step'th line starting with line first.
 For example, ``sed -n 1~2p'' will print all the
 odd-numbered lines in the input stream, and the
 address 2~5 will match every fifth line, starting
 with the second. (This is a GNU extension.)
 
 $ Match the last line.
 
 /regexp/
 Match lines matching the regular expression regexp.
 
 \cregexpc
 Match lines matching the regular expression regexp.
 The c may be any character.
 

Regular expressions

 POSIX.2 BREs should be supported, but they aren't comュ
 pletely yet. The \n sequence in a regular expression
 matches the newline character. There are also some GNU
 extensions. [XXX FIXME: more needs to be said. At the
 very least, a reference to another document which
 describes what is supported should be given.]
 
 This version of sed supports a \<newline> sequence in all
 regular expressions, the replacement part of a substitute
 (s) command, and in the source and dest parts of a
 transliterate (y) command. The \ is stripped, and the
 newline is kept.
 

SEE ALSO

 awk(1) , ed(1) , expr(1) , emacs(1) , perl(1) , tr(1) , vi(1) ,
 regex(5)  [well, one ought to be written... XXX], sed.info,
 any of various books on sed, the sed FAQ
 (http://www.wollery.demon.co.uk/sedtut10.txt,
 http://www.ptug.org/sed/sedfaq.htm).
 

BUGS

 E-mail bug reports to bug-gnu-utils@gnu.org. Be sure to
 include the word ``sed'' somewhere in the ``Subject:''
 field.
 


Comments - most recent first
(Please feel free to answer questions posted by others!)

krevotsk@gmail.com (12 Apr 2013, 13:13)
I am trying to look for a pattern in a line and if it exists change something else in that line globally in a file I have tried this but it changes all occurrences not just the lines with the pattern
sed -n "/onnection/p" $file|sed -e "s/_Dev/_Qas/g" $file >> test1_parsed.exp
krevotsk@gmail.com (11 Apr 2013, 18:55)
I want to search a file for pattern A. in every liune that contains pattern A change the text if it occurs in that line /aaa/bbb globally
Randy Vrabel (07 Sep 2012, 20:57)
I want to use SED to do a search through a text file for a line or lines that match the line or lines in another text file, replacing those lines with the lines in another text file. If this is possible could you send some examples of how the SED command would be coded? Thanks much.
arun mahajan (01 Dec 2011, 20:34)
I want to search records in a file through birth year by sed command...please show me the syntax

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:




Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.

Popular Linux Topics

Linux Intro
Linux Files
Linux Commands
Change Password
Copy Files
Linux Shell Basics

Linux Tutorial

Who is Doctor Bob?
What is Linux?
History of Unix
Operating Systems
What's Next?

Linux Basics

Living in a Shell
Root and Other Users
Virtual Consoles
Logoff and Shutdown
Choosing a Shell
The Command Prompt
Wildcards
Command History
Aliases
Redirection
Pipelines
Processes
Stopping a Program
Environment Variables
Help!

Linux Files

The Linux File System
Linux File Names
Linux Directories
Directory Terminology
Navigating the File System
Listing Linux Files
Displaying Linux Files
Copying and Renaming Files
Creating Files and Directories
Deleting Files and Directories
Linux Files - Wildcards
The Nine Deadly Keystrokes
Linux File Permissions
Changing File Permissions

Linux Commands

Important Linux Commands
Changing Your Password
Switching Users
Who is Logged In?
Date and Time
The Echo Command
Spell Checking
Printing Linux Files
Joining Files
Searching for Files
Comparing Files
Task Scheduling
Linking Files

Linux Editors

The Vi Editor
The Emacs Editor
The Pico Editor

Linux Data Manipulation

Slicing & Dicing
Heads or Tails?
Sorting Data
Eliminating Duplicates
Selecting Columns
Selecting Records
Search & Replace
Crunching Data
Finding Files
Pipe Fitting

Linux Shell Programming

Linux Shell Scripts
Executing a Script
Shell Script Variables
Shell Script Logic
Shell Script Looping
Shell Script Debugging

Perl Programming

Perl Basics
Perl Variables
Perl Arguments
Perl Logic
Perl Looping
Perl and Files
Perl Pattern Matching

Linux and Email

Sending Email
Reading Email
Other Mail Commands
Using Pine for Email
The Pine Inbox
Pine Email Basics
Pine Email Folders
Pine for Power Users

Compression and Encoding

Linux File Compression
Archiving With Tar
Compression With Gzip
Compress and Zcat
Zmore and Zless
Zip and Unzip
Encoding and Decoding
Encryption

Linux Does DOS

Accesing DOS Files
Accesing DOS Partitions
Running DOS Programs

Managing Linux

Updating Your Linux System
Installing Packages with RPM
Uninstalling Packages w/ RPM
Upgrading Packages with RPM
Querying Packages with RPM

AltStyle によって変換されたページ (->オリジナル) /