|
|
|
Line filters read from stdin, transform data, and
write to stdout. The |
|
|
Basic while read loop: |
|
|
The -r flag prevents backslash interpretation. Always use quotes around $line to preserve whitespace. |
|
|
Process a file line by line: |
|
|
IFS controls field splitting: |
|
|
Read from command output: |
|
|
Filter pattern: transform each line |
|
|
Filter with awk (more efficient for simple transforms): |
|
|
Numbering lines: |
|
|
Skip header line: |
|
|
Filter out empty lines: |
|
|
Filter with grep before processing: |
|
|
Accumulate values: |
|
|
Note: Variables set in piped while loops don’t persist outside due to subshell. Use a different approach: |
|
|
Process with multiple passes: |
|
|
Tee for debugging pipelines: |
|
|
xargs as line processor: |
|
|
Parallel processing with xargs: |
|
|
head/tail as filters: |
|
|
uniq for deduplication (requires sorted input): |
|
|
sort as filter: |
|
|
cut for field extraction: |
|
|
Handle last line without newline: |
|
|
Practical example: Log analyzer |
|
|
Cleanup |
|