The others have given you direct solutions, but let me discuss the reasons. You need the right tool for the right job.
sed is the "stream editor", and works on a per-line basis. It takes one line at a time into its pattern buffer (as determined by newline characters), applies a series of editing commands to it, then clears the buffer and grabs the next line. Which lines it grabs can be controlled by conditions you give it, or else it goes through the whole file line by line.
sed only sees the pattern buffer contents as a single continuous text stream; it has no concept of "fields". Thus the only way to extract a part of a line is to use the s/// command and regular expression pattern matching.
sed does also have some multi-line ability, but it's complex and awkward to use. Again, there are usually other tools that work better.
awk, on the other hand, is field based. You define the fields you want it to operate on, then you can print, edit, or reorganize them pretty much as you please.
It does, in fact, have two main input controls. First, the record separator determines how much of the file it will operate on in a single cycle. This is a single line by default, like sed, but it can be configured to do a paragraph at a time, for example, or even the whole file at once.
Then awk also has the field separator, which defines how the text fields inside each record are determined. The default is whitespace (any amount of contiguous spaces and/or tabs).
awk also has a much more comprehensive collection of functions available to it, and is able to apply complex conditions, loops, and mathematical operations to the input text.
This all means that sed is usually more convenient when doing simple single-line string extractions, insertions, alterations, and deletions, while awk is generally better for extracting individual fields from lines or blocks of text, and for doing more complex multi-line operations.
By the way, don't discount the convenience of chaining commands together either . cut is a single-purpose tool that does what it does quickly and efficiently. There's really nothing wrong with chaining sed and cut together to do what you want (except why use two programs when one is sufficient?
); you just need to do it properly. Either work from left to right to pipe the output of one command into the input of another, or use either
process substitution, or
command substitution with a
here string to simulate input from a file.
And since
$(..) is recommended over `..`, all of the following should work:
Code:
#using a pipe
#perhaps the most common method
cut -f1 myfile | sed -n 25p
sed -n 25p myfile | cut -f1
#using process substitution
#the space between the two <'s is required
sed -n 25p < <(cut -f1 myfile)
cut -f1 < <(sed -n 25p myfile)
#using a here string and command substitution
#quotes are needed here to protect newlines
sed -n 25p <<<"$(cut -f1 myfile)"
cut -f1 <<<"$(sed -n 25p myfile)"
The second instance in each pair is likely better, since it means cut only has to operate on a single line.
Note also that it's not usually necessary to use cat, as the majority of tools such as sed and cut can accept filenames as input. Not to mention that your shell can do
file redirects too.
Here are a few useful sed and awk references.
The grymoire links go to highly-recommended tutorials:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html