Text - jasper-zanjani/dotfiles GitHub Wiki
cat
cmp
comm
csplit
cut
diff
diff3
ed
egrep
ex
expand
fgrep
fmt
gettext
grep
groff
head
join
less
look
more
nl
od
paste
pr
sed
shuf
sort
split
tac
tail
tee
tr
unexpand
uniq
wc
a b c d e f h i l n p q r s t u v w x y B C D E F L N S T U W X
binary
from-file
left-column
no-ignore-file-name-case
sdiff-merge-assist
strip-trailing-cr
to-file
suppress-common-lines
unidirectional-new-file
Display text of $FILE, wrapping long lines
fold $FILEPrint first 8 characters of $FILE
head -c8 $FILENumber all lines, including blank lines
nl -b a file
nl --body-numbering=a fileMerge lines of files
Make a .csv file from two lists
paste -d ',' file1 file2Transpose rows
paste -s file1 file2Run sed commands in $SCRIPT on $FILE
sed -f $SCRIPT $FILESuppress automatic printing of pattern space
sed -n
sed --quiet
sed --silentsed ("Stream-oriented editor") is typically used for applying repetitive edits across all lines of multiple files. In particular it is, alongside awk one of the two primary commands which accept regular expressions in Unix systems.
| Script syntax | Effect |
|---|---|
# |
comments begin with octothorpe |
#n |
If first line of script begins with these two characters, it is equivalent to using the -n flag |
Invocation syntax has two forms:
# Inline
sed options 'instruction' $FILE
# "Command file"
sed options -f $SCRIPT $FILEsed instructions are made of addresses and procedures . Sources do not use consistent terminology to describe the two components of most sed commands:
:--- | :---
sed 'pattern {procedure}' file | [SA: 14]
sed 'address {action}' file | [YUG: 449]
sed 'address {instruction}' file | [PGL: 565]
Zero, one, or two addresses can precede a procedure.
- In the absence of an address, the procedure is executed over every line of input
- With one address, the procedure will be executed over every line of input that matches
- With two addresses, the procedure will be executed over groups of lines whereby:
- The first address selects the first line in the first group
- The second address selects the next subsequent line that it matches, which becomes the last line in the first group
- If no match for the second address is found, it point to the end of the file
- After the match, the selection process for the next group begins by searching for a match to the first address Addressing can be done in one of two ways:
-
Line addressing, specifying line numbers separated by a comma (e.g.
3,7p);$represents the last line of input -
Context addressing, using a regular expression enclosed by forward slashes (e.g.
/From:/p)
| Procedure | Description |
|---|---|
!c |
negation operator can be used with any procedure {c} |
a |
append text to the addressed lines |
d |
cause sed not to display the addressed lines ("delete"); can emulate grep -v, which selects lines which do not match the specified pattern |
i |
prepend text to the addressed lines |
n |
write out the currently selected line if appropriate, read the next input line, and start processing the new line with the next instruction |
x |
where {x} is a number, specifying occurrence (e.g. 2 would replace only the second occurrence of each pattern per line) |
g |
replace all occurrences |
p |
print original content (e.g. sed -n 's/test/another test/p' myfile) |
w outputfile |
write results to {outputfile} (e.g. sed 's/test/another test/w output' myfile) |
s/pattern/replacement/flags |
replace regex {pattern} with {replacement} ("substitute") |
g |
replace all instances of the search pattern with the replacement, rather than the first instance (global) |
& |
known as the repeated pattern, represents the represents the entire source string; the only special character used in the replacement string - all other characters are treated literally |
Edit the file in-place, but save a backup copy of the original with {suffix} appended to - the filename
-i=suffixDisplay first 3 lines YUG: 450
sed '3q' emp.lstDisplay first 5 lines, similar to head -5 emp.lst [PGL:569]
sed '5q' newPipe output of ps to sed, displaying top 10 memory-intensive processes
ps axch -o cmd,%mem --sort=-%mem | sed 11qPipe output of ps to sed, displaying top 10 CPU-intensive processes
ps axch -o cmd:15,%cpu --sort=-%cpu | sed 11qDisplay first two lines of file
Without -n, each line will be printed twice
sed -n '1,2p' emp.lstPrepending ! to the procedure reverses the sense of the command YUG: 450
sed -n '3,$!p' emp.lstDisplay a range of lines
sed -n '9,11p' emp.lstUse the -e flag to precede multiple instructions
sed -n -e '1,2p' -e '7,9p' -e '$p' emp.lstDelete lines Delete second line alone
sed '2d' myfileDelete a range of lines: from the 2nd through the 3rd
sed '2,3d' myfileDelete a range of lines, from the first occurrence of 'second' to the line with the first occurrence of 'fourth'
sed '/second/,/fourth/d' myfilePrint all of a file except for specific lines Suppress any line with 'test' in it
sed '/test/d' myfileSuppress from the 3rd line to EOF
sed '3,$d' myfileReplace text
Replace the first instance of the | character with : and display the first two lines [YUG:455]
sed 's/|/:/ emp.lst | head -2Replace all instances of the | character with :, displaying the first two lines [YUG:455]
sed 's/|/:/g' emp.lst | head -2Substitute HTML tags:
sed 's/<I>/<EM>/g'These commands will replace "director" with "executive director"
sed 's/director/executive director/' emp.lstsed 's/director/executive &/' emp.lstsed '/director/s//executive &/' emp.lst[YUG: 456-457]
Searching for text
Equivalent to grep MA *
sed -n '/MA/p' *Stringing sed statements together with pipe Take lines beginning with "fake" and remove all instances of "fake.", piping them... remove all parentheses with content and count lines of output (results)
sed -n '/^fake/s/fake\.//p' * | sed -nr 's/\(.*\)//p' | wc -lTake lines of all files in CWD beginning with "fake" and remove all instances of string "fake." Then remove all parentheses with any content within them and print only the top 10 lines
sed -ne '/^fake/p' * | sed -n 's/fake\.//p' | sed -nr 's/\(.*\)//p' | sed 11qCount the number of pipes replaced by piping output to cmp, which will use the -l option to output byte numbers of differing values, then counting the lines of output (YUG:456)
sed 's/|/:/g' emp.lst | cmp -l - emp.lst | wc -lSequence from 1 to 15
seq -f "%03g" 15Sequence from 5 to 99, separated by a space instead of a newline
seq -s " " 5 99Sequence every third number from 5 to 20
seq 5 320Sequence from 1 to 8
seq 8Print random selection of integers from {x} to {y} (inclusive) without replacement
shuf -i x-yPrint random selection of integers from {x} to {y} (inclusive), with replacement
shuf -i x-y -rShuffle items separated by newline in file cards.txt, displaying only one 10
shuf -n 1 items.txtSort by space-delimited columns. Processes consuming the most memory will be at the bottom 23
ps aux | sort -nk 4Processes consuming the most CPU will be at the bottom
ps aux | sort -nk 3Output last lines beginning at 30th line from the start
tail -n=+30
tail --lines=+30Change the case of a string [23]
tr [:upper:] [:lower:]Remove a character or set of characters from a string or line of output
tr -d "text"