|
Line filters read from stdin, transform data, and
write to stdout. The |
|
|
Output:
Basic line reading: Got: line1 Got: line2 Got: line3 |
|
|
Process a file line by line: |
|
|
Output:
Processing file: apple (red): 10 items banana (yellow): 20 items cherry (red): 15 items date (brown): 25 items |
|
|
|
|
|
Output:
Custom delimiter (CSV): alice is 30, works as engineer bob is 25, works as designer |
|
|
Read from command output: |
|
|
Output:
Reading from command: |
|
|
Filter pattern: transform each line |
|
|
Output:
Transform each line (uppercase): HELLO WORLD |
|
|
Numbering lines: |
|
|
Output:
Number lines: 1: first 2: second 3: third |
|
|
Skip header line: |
|
|
Output:
Skip header: name,age,city Alice (age 30) from NYC Bob (age 25) from LA Carol (age 35) from Chicago |
|
|
Filter out empty lines: |
|
|
Output:
Skip empty lines: line1 line2 line3 |
|
|
Filter with grep before processing: |
|
|
Output:
Red fruit: apple (10, red) Red fruit: cherry (15, red) |
|
|
Accumulate values using a subshell: |
|
|
Output:
Total: 60 |
|
|
Note: Variables set in piped while loops don’t persist outside due to subshell. Use a here-string or a different approach: |
|
|
Output:
Total: 60 |
|
|
Process with multiple passes using a subshell: |
|
|
Output:
apple 20 banana 40 cherry 30 |
|
|
Use |
|
|
Output:
Debug with tee: A B C a b c |
|
|
|
|
|
Output:
Using xargs: Processing: file1 Processing: file2 Processing: file3 |
|
|
|
|
|
Output:
Head and tail: 1 2 3 8 9 10 |
|
|
|
|
|
Output:
Unique lines:
a
b
c
Count occurrences:
2 a
1 b
3 c
|
|
|
Data can be sorted using the |
|
|
Output:
Sorted output: apple banana cherry |
|
|
|
|
|
Output:
Extract fields with cut: age |
|
|
Handle last line without newline: |
|
|
Output:
Handle missing final newline: [line1] [line2] |
|