Sed Awk Grep Bash
Sed Awk Grep Bash
2017
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 1
Spring 2017
Intro to Odyssey
Thursday, February 2nd 11:00AM – 12:00PM NWL 426
Intro to Unix
Thursday, February 16th 11:00AM – 12:00PM NWL 426
Extended Unix
Thursday, March 2nd 11:00AM – 12:00PM NWL 426
For other questions or issues, please submit a ticket on Parallel Job Workflows on Odyssey
the FASRC Portal https://portal.rc.fas.harvard.edu
Thursday, April 20th 11:00AM – 12:00PM NWL 426
Or, for shorter questions, chat with us on Odybot
https://odybot.rc.fas.harvard.edu Registration not required — limited seating.
https://rc.fas.harvard.edu 3
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 2
Spring 2017
Objectives
• Unix commands for searching
– REGEX
– grep
– sed
– awk
• Bash scripting basics
– variable assignment
• integers
• strings
• arrays
– for loops
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 3
Spring 2017
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 4
Spring 2017
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 5
Spring 2017
sed - Examples
• Take the time to create abc.txt file below and try out examples
abc abc
def mno
ghi pqr
jkl sed ‘2,4d’ abc.txt stu
mno vwx
pqr yz
stu
vwx
yz
abc 123
def def
ghi ghi
sed ‘s/abc/123/’ abc.txt
jkl jkl
mno mno
pqr pqr
stu stu
vwx vwx
yz yz
11
Objectives
• Unix commands for searching
– REGEX
– grep
– sed
– awk
• Bash scripting basics
– variable assignment
• integers
• strings
• arrays
– for loops
12
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 6
Spring 2017
awk
• command/script language that turns text into records and fields
which can be selected to display as kind of an ad hoc database.
With awk you can perform many manipulations to these fields or
records before they are displayed.
• syntax:
– using stdin: cat file | awk ‘command’
– using files: awk ‘command’ file
• concepts:
– Fields:
• fields are separated by white space, or by regex FS.
• The fields are denoted $1, $2, ..., while $0 refers to the entire line.
• If FS is null, the input line is split into one field per character.
– Records:
• records are separated by \n (new line), or by regex RS.
13
awk
• A pattern-action statement has the form:
pattern {action}
BEGIN {action}
{action}
END {action}
14
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 7
Spring 2017
15
alpha,gamma
awk '{OFS=",";print $1, $3}' alpha.txt delta,phi
lph
awk -Fa ‘{print $2}' alpha.txt epsilon phi
16
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 8
Spring 2017
awk - statements
• An action is a sequence of statements. A statement can be one of
the following:
– if (expression) statement [ else statement ]
– while (expression) statement
– for (expression ; expression ; expression) statement
– for (var in array) statement
– do statement while (expression)
epsilon
awk '{if (NR > 1) print $2}' alpha.txt
awk - variables
• Using variables:
– You can use the stock $1, $2, $3, … fields and set them to variables in the action
block.
alpha beta gamma
delta epsilon phi
alpha delta
123 + 456
Total: 2
18
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 9
Spring 2017
awk - mathematics
The operators in AWK,
Assignment = += -= *= /= %= ^=.
• Both absolute assignment (var = value) and operator-assignment
(the other forms) are supported.
19
20
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 10
Spring 2017
Objectives
• Unix commands for searching
– REGEX
– grep
– sed
– awk
• Bash scripting basics
– variable assignment
• integers
• strings
• arrays
– for loops
21
# Executing commands
echo “Var 1 is set to: $var1” Use a variable with “$”
cd $dir1
pwd
22
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 11
Spring 2017
#!/bin/bash
# Setting vars
var1=“1.txt 2.txt 3.txt 4.txt” string variable
# For loop
for i in $var1 ; do looping through each element in the string
echo $i
done
23
#!/bin/bash
j=0
for i in {01..05} ; do { } defines a range
j=$((j+1)) increment j
alpha[$j]=$i use j to index alpha array
echo ${alpha[*]} print all elements of alpha array
done
24
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 12
Spring 2017
Questions ???
h-ps://rc.fas.harvard.edu/training/
spring-2017/ 13