1 Day 16 Sed and Awk. 2 Looking through output We already know what “grep” does. –It looks for something in a file. –Returns any line from the file that.

Slides:



Advertisements
Similar presentations
การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation วิบูลย์ วราสิทธิชัย นักวิชาการคอมพิวเตอร์ ศูนย์คอมพิวเตอร์ ม. สงขลานครินทร์ เวอร์ชั่น 1 วันที่
Advertisements

1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin
Scripting Languages CS351 – Programming Paradigms.
Introduction to Unix – CS 21 Lecture 13. Lecture Overview Finding files and programs which whereis find xargs Putting it all together for some complex.
Guide To UNIX Using Linux Third Edition
T UTORIAL OF U NIX C OMMAND & SHELL SCRIPT S 5027 Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015.
Grep, comm, and uniq. The grep Command The grep command allows a user to search for specific text inside a file. The grep command will find all occurrences.
Introduction to Unix – CS 21 Lecture 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
UNIX Filters.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Advanced File Processing
Using the Unix Shell There is No ‘Undelete’. The Unix Shell “A Unix shell is a command-line interpreter or shell that provides a traditional user interface.
1 Day 3 Directories Files Moving & Copying. 2 Case Sensitive First thing to learn about UNIX is that everything is case sensitive. Thus the files: –enda.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
LIN 6932 Unix Lecture 6 Hana Filip. LIN 6932 HW6 - Part II solutions posted on my website see syllabus.
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
Day 2 Review of ITSK1601 – Intro Unix.. Break up into Cousin groups Each cousin group will work as a team. Each team will need 5 blank sheets of paper.
Review Please turn in your homework and practicals sed.
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
1 Day 5 Additional Unix Commands. 2 Important vs. Not Often in Unix there are multiple ways to do something. –In this class, we will learn the important.
Session 2 Wharton Summer Tech Camp Basic Unix. Agenda Cover basic UNIX commands and useful functions.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Quiz 15 minutes Open note, open book, open computer Finding the answer – working to get it – is what helps you learn I don’t care how you find the answer,
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Searching and Sorting. Why Use Data Files? There are many cases where the input to the program may come from a data file.Using data files in your programs.
I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
– Introduction to the Shell 1/21/2016 Introduction to the Shell – Session Introduction to the Shell – Session 3 · Job control · Start,
Sed. Class Issues vSphere Issues – root only until lab 3.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
UNIX commands Head More (press Q to exit) Cat – Example cat file – Example cat file1 file2 Grep – Grep –v ‘expression’ – Grep –A 1 ‘expression’ – Grep.
Lesson 6-Using Utilities to Accomplish Complex Tasks.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Learning Unix/Linux Based on slides from: Eric Bishop.
Filters and Utilities. Notes: This is a simple overview of the filtering capability Some of these commands are very powerful ▫Only showing some of the.
Regular Expressions Copyright Doug Maxwell (
Tutorial of Unix Command & shell scriptS 5027
Lesson 5-Exploring Utilities
Chapter 11 Command-Line Master Class
CIRC Summer School 2017 Baowei Liu
Some Linux Commands.
Unix Scripting Session 4 March 27, 2008.
Unix Operating System (Week Two)
CS 403: Programming Languages
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
Sarah Diesburg Operating Systems CS 3430
What is Bash Shell Scripting?
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
The Linux Command Line Chapter 6
Guide To UNIX Using Linux Third Edition
Tutorial of Unix Command & shell scriptS 5027
Unix Talk #2 (sed).
Chapter Four UNIX File Processing.
Exploring Shell Commands, Streams, and Redirection
Tutorial Unix Command & Makefile CIS 5027
More advanced BASH usage
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
CSC 4630 Meeting 4 January 29, 2007.
Exploring Shell Commands, Streams, and Redirection
Presentation transcript:

1 Day 16 Sed and Awk

2 Looking through output We already know what “grep” does. –It looks for something in a file. –Returns any line from the file that contained the word we searched for. Very often what it returns isn’t exactly what we wanted. –Sometimes we only want on column of output –Sometimes we need to change part of its output.

3 sed Used to search for and replace things. sed s/search/replace/g Example: –sed s/a/b/g –Search for all occurrences of “a” and replace them with “b” –sed s/blah//g –Search for all occurrences of “blah” and remove it…[replace it with nothing].

4 Searching and replacing Lets imagine you run some command such as ls Lots of your files end in.txt, but you don’t want to show that on the screen. ls | sed s/.txt//g

5 Other Example Look through a file and replace all occurrences of the word “edna” with “enda” cat file.txt | sed s/edna/enda/g > newfile.txt Note that sed doesn’t actually change the file, it just outputs the new file, which you can then redirect.

6 Exercise Write a script which takes 3 arguments: replace myfile.txt a b This should run through the file myfile.txt and replace all a’s with b’s. It should output the result to the screen.

7 Regular Expressions Sed actually supports an entire language called regular expressions. They allow you to do many complex things, however they are beyond the scope of this class. For more information –man regex

8 Information in Columns Sometimes the text you want is only one column of the output. For example –w Gives you way too much information. What if you only care about who is logged in. –You can use awk to find out

9 awk awk is an entire programming language. It was specifically designed to help you parse lots of text and do stuff with it. For this class we are just going to use it to show us one column of text at a time.

10 Using awk awk ‘{print $1}’ –The $1 here has nothing to do with shell parameters. –This would mean print out only the first column of the input. awk ‘{print $2}’ –This would print out the 2nd column. etc.

11 Who’s on If the only thing we care about is WHO is logged in, we can do: w | awk ‘{print $1}’ | sort | uniq | more This would give us a sorted list of every person logged in.

12 One Column Imagine you have a grade file, and you do: grep -i hw grades HW1: 80 HW2: 90 HW3: 20 However you just want the actual grades. grep -i hw grades | awk ‘{print $2}’ Will print out only the second column.

13 More than one column awk can print more than one column at a time Perhaps you want file name, and size: ls -l | awk ‘{print $9 “ - “ $5 }’

14 Exercise Write a script which: –Takes one argument, (a users name) –Uses the last command to find out when that user last logged in. –However it should one line, like this: endalast logged in Mar 18 at 23:55

15 last on last enda | awk '{print $1 " last logged in " $5 $6 " at " $7}' | head -1 –This does a last on enda. –Looks for the first, fifth, sixth, and seventh column. –Only shows us the most recent line