A bit about

Hello, everyone! All you can see below is just my bank of information. Some material I've found in the fathomless net, some I've learned myself. Don't think all of the information here is right or actual, but may be it could be of use for you :) All feedback is welcome, especially constructive ones :)

Thursday, March 26, 2009

Shell: Reading a file line-by-line as is

I faced this problem when I had to copy one file to another, but change or skip some lines. It's mandatory that all spaces of the copied lines were left unchanged. First of all I've tried this script:
cat "$original_file" | while read str; do
{some analysis of str variable}
echo "$str" >> "$new_file";
done

But it cut all leading and trailing spaces in the new line (it's a feature of "read" tool). Fortunately, it provides $REPLY variable if run without arguments. In this case $REPLY contains the line as is, with all spaces. So the working code was:
cat "$original_file" | while read; do
str="$REPLY";
{some analysis of str variable}
echo "$str" >> "$new_file";
done

0 comments:

Post a Comment