You are not appending (>>) the file but overwriting (>) it every time. Possibly your input file contains blank line as last line or the last line contains spaces thats why its causing the variable of last iteration as null.
It is always safe to use while do .. done construct to read the file to deal with spaces and always use double quotes as a safer side.
while read line
do
echo "${line}@example.com" >> /home/linux1/xxxxxx/f2.txt
done < /home/linux1/xxxxxxx/f1.txt
hi Clx, thank you for ur response. I'll try with ur suggestion. however, while reading the file f1.txt (which is in column-format) i think i'll need to do line increment within while loop. Is the below code ok??
while read $i=`cat /home/linux1/xxxxxxx/f1.txt`
do
echo "${i}@example.com" >> /home/linux1/xxxxxx/f2.txt
$i=$i+1
done
Hi CLX, i tried with above code and got the error message +1: command not foundDB1 whr DB1 is the content in f1.txt. actully it should have shown like this DB1@example.com
#!/bin/sh
cd /home/linux1/xxxxxxx
while read i
do
echo "${i}@example.com >> /home/linux1/xxxxxxx/f2.txt
done < /home/linux1/xxxxxxx/f1.txt
exit 0
the previous error was due to $i=$i+1 . But i've removed it.
However i'm not getting the result in f2.txt. all the lines in f2.txt are filled by @example.com
instead of something like DB1@example.com
If you copied rbatte1's script verbatim, your input file must be in DOS format with <carriage-return><newline> line terminators instead of UNIX format <newline> line terminators. To verify, show us the output from the command:
od -c /home/linux1/xxxxxxx/f1.txt
Assuming my guess is correct, try changing the script to:
tr -d '\r' < /home/linux1/xxxxxxx/f1.txt | while read line
do echo "${line}@example.com"
done >> /home/linux1/xxxxxx/f2.txt
Hi Don, RudiC, sorry ws away due to bad health.
Yes, the problem seems to be due to file format of f1.txt. I tried with:
tr -d '\r' < /home/linux1/xxxxxxx/f1.txt | while read line
do
echo "${line}@example.com"
done >> /home/linux1/xxxxxx/f2.txt
And it WORKS!!! will test it thoroughly. Thank you Don.
But still i'm not understanding why file format of f1.txt is making the difference. Please help me understand.
This is the linux version i'm using: Linux 2.6.9-55.EL.
---------- Post updated at 08:07 PM ---------- Previous update was at 07:44 PM ----------
one more thing i'm noticing in f2.txt which is the output file...
some of the entries have
^M
for example:
DB34^M@example.com
but some others do not have it and are correctly appended like this:
<^M> is the carriage return (<CR>, 0x0D) char, and it does exactly that: it prints DB34 from your sample, then goes back to the first position in the line, and prints @example.com , overwriting the DB34.
This is frequently the problem when people work between windows and *nix environments; all DOS/windows systems use <CR><LF> line terminators while *nix use just <LF>.