Brainscape Linux Flashcards
how do you specify a delimiter, also, what is the default delimiter if not specified
-d, –delimiter (e.g. “cut -d ‘,’”) tab is default
what are the three ways you can specify what is to be sliced
-b, –bytes -c, –characters -f, –fields
how do you select the inverse the fields you specify (e.g. everything but)
–complement
how do you ignore any lines that don’t contain the delimiter character
-s, –only-delimited
how do you specify a different delimiter for the OUTPUT of what is cut
–output-delimiter=STRING
how do you specify the following: from N to end of line from beginning of line to M from N to M columns 2 through four and column 6
N- -M N-M 2-4,6
are ranges inclusive or exclusive
ranges are inclusive
what do fields begin counting from
fields begin at at “1” for cut
using sed, double space a file
sed G
using sed, double space a file that already has blank lines in it (the output should have no more than one blank line between lines of text)
sed ‘/^$/d;G’ (delete any lines that meet the address flag of being blank, and double space file)
using sed, undo double-spacing (assuming even-numbered lines are always blank)
sed ‘n;d’
using sed, convert Unix format (LF) to DOS format (CRLF)
sed ‘s/$/\r/’
using sed, delete leading whitespace (tabs, spaces) from beginning of each line
sed ‘s/^[ \t]*//’
using sed, delete trailing whitespace (tabs, spaces) from each end of line
sed ‘s/[ \t]*$//’
using sed, replace only the first instance in each line
sed ‘s/foo/bar/’
using sed, replace only the fourth instance in each line
sed ‘s/foo/bar/4’
using sed, replace all instances in a line
sed ‘s/foo/bar/g’
using sed, substitute “foo” with “bar” ONLY for lines which contain “baz”
sed ‘/baz/s/foo/bar/g’
using sed, substitute “foo” with “bar” EXCEPT for lines which contain “baz”
sed ‘/baz/!s/foo/bar/g’
using sed, change “scarlet” or “ruby” or “puce” to “red”
sed ‘s/scarlet|ruby|puce/red/g’
using sed, add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
sed ‘1~5G’
using sed, print the first 10 lines of file (emulate “head”), print the first line of the file
sed 10q (remember that “q” will print the current pattern space before quitting sed q)
using sed, print only lines which match regular expression (emulate “grep”)
sed -n ‘/regexp/p’ OR sed ‘/regexp/!d’
using sed, print only lines that do NOT match regexp (emulate “grep -v”)
sed -n ‘/regexp/!p’ OR sed ‘/regexp/d’
using sed, print all of file EXCEPT section between 2 regular expressions
sed ‘/Iowa/,/Montana/d’
using sed, delete lines matching pattern
sed ‘/pattern/d’
using sed, add a leading angle bracket and space to each line (quote a message)
sed ‘s/^/> /’
using sed, delete leading angle bracket & space from each line (unquote a message)
sed ‘s/^> //’
sed command: q
print current pattern space and quit
sed command: d
delete current pattern space and immediately start next cycle
sed command: p
print the current pattern space (usually used in conjunction with “-n” option)
sed command: n
print pattern space (or not, if auto-print is disabled), and replace the pattern space with the next line of input (mnemonic: “next”)
sed command: { and }
groups of commands can be enclosed between curly braces–this can be helpful when using addresses for multiple commands
sed command: “a text”
append “text” after a line (at the end of the cycle), ignoring leading whitespace (text to add is read to the end of the line)
sed command: “i text”
same as “a text”, but inserts text BEFORE the line
sed command: “c text”
change text (same as “a text”, except that this one deletes lines matching the address or address-range, and puts “text” in their place)
sed command: =
print the current line number, with a trailing newline
sed command: “r filename”
read filename and insert at end of cycle
sed command: “w filename”
write the pattern space to filename (file will be created (or overwritten) at the beginning of the script, and all “w” commands pointed to the same filename will be appended to the file throughout the script)
sed command: D
delete text in the pattern space up to the first newline, and start a new cycle without reading a new line of input (if only one line in pattern space, works just like lower-case “d”)
sed command: N
add a newline and append the next line of input to pattern space (no more input, exit without processing any more commands)
sed command: P
print pattern space up to the first newline
sed command: h
overwrite the hold space with the contents of the pattern space
sed command: H
append a newline to hold space and append current pattern space to hold space
sed command: g
write the hold space into pattern space (overwrites) (mnemonic: “Go to the pattern space”)
sed command: G
append newline to pattern space and add contents of hold space
sed command: x
swap contents of the hold and pattern spaces
sed command: “: label”
create a label for future branch commands
sed command: “b label”
branch unconditionally to label; if label is omitted, just start next cycle
sed command: “t label”
branch to label only if there has been a successful substitution since the last input line was read or conditional branch was taken (if label is omitted, just start the next cycle)
sed command: l (lower-case “L”)
unambiguously print the pattern space (useful for debugging)
sed command: s///g
search and replace, all matches in line
sed command: s///2
replace only the second occurrence in each line
sed command: s///p
print the pattern space if the substitution was made
sed command: s///w file.txt
write result to file if successful substitution was made (will append subsequent references to the same file)
sed command: s///i
make regexp case-insensitive (can be lowercase or uppercase “i”)
sed command: s///i2w file.txt
You can specify more than one flag (this one will be case-insensitive, sub the second match on a line, and write result to file).
sed addressing: delete line three
3d
sed addressing: delete last line in stream
$d
sed addressing: delete all lines that are NOT the last line
$!d
sed addressing: match every “stepth” line, starting with “first”
first~step
sed addressing: delete lines matching regexp, also, how to use a different character for regexp addresses
/regexp/d \%regexp%d
sed addressing: case-insensitive regexp
/regexp/I (must be uppercase)
sed addressing: multi-line mode regexp
/regexp/M (must be uppercase)
how to use the delimiter character in a regexp
escape it with a backslash
sed addressing: reuse the last-used regexp
use address of “//” (works both for addressing and for the “s” command!)
sed addressing: print lines four through six
sed -n ‘4,6p’
what will the following print and why: seq 10 | sed -n ‘4,/[0-9]/p’
It will print “4” and “5”. This is because regular expression matching begins only on the line after the first address.
sed addressing: match address and the “N” lines following the first address
address,+N e.g. ‘6,+2’ or ‘/hey/,+2’ (address can be either a number or a regular expression)
sed options: suppress automatic printing of pattern space
-n, –quiet, –silent
sed options: edit file in-place (and make backup if SUFFIX supplied)
-i [SUFFIX], –in-place[=SUFFIX]
sed options: use extended regular expressions instead of basic (ERE vs BRE)
-E, -r, –regexp-extended
sed options: add a short script of sed commands from the command line
-e (e.g. “sed -e ‘1d’ -e ‘2s/a/b/’”)
add the contents of a script file to the commands being executed
-f script-file, –file=script-file
what is the difference between BRE and ERE regular expressions
In the POSIX standard, Basic Regular Syntax (BRE) requires that the metacharacters ? + | ( ) and { } and | must be escaped to make them special (e.g. () and {}), whereas Extended Regular Syntax (ERE) does not.
how to simulate “-n” right from within a Sed script
first two characters in file “#n”
using sed, make to work on each file separately (rather than as one continuous stream of concatenated files)
specify the -s, –separate option
using sed, convert DOS format (CRLF) to Unix format (LF)
sed ‘s/\r$//’
create a gzip/bzip2/xz compressed archive
tar czvf/cjvf/cJvf compressed.tar.gz/bz2/xz directory
peek inside a gzip/bzip2/xz compressed archive
tar tzvf/tjvf/tJvf compressed.tar.gz/bz2/xz
extract a gzip/bzip2/xz compressed archive
tar xzvf/xjvf/xJvf compressed.tar.gz/bz2/xz
what are the options you can use for compression using tar (short and long)
z –gzip j –bzip2 J –xz
tar argument used to specify output archive file name
f FILE, –file=FILE
tar function to create a new archive
c, –create
tar option to be verbose
v, –verbose
tar function to list the contents of an archive
t, –list (mnemonic “Tell me what’s in this archive”)
tar function to extract files from an archive
x, –extract, –get
tar function to append other TAR files to an existing archive
A, –catenate, –concatenate
tar function to calculate differences between the archive and the file system
d, –diff, –compare
tar function to append files to the end of a tar archive
r, –append (mnemonic: “r” is at the end of the word “tar”, and this command will append a file to the end of the archive)
tar function to append files, but only those that are newer than the copy in the archive
u, –update
what is the difference between –append and –update in tar
–append adds the files no matter what, whereas –update only adds them if the files are newer than those in archive
tar option to infer type of compression to use based on the file extension you use
-a, –autocompress (or –no-auto-compress to explicitly turn off this behavior)
tar option to exclude certain files or folders from the backup
–exclude=PATTERN (patterns are case sensitive by default; use –ignore-case to change this)
tar option to exclude version control system directories
–exclude-vcs
tar option to explicitly not overwrite existing files when extracting
k, –keep-old-files
tar option to not overwrite any files that are newer than the archived copies (when extracting)
–keep-newer-files
tar option to display its default options
–show-default
tar option to exclude files and patterns LISTED IN A FILE instead of from command line
-X, –exclude-from=FILE
when listing or extracting with tar, list each directory that does not match search criteria
–show-omitted-dirs
how to replace a file in an archive with a different copy
delete the file first using “tar –delete –file=archive.tar file1”, then use append to add the new file (tar does not delete any files from archive automatically)
what is the basic tar syntax
tar {function} [options] [pathname …] (e.g. tar af archive.tar fileone.txt filetwo.txt), so one of the tar functions (e.g. –create, –update, –extract, etc.) has to be first in line for your arguments