0% found this document useful (0 votes)
10 views25 pages

Unix Text Editing Techniques Guide

Uploaded by

miahajjar95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views25 pages

Unix Text Editing Techniques Guide

Uploaded by

miahajjar95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unix

Administration
Chantal S. Hajjar
Khalil Hariss

USJ - ESIB
Department of Electrical and Mechanical Engineering
Program: Computer and Communications Engineering

1
Unix Administration Chapter 4: Text Editing

Course Outline
1. Introduction to Unix and Linux Operating Systems
2. Linux Basics
3. File Systems and Disk Management
4. Text Editors
5. Shell Scripting
6. System Administration Basics
7. Process and System Monitoring
8. Networking and Security
9. Set Up and Configure Linux Environments

USJ-ESIB DEM – CCE Program 2


Unix Administration Chapter 4: Text Editing

Chapter 4: Text Editing


• Text Editors
• sed command
• cut command
• awk command
• join command
• tr command

USJ-ESIB DEM – CCE Program 3


Unix Administration Chapter 4: Text Editing

Text Editors
• Terminal-based Text mode editors: Nano, Vi or Vim (Vi Improved),
Emacs…
• Editors in graphical mode: Gedit (integrated in GNOME), KWrite
(integrated in KDE), GVim (graphical interface of Vim, for GNOME),
KVim (graphical interface graph of Vim, for KDE).
• To open or edit a file with Gedit myfile: gedit myfile &
If myfile does not exist, the file is created.
GNOME (GNU Network Object Model Environment) is a graphical user interface (GUI) and set
of computer desktop applications for users of the Linux operating system.

KDE is an international free software community producing an integrated set of cross-platform


applications designed to run on Linux.

USJ-ESIB DEM – CCE Program 4


Unix Administration Chapter 4: Text Editing

nano Text Editor


nano is a simple and user-friendly terminal-based text editor.
• To open a file : nano filename
• To add text: start typing directly.
• To undo changes: Alt + U
• To save the file : Ctrl + O (Write Out), then Enter
• To Exit Nano: Ctrl + X
• To cut Text: Ctrl + K (cuts the current line or marked text)
• To copy Text: Ctrl + ^ (start marking), then Alt + 6
• To paste Text: Ctrl + U
• Find Text: Ctrl + W, then enter search term
• Replace Text: Ctrl + \ (search and replace)
USJ-ESIB DEM – CCE Program 5
Unix Administration Chapter 4: Text Editing

nano Text Editor


• Other useful keyboard shortcuts:
▪ Ctrl+G: displays help.
▪ Ctrl+A: go to the beginning of the line.
▪ Ctrl+E: go to end of line.
▪ Ctrl+D: deletes the character under the cursor.
▪ Ctrl+H: deletes the character preceding the cursor.
▪ Ctrl+^: (de)activates markup mode (text selection).
▪ Ctrl+R: insert a text file.

USJ-ESIB DEM – CCE Program 6


Unix Administration Chapter 4: Text Editing

vi Text Editor
• vi is the standard text editor in Linux. vim is an improved version of vi.
• To create and open a file: vi myfile or vim myfile
• Two working modes:
▪ The normal mode for issuing commands.
▪ Insert mode for entering text.
• The vimtutor command launches a tutorial for vi/vim commands.
▪ Basic Vi/Vim commands:
• Mode change:
• i: enable insert mode.
• a: enable insert mode; typing begins at the next character.
• esc: activates normal mode or interrupts the current command.
• Commands in insert mode:
• F1: displays help.
• Ctrl+o cmd: execute the cmd command without exiting insert mode

USJ-ESIB DEM – CCE Program 7


Unix Administration Chapter 4: Text Editing

Basic vi/vim Commands in Normal Mode


• Show help:
▪ F1 or :help
• Mark text:
▪ v: (de)activates character marking mode.
▪ Shift+v: (de)activates line marking mode.
• Delete text:
▪ dw: deletes the next word.
▪ db: deletes the previous word.
▪ Shift+d: delete to end of line.
▪ dd: delete the current line.
▪ ndd: delete n rows.
▪ x: deletes the character under the cursor or the marked text.
▪ Shift+x: deletes the character preceding the cursor.
• Copy text:
▪ Y: copies the marked text.
▪ YY: copies the current line.
• Navigate inside text:
▪ gg or ctrl+home: places cursor at the beginning of the text.
▪ G or ctrl+end: places cursor at the end of the text.

USJ-ESIB DEM – CCE Program 8


Unix Administration Chapter 4: Text Editing

Basic Vi/Vim Commands in Normal Mode


