Regular Expressions grep Familiy of Commands

Slides:



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

LINUX System : Lecture 3 (English-Only Lecture) Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University Acknowledgement.
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 31: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Regular Expressions. u A regular expression is a pattern which matches some regular (predictable) text. u Regular expressions are used in many Unix utilities.
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
Chapter 4: UNIX File Processing Input and Output.
Last Updated March 2006 Slide 1 Regular Expressions.
Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.
Regular Expression Darby Tien-Hao Chang (a.k.a. dirty) Department of Electrical Engineering, National Cheng Kung University.
System Programming Regular Expressions Regular Expressions
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.
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 5 Additional useful commands COP 3353 Introduction to UNIX.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 10 Linux.
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/15.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Regular Expression - Intro Patterns that define a set of strings (or, pieces of a string) Not wildcards (similar notion, but different thing) Used by utilities.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Appendix A: Regular Expressions It’s All Greek to Me.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Lesson 4-Mastering the Visual Editor. Overview Introducing the visual editor. Working in an existing file with vi. Understanding the visual editor. Navigating.
May 2008CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
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
Advanced Text Processing. 222 Lecture Overview  Character manipulation commands cut, paste, tr  Line manipulation commands sort, uniq, diff  Regular.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
Regular Expressions CS 2204 Class meeting 6 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1.
UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
What is grep ?  % man grep  DESCRIPTION  The grep utility searches text files for a pattern and prints all lines that contain that pattern. It uses.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
May 2006CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
Regular Expressions Copyright Doug Maxwell (
RE Tutorial.
Lesson 5-Exploring Utilities
Looking for Patterns - Finding them with Regular Expressions
Lexical Analysis.
The UNIX Shell Learning Objectives:
Regular Expression - Intro
Lecture 9 Shell Programming – Command substitution
Week 14 - Friday CS221.
CS 403: Programming Languages
Basic UNIX OLC Training.
Folks Carelli, Instructor Kutztown University
Grep Searches for text in a file Can search for simple words: “chair”
The ‘grep’ Command Colin Masterson.
Advanced Find and Replace with Regular Expressions
Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
Unix Talk #2 (sed).
Selenium WebDriver Web Test Tool Training
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
CSE 303 Concepts and Tools for Software Development
Regular Expressions and Grep
CSCI The UNIX System Regular Expressions
1.5 Regular Expressions (REs)
Validation using Regular Expressions
Lab 8: Regular Expressions
REGEX.
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS
Regular Expressions.
Presentation transcript:

Regular Expressions grep Familiy of Commands Dr. Bill M. 2016

INCS-745 Intrusion Detection Topics Grep Searching text Searching logs INCS-745 Intrusion Detection Lecture 1.1 Copyright © R. A. Mihajlovic

INCS-745 Intrusion Detection Homework Perform 6 different examples of using grep command. Use different RegExp strings. INCS-745 Intrusion Detection Lecture 1.1 Copyright © R. A. Mihajlovic

What is grep Command grep - "general regular expression parser“ Search command for UNIX. Used to search for text strings and regular expressions within one or more files. man grep

Grep Searches for text in a file Can search for simple words: “chair” Can look for “regular expressions”; more complex character strings such as “chair” followed by any number of spaces, followed by a digit or lowercase letter.

Grep usage grep “something” somefile.txt returns all lines with the word something from somefile.txt grep -v “something” something.txt returns all lines that don't have the word something in them grep -i “something” something.txt returns all lines with a mixed upper and lowercase something in them.

Simple regular expressions “[0-9]” look for any digit “[a-zA-Z]” look for one upper or lowercase letter “.” look for one character “.*” any number of characters “\.” a literal decimal point “\.161:” dot, then 161, then colon “\.161[: ]” dot, then 161, then colon or space

Advanced regular expressions Look for lines that hold either “dog” or “cat” grep -e '(dog|cat)' animalfarm.txt Lines that have cat followed by dog on the same line, but possibly with other characters in between: grep 'cat.*dog' animalfarm.txt cat has to be at the beginning of the line: grep '^cat' animalfarm.txt Look for it at the end of the line: grep 'cat$' animalfarm.txt

Ways to use it Three identical ways to search in a file: grep promiscuous messagesF cat messagesF | grep promiscuous grep promiscuous < messagesF Look for something in multiple files: (zcat /var/log/messages.*.gz ; cat /var/log/messages ) | grep 'promiscuous' | less

Example: Lab Firewall SNMP probes: 161,162 Grep “\.16[12][: ].*udp” firewall.log >snmp.txt Inbound Unix traceroute grep “\.33[45][0-9][0-9][: ].*udp” firewall.log >traceroute.txt FW-1 256-259, 260-261, 264-265, 900, 18207 grep -e “(\.25[6-9][: ]|\.26[0145][: ]|\.900[: ]|\.18207[: ])” firewall.log >fw1.txt Half-life grep “\.27015[: ].*udp” firewall.log >halflife.txt AIX/broken PMTU Size 1500 icmp echo request DF grep “icmp: echo request (DF).*len 1500” firewall.log >aix.txt Note we only get one of these (fw1?)

Example: firewall.log Search 197,128 lines “\.4040[: ]” #CipherIM: 11M! “\.(80\|8080)[: ]” #Web 10.5M “\.53[: ]” #DNS 0.16M “\.22[: ].*( S \|ack)” #ssh 202K “\.25[: ]” 30K “arp” #26K “ripv1” #4K “148\.64\.147\.168” #118K “\.123[: ]” 4K Result: 5K, 150 lines

Common grep Command Options grep [options] pattern [files] -b Display the block number at the beginning of each line. -c Display the number of matched lines. -h Display the matched lines, but do not display the filenames. -i Ignore case sensitivity. -l Display the filenames, but do not display the matched lines. -n Display the matched lines and their line numbers. -s Silent mode. -v Display all lines that do NOT match. -w Match whole word. grep -c Alex my_file.htm

How to use grep command Search file for a user $ grep ad85 /etc/passwd Search file ignoring word case $ grep -i “ad85" /etc/passwd Search recursively all files and directories under given directory $ grep -r “ad85" /etc/

How to use grep command Search for a specific word in file $ grep -w “alex" $HOME/cs265.htm Search for 2 different words in file $ grep -w ‘alex|victoria' $HOME/cs265.htm Count lines that matched in file $ grep -c 'word' $HOME/cs265.htm

How to use grep command Display lines that did not match a pattern $ grep -v cs265 $HOME/cs265.htm Number of lines that contain matched pattern $ grep -n 'word' $HOME/cs265.htm Display filenames that matched pattern, but not lines from the files $ grep -l ‘word' *.htm

grep and Wildcards Dot ( . ) – matches 1 character Asterisks ( * ) – matches multiple characters Examples: grep b.g myfile  finds the words “big”, “bag” grep b*k myfile  finds the word “back”, “buck”, “book”

grep and Regular Expressions A "regular expression" is a pattern that describes a set of strings. Regular expressions are used when you want to search for specific lines of text containing a particular pattern.

grep and Regular Expressions ^ (Caret) = match expression at the start of a line, as in ^A. $ (Dollar Sign) = match expression at the end of a line, as in A$. \ (Back Slash) = turn off the special meaning of the next character, as in \^. [ ] (Brackets) = match any one of the enclosed characters, as in [aeiou]. Use Hyphen "-" for a range, as in [0-9]. [^ ] = match any one character except those enclosed in [ ], as in [^0-9].

grep and Regular Expressions . (Period) = match a single character of any value, except end of line. * (Asterisk) = match zero or more of the preceding character or expression. \{x,y\} = match x to y occurrences of the preceding. \{x\} = match exactly x occurrences of the preceding. \{x,\} = match x or more occurrences of the preceding.

grep and Regular Expressions grep bob files {search files for lines with ‘bob'} grep '^bob' files {‘bob' at the start of a line} grep ‘bob$' files {‘bob' at the end of a line} grep '^bob$' files {lines containing only ‘bob'} grep '\^b' files {lines starting with '^b', "\" escapes the ^} grep '[Bb]mug' files {search for ‘Bob' or ‘bob'} grep 'B[oO][bB]' files {search for BOB, Bob, BOb or BoB } grep '^$' files {search for empty lines} grep '[0-9][0-9]' files {search for pairs of numeric digits}

INCS-745 Intrusion Detection The End INCS-745 Intrusion Detection Lecture 1.1 Copyright © R. A. Mihajlovic