Sample Header Ad - 728x90

read single line from stdin and write to a file

0 votes
0 answers
472 views
Is there a way in bash to read a single line from stdin and write directly to a file? Something like: t="$(mktemp)" while true do [read single line from stdin] > "${t}" [nothing to read] && break printf '%s\n' "$t" t="$(mktemp)" done I have limited RAM available and expect lines that could be absurdly long. Otherwise I'd just do a while-read loop. Edit: I need to do some extractions and filtering. A more complete pseudo code-ish snippet would be something like: f () { t="$(mktemp)" while true do [read single line from stdin] > "${t}" [nothing to read] && break printf '%s\n' "$t" t="$(mktemp)" done | while read -r file do if cat "$file" | grep -q '[criteria]' then cat "$file" | jq '[filter1]' cat "$file" | jq '[filter2]' >> [seprate file] [etc] else printf '%s\n' "There was a mismatch" > /dev/stderr fi rm "$file" done }
Asked by fuumind (449 rep)
Jan 25, 2024, 02:28 PM
Last activity: Jan 25, 2024, 04:34 PM