• Paste text:
▪ p: pastes the deleted or copied text at the cursor position.
▪ Shift+p: pastes deleted or copied text before the cursor position.
• Undo and redo commands:
▪ . : repeats the last command.
▪ u: cancels the last command.
▪ Shift+u: undoes all changes to the current line.
▪ Ctrl+r: redo undone commands.
• Search for text:
▪ /searched text: search text forward.
▪ ?searched text: search backwards for text.
▪ n: repeats the search forward.
▪ Shift+n: repeat search backwards.
▪ /\csearched text or ?\csearched text: search for text while ignoring case

USJ-ESIB DEM – CCE Program 9


Unix Administration Chapter 4: Text Editing

Basic Vi/Vim Commands in Normal Mode


• Replace text:
▪ :%s/pattern1/pattern2/g: replaces all occurrences of pattern1 with pattern2 without
confirmation.
▪ :%s/pattern1/pattern2/gc: replaces all occurrences of pattern1 with pattern2 with
confirmation.
• q: aborts the operation.
• a: replaces all subsequent occurrences.
▪ :%s/pattern1/pattern2/gi: replaces all occurrences of pattern1 with pattern2 while ignoring
case.
• Show line numbers:
▪ :set number: displays line numbers.
▪ :set nonumber: disables the display of line numbers.
• Save files:
▪ :w: save the file.
▪ :q: quit Vi/Vim.
▪ :q!: exits Vi/Vim even if changes have not been saved.
▪ :wq or :x: save the file and quit Vi/Vim.

USJ-ESIB DEM – CCE Program 10


Unix Administration Chapter 4: Text Editing

Text Manipulation
The cat command (for concatenate) concatenates files and displays them on the standard output.
• cat myfile prints the file myfile to standard output.
• cat file1 file2 ... concatenates multiple files and prints the result to standard output.
• cat file1 file2 ... > myfile concatenates several files and saves the result in myfile.
• cat file1 file2 ... >> myfile concatenates several files and appends the result to (the end of) myfile.
• cat > myfile saves standard input to myfile: all lines typed in input, up to Ctrl+D, are saved to
myfile.
• cat >> myfile add standard input to myfile: all lines typed in input, up to Ctrl+D, are added to
myfile.
• The tac command (cat spelled backwards) concatenates files and prints them in reverse line order
to standard output.
• cat -n myfile adds line numbers to displayed lines of myfile on the standard output.

USJ-ESIB DEM – CCE Program 11


Unix Administration Chapter 4: Text Editing

sed (stream editor) Command


• The sed command is a powerful text stream editor. It can do insertion, deletion, search and
replace(substitution).
• It processes text line by line and can perform transformations without opening the file in an
editor.
• Commonly used in shell scripting, data processing, and text manipulation tasks.
• Basic Syntax:
▪ sed [options] command filename
• Command itself is composed of two components: addr and cmds.
• sed 'addr cmds' myfile reads myfile line by line, apply the set of cmds commands to any line that
matches addr and returns the result to standard output.
• addr is of the form:
▪ N: cmds is applied to line N.
▪ $ indicates the last line.
▪ M,N: cmds is applied to lines M through N.
▪ /pattern/: cmds is applied to lines that match pattern.
▪ /pattern1/,/pattern2/ : cmds is applied to lines between the first line which matches pattern1 and the first
line which matches pattern2.
• When addr is omitted, cmds is applied to all lines in the input stream.
USJ-ESIB DEM – CCE Program 12
Unix Administration Chapter 4: Text Editing

sed Command
• The main cmds editing commands are:
▪ 'p': displays the lines of the input stream (the current workspace).
▪ 'd': removes (does not display) lines from the input stream.
▪ 'w': copies lines from the input stream to a file.
▪ 'c': replaces lines in the input stream with text.
▪ 'a': adds text after the lines of the input stream.
▪ 'i': adds text before the lines of the input stream.
▪ 'r': inserts a file after the lines of the input stream.
▪ 'e': executes a command to the lines of the input stream.
▪ 's': substitutes strings in the lines of the input stream.
▪ 'G': Insert a blank line between the lines of the input stream.
• Options:
▪ By default, lines from the input stream are printed to standard output. The -n option suppresses the
default display.
▪ By default, the input file remains intact (without transformation). The -i option redirects standard output
to the input file.
▪ The -e option combines several editing commands.
▪ The -f option reads editing commands from a script file

USJ-ESIB DEM – CCE Program 13


Unix Administration Chapter 4: Text Editing

sed Command Examples


