CS155: Introduction to Unix

Spring 2018

Commands 3

See this page as a slide show

More Commands

CS155 Commands3

sed – stream editor

sed syntax

All lines:sed 'command' filename
One line:sed 'line command' filename
Range of lines:sed 'startline,endline command' filename

Commands:

sed line specification

A line specification in sed can be:

sed: A Few Examples

sed: A Few Examples

sed: More Examples

A Little More Regular Expression Syntax

Alternation through Escaping

diff: Comparing Files

% sed '/^four/d' <~/pub/numbers >some-numbers
% echo "booga booga" >>some-numbers
% diff ~/pub/numbers some-numbers
4d3
< four
14d12
< fourteen
100a99
> booga booga

file: Getting Information on File Type

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

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2017-12-24T13:29

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building