Concatenate strings

Hi,I'm trying to concatenate @example.com to each line of a file f1.txt. and push it into f2.txt. Here is the code i'm using.

for i in `cat /home/linux1/xxxxxxx/f1.txt`;
do
echo ${i}@example.com > /home/linux1/xxxxxx/f2.txt;
done

But above code only printing @example.com in f2.txt. what i need is for each line in f1.txt should be concatenated with @example.com.
Desired ouput:

a@example.com
b@example.com
c@example.com

Requesting help.

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
1 Like

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

You need to use while loop exactly as I have shown. The i variable is equal to line variable in my code. This contains each line of the file.

If you are still confused,

while read i
do
 echo "${i}@example.com" >> /home/linux1/xxxxxx/f2.txt
done < /home/linux1/xxxxxxx/f1.txt

Does that make sense?

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

Can you post the updated script?

Hi CLX, here is the script.

#!/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

I am clueless. If possible, can you please post the input file?

However, You haven't quoted the echo'ed line
Also make sure you are reading from a proper unix format file (line endings \n)

Another try could be

while IFS= read i
do
 echo "${i}@example.com" >> /home/linux1/xxxxxxx/f2.txt
done < /home/linux1/xxxxxxx/f1.txt

Would you not be better/simpler/more efficient as:-

while read line
do
 echo "${line}@example.com" 
done < /home/linux1/xxxxxxx/f1.txt >> /home/linux1/xxxxxx/f2.txt

This way, the output file just gets opened once rather than for every record in the input file.

Robin

1 Like

Hi CLX, rbatte1, thank you for your responses. I tried with the suggested code and didn't get expected result DB1@example.com . Just got

@example.com
@example.com
@example.com

The file f1.txt contains

DB1
DB2
DB3

File f2.txt is the target file where i should get the ouput like this for each line of f1.txt; 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
1 Like

here is the o/p of

od -c /home/linux1/xxxxxxx/f1.txt
[linux1@xxxxxxx]$ od -c /home/linux1/xxxxxxx/f1.txt
0000000

i'll try with the 'tr' option with while loop and post result shortly.

Actually, your f1.txt's od -c should be srh like

0000000   D   B   1  \n   D   B   2  \n   D   B   3  \n

. Looks like your file is empty; no surprise nothing is prefixed to the other file...

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:

DB50@example.com

<^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>.

Thanks for the response RudiC. Is there any other option with

tr -d '\r'

which could avoid carriage return problem?

---------- Post updated at 09:50 PM ---------- Previous update was at 09:47 PM ----------

And i didn't understand why did the linux system which i logged on remotely using putty, consider f1.txt which is inputfile in DOS format?

Use *nix compatible editors in the first place. If not possible, convert file with e.g. dos2unix , or recode / iconv . awk also can remove the \r.

Hi RudiC, i tried with dos2unix command which is available in my linux server. but didn't get expected result in the f2.txt o/p file

[linux1@XXXXXXX yyyyyy]$ dos2unix f1.txt
dos2unix: converting file f1.txt to UNIX format ...

Saying "didn't get the expected result" gives us zero useful information.

What did you get?
What did you expect?
Show us the output of the commands:

od -c your_current_script
           and
od -c /home/linux1/xxxxxxx/f1.txt

and then run your_current_script and show us any output it printed and the output of the command:

od -c /home/linux1/xxxxxxx/f2.txt