• Display lines of text with sed using the 'p' cmd:
▪ sed 'p' myfile duplicates the display of lines from myfile.
▪ sed -n '3p' myfile displays the third line of myfile.
▪ sed -n '2,5p' myfile displays lines 2-5 of myfile.
▪ sed -n '$p' myfile displays the last line of myfile.
▪ sed -n '3,8p' myfile displays lines 3-8 of myfile.
▪ sed -n '/[A-Z]all/p' myfile shows lines from myfile that contain a capital letter followed by all.
▪ sed -n '/^the/,6p' myfile displays lines from myfile which begin with the and the lines which
follow until the sixth line.
▪ sed -n '/[0-9][0-9]*/,$p' myfile shows lines from myfile from the first line to the last line that
contain at least one digit.
▪ sed -n '/BEGIN/,/END/p' myfile displays the lines of myfile between the first line that contains
BEGIN and the first line that contains END.
▪ sed -n '/^$/!p' myfile displays non-empty lines of myfile.
The option -E is used if an extended regex is used.
USJ-ESIB DEM – CCE Program 14
Unix Administration Chapter 4: Text Editing

sed Command Examples


• Delete lines of text with sed with using the 'd' cmd:
▪ sed '4d;7d' myfile removes lines 4 and 7 from myfile.
▪ sed '$d' myfile removes the last line.
▪ sed '/^$/d' myfile remove empty lines from myfile.

• Copy lines of text with sed with using the 'w' cmd:
▪ sed 'w file1' myfile copies all lines from myfile in the file file1.
▪ sed '1,3w file1' myfile copies the first three lines from myfile in the file file1.
▪ sed -e '/pattern1/w file1' -e '/pattern2/w file2' -e '/pattern3/w file3' myfile copies lines from myfile
that match pattern1, pattern2 and pattern3 in file1, file2 and file3 respectively.
• Insert blank lines with the G cmd:
▪ sed G myfile inserts one blank line after each line

USJ-ESIB DEM – CCE Program 15


Unix Administration Chapter 4: Text Editing

sed Command Examples


• Replace lines of text with the 'c' cmd:
▪ sed '4c new text' myfile replaces the fourth line of myfile with new text.
▪ sed '2,6c new text' myfile replaces lines 2-6 of myfile with new text.
▪ sed '/pattern/c new text' myfile replaces lines of myfile that match pattern with new text.
• Add text with the 'a' cmd:
▪ sed '5a new text' myfile appends new text after the fifth line of myfile.
▪ sed '$a new text' myfile appends new text to the end of myfile.
▪ sed '/pattern/i new text' myfile inserts new text before lines in myfile that match pattern.
• Insert files into other files with the 'r' cmd:
▪ sed '2r file1' myfile inserts the file file1 after the second line of myfile.

USJ-ESIB DEM – CCE Program 16


Unix Administration Chapter 4: Text Editing

sed Command Examples


• Replace text with the 's' cmd:
▪ sed 's/pattern/replacement_string/' myfile replaces the first occurrence of pattern in
each line of myfile with replacement_string.
▪ sed 's/BEGIN/START/g' myfile replaces all occurrences of BEGIN in the lines of myfile
with START.
▪ sed '1,3 s/,/|/g' myfile replaces all occurrences of , from line 1 to line 3 of myfile with |.
▪ sed 's/,/|/4' myfile replaces the fourth occurrence of , in the lines of myfile with|.
▪ sed -n 's/pattern/replacement_string/p' myfile only displays transformed lines from
myfile.
▪ sed -n 's/pattern/replacement_string/w file1' myfile only copies transformed lines from
myfile into file1.
▪ sed -n 's/pattern/replacement_string/ip' myfile is case insensitive and displays only
transformed lines of myfile.
sed, a stream editor
USJ-ESIB DEM – CCE Program 17
Unix Administration Chapter 4: Text Editing

sed command – Real-World Examples


• Remove all blank lines from myfile:
▪ sed '/^$/d' myfile
• Replace "foo" with "bar" in all txt files recursively:
▪ find . -type f -name "*.txt" -exec sed -i 's/foo/bar/g' {} \;
• Comment out lines containing "DEMO" in myfile:
▪ sed '/DEMO/s/^/#/' [Link]

USJ-ESIB DEM – CCE Program 18


Unix Administration Chapter 4: Text Editing

cut Command
• The cut command extracts line passages from files.
▪ The -b option extracts passages by bytes.
▪ The -c option extracts passages by characters.
▪ The -f option extracts passages by fields.
▪ The -d option defines the field separator (tabulation \t by default).
• cut -c1 /etc/passwd extracts the first character of lines from /etc/passwd.
• cut -c2-5 /etc/passwd extracts characters 2-5 from lines of /etc/passwd.
• cut -c8- /etc/passwd extracts from the eighth character of lines from /etc/passwd.
• cut -d':' -f1,3 /etc/passwd sets the field separator to : and extracts fields 1 and 3
(login identifier and UID) from the lines of /etc/passwd.
• cut -d':' -f1,3 --output-delimiter=',' /etc/passwd separates the extracted fields by ,
instead of :

USJ-ESIB DEM – CCE Program 19


Unix Administration Chapter 4: Text Editing

