Sort, Cut, Sed options Flashcards
(17 cards)
What does sort -n do?
- 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 does sort -r change the output?
- 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
What is the purpose of sort -u?
- Removes duplicate lines while sorting (unique sort)
- Combines sorting and deduplication in one operation
More efficient than using sort | uniq separately
When would you use sort -k?
- 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
What does sort -t accomplish?
- 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 does sort -f affect sorting?
- Performs case-insensitive sorting (fold case)
- Treats uppercase and lowercase letters as equivalent
Prevents sorting issues where uppercase letters come before lowercase
What does cut -d specify?
- 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 does cut -f work?
- 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)
What is cut -c used for?
- 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
When would you use cut -b?
- 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
What does cut –complement do?
- 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
What does sed ‘s/old/new/’ do?
- 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 does sed ‘s/old/new/g’ differ from sed ‘s/old/new/’?
- 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
What is sed -i used for?
- 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 does sed -n work with p command?
- 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
What does sed ‘/pattern/d’ accomplish?
- Deletes lines that match the specified pattern
Example: sed ‘/^#/d’ file.txt removes all lines starting with #
* Useful for removing comments or unwanted content
When would you use sed ‘s/old/new/I’?
- Case-insensitive substitution using the I flag
- Matches pattern regardless of upper/lowercase
Example: sed ‘s/error/ERROR/I’ matches ‘Error’, ‘ERROR’, ‘error’