Concatenate strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate strings
# 8  
Old 07-24-2014
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

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

# 9  
Old 07-24-2014
Would you not be better/simpler/more efficient as:-
Code:
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
This User Gave Thanks to rbatte1 For This Post:
# 10  
Old 07-30-2014
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
Code:
@example.com
@example.com
@example.com

The file f1.txt contains
Code:
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.

Last edited by Don Cragun; 07-30-2014 at 04:38 AM.. Reason: Change ICODE tags to CODE tags for multi-line data.
# 11  
Old 07-30-2014
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:
Code:
od -c /home/linux1/xxxxxxx/f1.txt

Assuming my guess is correct, try changing the script to:
Code:
tr -d '\r' < /home/linux1/xxxxxxx/f1.txt | while read line
do	echo "${line}@example.com" 
done >> /home/linux1/xxxxxx/f2.txt

This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 08-02-2014
here is the o/p of
Code:
od -c /home/linux1/xxxxxxx/f1.txt

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

i'll try with the 'tr' option with while loop and post result shortly.
# 13  
Old 08-02-2014
Actually, your f1.txt's od -cshould be srh like
Code:
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...
# 14  
Old 08-06-2014
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:
Code:
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
Code:
^M

for example:
Code:
DB34^M@example.com

but some others do not have it and are correctly appended like this:
Code:
DB50@example.com


Last edited by sam_bd; 08-06-2014 at 11:24 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Concatenate strings in a a for loop

hi guys, I have this question. I am creating an script to that read a text file(.ini) with the list of the patterns to find for example: EPMS_VO EMPS_PARTS Then it check if file have been delivered in a folder and process it with each pattern, but I am having problems concatenting the... (7 Replies)
Discussion started by: Danman
7 Replies

2. Programming

Concatenate two strings

Hello! Can anyone explain line 28 for me? I was thinking *a would be replaced by *b, but it actually appends *a to *b. I know it is related to pointer address, but could not figure it out by myself. Thanks a lot! 1 //Concatenate two strings 2 3 #include<stdio.h> 4 char *stradd (char *,... (5 Replies)
Discussion started by: yifangt
5 Replies

3. Shell Programming and Scripting

Concatenate text between patterns in individual strings

In any given file, wherever a certain data block exists I need to concatenate the values(text after each "=" sign) from that block. in that block. The block starts and ends with specific pattern, say BEGIN DS and END DS respectively. The block size may vary. A file will have multiple such blocks.... (12 Replies)
Discussion started by: Prev
12 Replies

4. Web Development

Concatenate Strings

hi..:) this is my sample part of my program.. $csv_output .= $row.",". $row.",". $row.",". $row.",". $row.",". ... (2 Replies)
Discussion started by: Jeneca
2 Replies

5. UNIX for Dummies Questions & Answers

concatenate strings

if i use echo "ravi" echo "sankar" it showing output ravi sankar but i want output as ravi sankar remember sankar should be in another echo statement only (2 Replies)
Discussion started by: shankr3
2 Replies

6. Shell Programming and Scripting

delete repeated strings (tags) in a line and concatenate corresponding words

Hello friends! Each line of my input file has this format: word<TAB>tag1<blankspace>lemma<TAB>tag2<blankspace>lemma ... <TAB>tag3<blankspace>lemma Of this file I need to eliminate all the repeated tags (of the same word) in a line, as in the example here below, but conserving both (all) the... (2 Replies)
Discussion started by: mjomba
2 Replies

7. UNIX for Dummies Questions & Answers

Concatenate Strings

Hi Friends, I have a requirement I need to concatenate the below two strings. String 1 = /@jobid_at_ String 2 = value stored in ORACLE_SID String 3 = string1 concatenated with String 2. Please let me know how should i do it in UNIX. Thanks, (2 Replies)
Discussion started by: diva_thilak
2 Replies

8. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question