Sort, Cut, Sed options Flashcards

(17 cards)

1
Q

What does sort -n do?

A
  • Performs numeric sort instead of alphabetical sort
  • Treats numbers as actual values rather than strings

Example: sort -n numbers.txt sorts 10 before 2 (unlike default alphabetical)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does sort -r change the output?

A
  • Reverses the sort order (descending instead of ascending)
  • Can be combined with other options like sort -nr for reverse numeric sort

Useful for finding largest values or reverse alphabetical order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of sort -u?

A
  • Removes duplicate lines while sorting (unique sort)
  • Combines sorting and deduplication in one operation

More efficient than using sort | uniq separately

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When would you use sort -k?

A
  • Sorts by a specific field/column number
  • Example: sort -k2 file.txt sorts by the second field
  • Essential for sorting structured data like CSV files
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does sort -t accomplish?

A
  • Specifies field delimiter/separator character
  • Default is whitespace, but -t allows custom delimiters

Example: sort -t: -k3 /etc/passwd sorts by third field using colon delimiter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does sort -f affect sorting?

A
  • Performs case-insensitive sorting (fold case)
  • Treats uppercase and lowercase letters as equivalent

Prevents sorting issues where uppercase letters come before lowercase

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does cut -d specify?

A
  • Sets the delimiter character for field separation
  • Default delimiter is tab, -d allows custom delimiters

Example: cut -d: -f1 /etc/passwd uses colon as delimiter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does cut -f work?

A
  • Selects specific fields/columns by number
  • Can specify ranges like -f1-3 or multiple fields like -f1,3,5

Must be used with delimited data (requires -d for custom delimiters)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is cut -c used for?

A
  • Cuts by character positions instead of fields

Example: cut -c1-10 file.txt extracts first 10 characters of each line
* Useful for fixed-width data or extracting specific character ranges

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When would you use cut -b?

A
  • Cuts by byte positions (important for multi-byte characters)
  • Similar to -c but handles UTF-8 and other multi-byte encodings correctly

Essential for international text processing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does cut –complement do?

A
  • Inverts the selection - shows everything except specified fields/characters

Example: cut -d: –complement -f3 shows all fields except the third
* Opposite of normal cut behavior

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does sed ‘s/old/new/’ do?

A
  • Substitutes first occurrence of ‘old’ with ‘new’ on each line
  • Basic find-and-replace operation in sed

Only replaces first match per line unless global flag is used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does sed ‘s/old/new/g’ differ from sed ‘s/old/new/’?

A
  • Global flag (g) replaces all occurrences on each line, not just the first
  • Without g, only first match per line is replaced

Essential for complete text replacement operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is sed -i used for?

A
  • In-place editing - modifies the original file directly
  • Without -i, sed outputs to stdout and leaves original unchanged

Dangerous option - always backup files before using sed -i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does sed -n work with p command?

A
  • Suppresses automatic printing of lines (-n) and p prints specific lines

Example: sed -n ‘5p’ file.txt prints only line 5
* Commonly used for extracting specific lines from files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does sed ‘/pattern/d’ accomplish?

A
  • Deletes lines that match the specified pattern

Example: sed ‘/^#/d’ file.txt removes all lines starting with #
* Useful for removing comments or unwanted content

17
Q

When would you use sed ‘s/old/new/I’?

A
  • Case-insensitive substitution using the I flag
  • Matches pattern regardless of upper/lowercase

Example: sed ‘s/error/ERROR/I’ matches ‘Error’, ‘ERROR’, ‘error’