The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need to concatenate two lines if the line doesnt end with quotes laxmi131 UNIX for Advanced & Expert Users 9 10-27-2008 07:22 AM
concatenate all duplicate line in a file. vaskarbasak Shell Programming and Scripting 30 08-29-2008 05:31 AM
concatenate and display 2 lines as 1 with a condition for 2 line ? vithala Shell Programming and Scripting 7 07-11-2008 01:01 AM
How to append some strings line by line? xinoo Shell Programming and Scripting 4 06-24-2008 02:04 AM
how to concatenate two command in one line and get the display in one screen vasikaran UNIX for Dummies Questions & Answers 9 07-01-2005 05:41 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-09-2008
hagdanan hagdanan is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 4
Unhappy 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 (permalink)  
Old 12-10-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Can you not have the contents of variable in files ?

If then, you can use
Code:
paste file1 file2
  #3 (permalink)  
Old 12-10-2008
hagdanan hagdanan is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 4
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 (permalink)  
Old 12-12-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by hagdanan View Post
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 (permalink)  
Old 12-12-2008
hagdanan hagdanan is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 4
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 (permalink)  
Old 12-12-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by hagdanan View Post
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 (permalink)  
Old 12-12-2008
hagdanan hagdanan is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 4
Thanks a lot sir.
using paste is much faster compared to the solution using awk.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0