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


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
< four
< fourteen
> 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: assembler source, ASCII text
% file /bin/bash
/bin/bash: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=9a57e086388119ecd285c4d5c66823f3f3b68ab5, stripped
% gzip -v <~/monster >monster.gz
% file ~/monster monster.gz
/s/bach/a/class/cs155/monster: UTF-8 Unicode text
monster.gz:                    gzip compressed data, from Unix, last modified: Fri Jan 26 13:53:31 2018

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

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