Docsity

Unix Shell Programming: Changing File Names and Processing Text Files, Slides of Artificial Intelligence

Toggle sidebar
bg1
1
CSCI 100
Think Like Computers
Lecture 9
Fall 2008
Unix: Shell Programming
•Unix Shell Programming:
Unix offers not only a variety of tools and
commands, but also a mechanism for you to
use them together
•“Scripting”
Like a “batch” file in Windows
•Create your own tool quickly
Example
•Imagine you have 1000 image files
DSCnnnn.JPG, and you want to:
Change their names to Picnnnn.jpg
Another Example
•You have a list of names and emails, you
want to send each of them a customized
letter for marketing, fundraising, etc
Dealing with Text/Data files
•Tools we already know:
cat, more, less
vi, nano
grep
> file - redirect output (save as a file)
>> file - append output to file
Dealing with Text/Data files
•head - first part of files
head -10 file # get the first 10 lines
•tail – last part of files
tail -10 file # get the last 10 lines
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Unix Shell Programming: Changing File Names and Processing Text Files and more Slides Artificial Intelligence in PDF only on Docsity!

CSCI 100

Think Like Computers

Lecture 9

Fall 2008

Unix: Shell Programming

  • Unix Shell Programming: Š Unix offers not only a variety of tools and commands, but also a mechanism for you to use them together
  • “Scripting” Š Like a “batch” file in Windows
  • Create your own tool quickly

Example

  • Imagine you have 1000 image files

DSC nnnn .JPG, and you want to:

Š Change their names to Pic nnnn .jpg

Another Example

  • You have a list of names and emails, you

want to send each of them a customized

letter for marketing, fundraising, etc

Dealing with Text/Data files

  • Tools we already know:

Š cat, more, less Š vi, nano Š grep

Š > file - redirect output (save as a file) Š >> file - append output to file

Dealing with Text/Data files

  • head - first part of files Š head -10 file # get the first 10 lines
  • tail – last part of files Š tail -10 file # get the last 10 lines

Dealing with Text/Data files

  • wc - word count in file(s) Š wc file
  • sort – sort lines of text file(s) Š sort unsorted > sorted Š sort –n … Š sort –r
  • uniq - remove duplicates from a sorted file Š uniq –c sorted > uniqued

Cut and Paste column

  • Very useful for table and formatted text
  • cut – remove sections from each line Š cut –c1,4-7 file # characters 1 and 4 to 7 Š cut –f1,2 file # 1st^ & 2nd^ fields (tabbed) Š cut –d, -f1 file #1st^ field (comma separated)
  • paste – merge lines of files Š paste –d, file1 file

Example

  • Imagine you have 1000 image files

DSC nnnn .JPG, and you want to:

Š Change their names to Pic nnnn .jpg

  • First, get the nnnn part:

Š ls DSC*.JPG > filenames Š cut –c4-7 filenames > nnnns

Next Step

  • For each nnnn value stored in nnnns,

rename DSCnnnn.JPG to Picnnnn.jpg

  • Some key concepts: Š The for loop Š Variables Š substitutions

The for loop syntax

  • for name in word1 word2 …
  • do
  • do-list # commands
  • done
  • for fruits in apples pears oranges
  • do
  • echo $fruit are fruits
  • done

The Variable

  • for name … Š this part is called a variable
  • The value of the variable is $name
  • …. in apples pears oranges Š this list shows the values for the variable to be used in each loop iteration

Another Example

  • You have a list of names and emails, you

want to send each of them a customized

letter for marketing, fundraising, etc

A Letter

  • Welcome back, Jane!
  • We hope that you and all members
  • of the Public family are constantly
  • reminding your neighbours there
  • on Maple Street to shop with us.
  • As usual, we will ship your order to
  • Ms. Jane Q. Public
  • 600 Maple Street
  • Your Town, Iowa 12345

A Letter

  • The data format (comma separated values, .csv)
  • Public,Jane,Q,Ms.,600,Maple Street,Your Town,Iowa,
  • Welcome back, Jane!
  • We hope that you and all members
  • of the Public family are constantly
  • reminding your neighbours there
  • on Maple Street to shop with us.
  • As usual, we will ship your order to
  • Ms. Jane Q. Public
  • 600 Maple Street
  • Your Town, Iowa 12345

Form Letter

  • Welcome back, $first!
  • We hope that you and all members
  • of the $last family are constantly
  • reminding your neighbours there
  • on $strname to shop with us.
  • As usual, we will ship your order to
  • $salute $first $middlei $last
  • $strnum $strname
  • $town, $state $zip

Write to file

  • echo “Welcome back, $first!” > letter
  • echo “We hope that you and all members” >> letter
  • echo “of the $last family are constantly” >> letter
  • echo “reminding your neighbours there” >> letter
  • echo “on $strname to shop with us. ” >> letter
  • echo “As usual, we will ship your order to” >> letter
  • echo “ $salute $first $middlei $last” >> letter
  • echo “ $strnum $strname” >> letter
  • echo “ $town, $state $zip” >> letter
  • … mail the letter …

Get the values for variables

  • line is: Public,Jane,Q,Ms.,600,Maple

Street,Your Town,Iowa,

Š by reading the datafile

  • echo $line > linefile # write to a file
  • then, Š last=cut –d, -f1 linefile Š first=cut –d, -f2 linefile Š middlei=cut –d, -f3 linefile Š …

Together

  • for line in cat datafile.csv
  • do
  • echo $line > linefile
  • last=cut –d, -f1 linefile
  • first=cut –d, -f2 linefile
  • middlei=cut –d, -f3 linefile
  • …
  • echo “Welcome back, $first!” > letter
  • echo “We hope that you and all members” >> letter
  • echo “of the $last family are constantly” >> letter
  • echo “reminding your neighbours there” >> letter
  • echo “on $strname to shop with us. ” >> letter
  • …
  • done