Concatenate strings line by line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate strings line by line
# 1  
Old 12-09-2008
Data Concatenate strings line by line

Hi, I have a noob question . Can someone help me how to concatenate line by line using this variables?


var1:
Apple|
Banana|

var2:
Red
Yellow

then how can I concatenate both line by line? in which the result would be:
Apple|Red
Banana|Yellow

just to generate a row result i was using:

var1=$(
cat <<-__EOF__
APPLE
BANANA
__EOF__
)

var2=$(
cat <<-__EOF__
RED
YELLOW
__EOF__
)

I tried using unix paste but I do not know how to use it if it is a variable.


Thanks for your help.
# 2  
Old 12-10-2008
Can you not have the contents of variable in files ?

If then, you can use
Code:
paste file1 file2

This User Gave Thanks to vino For This Post:
# 3  
Old 12-10-2008
I cant because if another user is using the page. it may cause conflict.


This is the solution I used by the way in case someone might need it also.

allData=$(echo "$var1" | awk -v var2="$var2" '
{split(var2,arr1,"\n");print $0""arr1[NR]}
')

allData holds now the concatenated data line by line.
# 4  
Old 12-12-2008
Quote:
Originally Posted by hagdanan
Hi, I have a noob question . Can someone help me how to concatenate line by line using this variables?

var1:
Apple|
Banana|

var2:
Red
Yellow

then how can I concatenate both line by line? in which the result would be:
Apple|Red
Banana|Yellow
...
I tried using unix paste but I do not know how to use it if it is a variable.

With bash you can use process substitution:

Code:
var1="Apple|
Banana|"

var2="Red
Yellow"

paste -d '' <( echo "$var1" ) <( echo "$var2" )

:

# 5  
Old 12-12-2008
thanks a lot. I think I am going to use the process substitution.

would it be faster compared to the awk solution that I am using?
# 6  
Old 12-12-2008
Quote:
Originally Posted by hagdanan
thanks a lot. I think I am going to use the process substitution.

would it be faster compared to the awk solution that I am using?

Use the time command to see which is faster.
# 7  
Old 12-12-2008
Thanks a lot sir. Smilie
using paste is much faster compared to the solution using awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Help with concatenate multiple line into one line

Hi, Do anybody experience how to concatenate multiple line into one line by using awk or perl command? Input file: >set1 QAWEQRQ@EWQEASED ASDAEQW QAWEQRQTQ ASRFQWRGWQ From the above Input file, it got 5 lines Desired output file: >set1... (6 Replies)
Discussion started by: perl_beginner
6 Replies

3. Shell Programming and Scripting

Concatenate small line with next line perl script

Hello to all, I'm new to perl, I have input file that contains the string below: 315350535ff450000014534130101ff4500ff45453779ff450ff45545f01ff45ff453245341ff4500000545000This string has as line separator "ff45". So, I want to print each line but the code below is not working. perl -pe '... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

4. Shell Programming and Scripting

awk concatenate every line of a file in a single line

I have several hundreds of tiny files which need to be concatenated into one single line and all those in a single file. Some files have several blank lines. Tried to use this script but failed on it. awk 'END { print r } r && !/^/ { print FILENAME, r; r = "" }{ r = r ? r $0 : $0 }' *.txt... (8 Replies)
Discussion started by: sdf
8 Replies

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

6. Infrastructure Monitoring

How to concatenate this simple line in Unix?

Hi: I use the snmpget command everyday. And Im getting tire of writing the same line evertime I have to verify something. Example of the line: snmpget -c DreamTeam dal2-hr2 ifAlias.227 The .227 its the circuit interface and also its variable; could be any other number depending on the... (13 Replies)
Discussion started by: javygonx
13 Replies

7. Shell Programming and Scripting

Logfile - extracting certain lines to concatenate into 1 line

I've got a log file from automatic diagnostic runs. The log file is appended to each time an automatic log is run. I'd like to just pull certain lines from each run in the log file, and concatenate them into 1 comma delimited line (for export into excel or an html table). Each diagnostic run... (3 Replies)
Discussion started by: BecTech
3 Replies

8. Shell Programming and Scripting

concatenate all duplicate line in a file.

Hi All, i have a zip file like the format 794051400123|COM|24|0|BD|R|99.98 794051413727|COM|11|0|BD|R|28.99 794051415622|COM|23|0|BD|R|28.99 883929004676|COM|0|0|BD|R|28.99 794051400123|MOM|62|0|BD|R|99.98 794051413727|MOM|4|0|BD|R|28.99 794051415622|MOM|80|0|BD|R|28.99 ... (30 Replies)
Discussion started by: vaskarbasak
30 Replies

9. Shell Programming and Scripting

How to append some strings line by line?

I would like to append the numbers 1, 2, 3, 4 and so on to the lines of the file: Adam Wilkins | Colorado | 36 John Butler | Los Angeles | 47 Cassey Johnson | Minneapolis | 25 Albert Aniston | Miami | 19 .... Tony Legler | Sacramento | 55 Matt Simmons | New York | 38 Output would look... (4 Replies)
Discussion started by: xinoo
4 Replies

10. UNIX for Dummies Questions & Answers

how to concatenate two command in one line and get the display in one screen

Hi, I would like to know , how to concatenate two command in one line and get the display in one screen for eg command 1 : ls -l /data/logs command 2 : ls -l /data/errors output shd be /data/logs /data/errors xx-xx-xx-xx abc.log xx-xx-xx-xx... (9 Replies)
Discussion started by: vasikaran
9 Replies
Login or Register to Ask a Question