awk Command
• The awk language is a data-driven scripting language that doesn't require
compilation. It searches one or more files to see if they contain lines that matches
with the specified patterns and then perform on the them the defined actions.
• awk command usage:
▪ awk '{action}' myfile: to perform the actions specified inside {} on myfile.
▪ awk '/regex pattern/{action}' myfile: to perform the action specified inside {} on text
that matches the regex pattern.
▪ awk 'logical_expr{action}' myfile: to perform the action specified inside {} on text that
matches the logical expression.
• awk can identify fields in a line separated by a special character (space by default).
To access a specific field, we use $n where n is the field number. $0 means the
whole line. $NF references the last field. NR the line number.
• The -F option is used to define the field separator.

USJ-ESIB DEM – CCE Program 20


Unix Administration Chapter 4: Text Editing

awk Command Examples


• awk '{print}' myfile: to print all the lines of myfile
• awk '/manager/ {print}' myfile: to print the lines of myfile containing the pattern manager.
• awk '/manager/' myfile: to print the lines of myfile containing the pattern manager.
• awk '{print $1,$NF}' myfile: to display the first and last field of each line of myfile.
• awk '{print NR,$0}' myfile: to print all lines of myfile preceded by line numbers.
• awk -F'|' '$1 == "yes" && $4 < 40 {print "Field 1 = "$1" and field 4 = "$4", row "NR}' myfile
for any line in myfile, a message is displayed when the first field is yes and the fourth field is
less than 40.
• awk 'length($0) > 10' myfile: prints all lines of myfile with more than 10 characters.
• awk -F'|' '$2 ~ /^[0-9][0-9]*$/ {print "Field 2 of the "NR" line only contains digits: "$2} '
myfile
for any line in myfile, a message may appear if field 2 contains digits only.

USJ-ESIB DEM – CCE Program 21


Unix Administration Chapter 4: Text Editing

awk Command Examples


• awk -F':' '{print "id: "$1", shell: "$7}' /etc/passwd extracts fields 1 and
7 from the lines of /etc/passwd.
• awk '{print $0}' /etc/passwd print all lines from /etc/passwd.
• awk -F':' '{print $3,$1}' /etc/passwd extracts fields 3 and 1 of the lines
in /etc/passwd.
• awk -F':' -v OFS=',' '{print $3,$1}' /etc/passwd (OFS for Output Field
Separator) or awk -F':' '{print $3 "," $1}' /etc/passwd separates
extracted fields with , instead of a space.

USJ-ESIB DEM – CCE Program 22


Unix Administration Chapter 4: Text Editing

Advanced awk commands


• Performing Arithmetic Operations:
▪ awk '{sum += $2} END {print "Total:", sum}' myfile # Sums the 2nd column
• Using Conditionals (if, else):
▪ awk '{if ($2 > 50) print $1, "Passed"; else print $1, "Failed"}' myfile
• Loops:
▪ awk '{for (i = 1; i <= NF; i++) print $i}' myfile # Prints all fields
• Special Blocks:
▪ BEGIN {} — Runs before processing any input lines.
• awk 'BEGIN {print "Name\tScore"} {print $1, $2}' myfile
▪ END {} — Runs after processing all input.
• awk '{sum += $2} END {print "Total Score:", sum}' myfile
• Combining actions:
▪ awk '{sum += $2; count++} END {print "Average:", sum/count}' myfile
Understanding AWK - Earthly Blog
USJ-ESIB DEM – CCE Program 23
Unix Administration Chapter 4: Text Editing

join Command
• The join command merges lines from two files that share a common field.
▪ If directory file contains:
• 555-123-4567 Alice
• 555-231-3891 Bob
• 555-893-1048 Eve
▪ If cities file contains:
• 555-123-4567 Paris
• 555-231-3891 Reindeer
• 555-893-1048 Maroon
▪ join cities directory gives:
• 555-123-4567 Alice Paris
• 555-231-3891 Bob Reindeer
• 555-893-1048 Eve Bordeaux

USJ-ESIB DEM – CCE Program 24


Unix Administration Chapter 4: Text Editing

tr Command
• The tr command transposes or eliminates characters.
▪ tr 'a-z' 'A-Z' < myfile transposes the lowercase letters of myfile in capital letters.
▪ tr '{}' '()' < myfile transposes the braces of myfile in parentheses.
▪ echo 'This is a test' | tr '[:space:]' '\t' transposes white spaces in tabs.
▪ echo 'This is a test' | tr -s '[:space:]' transposes sequences of white spaces into a single
space.
▪ echo 'This is a test' | tr -d ' ' eliminates white spaces.
▪ echo 'my id: 432234' | tr -cd '[:digit:]\n' eliminates all characters except numbers.

USJ-ESIB DEM – CCE Program 25

You might also like