Edit

Bash: Read a file line by line

Postach.io published

You learn new things every day. Here’s a cool little script that I wrote up.

#!/bin/bash
# start the counter
n=0
# get the number of lines
# it will give an output with spaces
lines=$(wc -l < tweets.csv)
# remove those spaces
lines="$(echo -e "${lines}" | tr -d '[:space:]')"
# for ever line use sed to output into "new_file.csv"
# and increment our counter
while (("$n" < "$lines")); do
sed -n "${n}p" < tweets.csv >> new_file.csv
n=$(($n + 1))
done
%23%20Bash%3A%20Read%20a%20file%20line%20by%20line%20%0A@%28Postach.io%29%5Bpublished%5D%0A%0A%0AYou%20learn%20new%20things%20every%20day.%20Here%27s%20a%20cool%20little%20script%20that%20I%20wrote%20up.%20%0A%0A%60%60%60bash%0A%23%21/bin/bash%0A%0A%23%20start%20the%20counter%0An%3D0%0A%0A%23%20get%20the%20number%20of%20lines%0A%23%20it%20will%20give%20an%20output%20with%20spaces%0Alines%3D%24%28wc%20-l%20%3C%20tweets.csv%29%0A%23%20remove%20those%20spaces%0Alines%3D%22%24%28echo%20-e%20%22%24%7Blines%7D%22%20%7C%20tr%20-d%20%27%5B%3Aspace%3A%5D%27%29%22%0A%0A%23%20for%20ever%20line%20use%20sed%20to%20output%20into%20%22new_file.csv%22%0A%23%20and%20increment%20our%20counter%0Awhile%20%28%28%22%24n%22%20%3C%20%22%24lines%22%29%29%3B%20do%0A%20%20sed%20-n%20%22%24%7Bn%7Dp%22%20%3C%20tweets.csv%20%3E%3E%20new_file.csv%0A%20%20n%3D%24%28%28%24n%20+%201%29%29%0Adone%0A%60%60%60