Text processing Flashcards
What is the ‘cut’ command used for?
Used for extracting text by splitting lines on delimiters/byte positions/character patterns
How can you get the nth character from each line with ‘cut’?
cut -c<n> <filename></filename></n>
How can you get the multiple characters from each line with ‘cut’?
cut -c1,3,4 <filename> (returns 1st 3rd and 4th characters)</filename>
How do you get a range of characters from each line using ‘cut’?
cut -c1-3,6-10 <filename></filename>
How do you get a range of bytes from each line using ‘cut’?
cut -b1-8 <filename> (first 8 bytes)</filename>
How can you split lines into columns on a colon (:) delimiter and select the 5th column using ‘cut’?
cut -d: -f 5 <filename></filename>
How can you use ‘cut’ on the output of another command?
<command></command>
[OPTIONS] | cut [OPTIONS] <filename></filename>
What is the ‘awk’ command used for?
Text processing utility/language used to extract text from files or command output
How do you out space separated columns lines of text using ‘awk’?
awk ‘{print $1}’ <filename> ($n is the column number)</filename>
How can you get multiple columns using ‘awk’?
awk ‘{print $1,$2,$3}’ <filename></filename>
How can you get the last column using ‘awk’?
awk ‘{print $NF}’ <filename></filename>
How can you search for text using ‘awk’?
awk ‘/<text_search>/ {print}' <filename></filename></text_search>
How can you split text on a delimiter using ‘awk’?
awk -F<delimiter> '{print $1}' <filename></filename></delimiter>
How can you replace text using ‘awk’?
echo “One” | awk ‘{$1=”Two”; print $0}’ (replaces One with Two)
How can you get all file lines longer than n bytes with ‘awk’?
cat <filename> | awk 'length($0) > <n>'</n></filename>