CS155 Commands3
sed
is a non-interactive editor that operates on streams.
sed 's/pattern/replace/' file > new
sed
does not change the contents of the original file.
Use >
to send changed output to a new file.
All lines: | sed ' command' filename |
One line: | sed ' line command' filename |
Range of lines: | sed ' startline,endline command' filename |
Commands:
s/
old/
new/
(with optional g
flag)
d
A line specification in sed
can be:
$
- the last line
/
pattern/
sed 's/hello/goodbye/g' new_file
sed '/foo/,/bar/d' file
sed '/bob/d' data
new_file
(but don't change new_file
!): sed '2,5d' new_file
foo
with bar
in the first 5 lines of file: sed '1,5s/foo/bar/g' file
sed 's/[,.?-]//g'
filename
sed 's/^ *//'
filename
sed 's/ *$//'
filename
.
matches any character, but \.
matches only the dot itself.
?
matches itself, but \?
is a special character
\|
can be used to match either of two patterns: grep 'one\|two' files
diff
file1 file2
diff
describes the modifications that should be applied to
file1 to create file2
% sed '/^four/d' <~/pub/numbers >some-numbers % echo "booga booga" >>some-numbers % diff ~/pub/numbers some-numbers 4d3 < four 14d12 < fourteen 100a99 > booga booga
a
– add
c
– change
d
– delete
The file
command attempts to determine the type of a file:
% file ~ /s/bach/a/class/cs155: directory % file ~/pub/numbers /s/bach/a/class/cs155/pub/numbers: ASCII text % file /bin/bash /bin/bash: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=bfd7e2e389b63eb588a948f17d3c583c3fafbe23, stripped % gzip -v <~/monster >monster.gz 59.6% % file ~/monster monster.gz /s/bach/a/class/cs155/monster: UTF-8 Unicode text monster.gz: gzip compressed data, last modified: Fri Jan 26 20:53:31 2018, from Unix, original size 401
It doesn’t just look at the file suffix—it looks at the contents. Let’s lie:
% man -Tps date >date-manual.jpg % file date-manual.jpg date-manual.jpg: PostScript document text conforming DSC level 3.0