command paste with variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting command paste with variables
# 8  
Old 03-27-2009
16298 mmap2(NULL, 83186, PROT_READ, MAP_PRIVATE, 3, 0 <unfinished ...> 16298 <... mmap2 resumed> ) = 0xb7f71000 \n
16298 close(3 <unfinished ...>16298 <... close resumed> ) = 0 \n
16299 access("/etc/ld.so.nohwcap", F_OK <unfinished ...> 16299 <... access resumed> ) = -1 ENOENT (No such file or directory) \n
# 9  
Old 03-27-2009
Try this script or change it however you want. It uses the coprocess feature of the shell...
Code:
#!/usr/bin/sh

var1="var1line1\n
var1line2\n
var1line3\n
var1line4"

var2="var2line1\n
var2line2\n
var2line3"

echo $var1 |&
exec 3>&p; exec 4<&p

echo $var2 |&
exec 5>&p; exec 6<&p

while true
do
  read -u4 v1
  read -u6 v2

  if [ -z "$v1" -a -z "$v2" ]; then
     break
  else
     if [ -z "$v1" ]; then
        echo "$v2"
     elif [ -z "$v2" ]; then
        echo "$v1"
     else
        echo "$v1 $v2"
     fi
  fi
done

# 10  
Old 03-27-2009
Quote:
Originally Posted by samos
16298 mmap2(NULL, 83186, PROT_READ, MAP_PRIVATE, 3, 0 <unfinished ...> 16298 <... mmap2 resumed> ) = 0xb7f71000 \n
16298 close(3 <unfinished ...>16298 <... close resumed> ) = 0 \n
16299 access("/etc/ld.so.nohwcap", F_OK <unfinished ...> 16299 <... access resumed> ) = -1 ENOENT (No such file or directory) \n
Maybe what I'm asking seems a bit trivial, which of the following is the format of your data?

Code:
VARIABLE="line1
line2
line3
...

or

Code:
VARIABLE="line1\n
line2\n
line3\n
...

What I mean is that do you explicitly write at the end of each line \n or you just push the enter key in order to insert a new line?

Last edited by dariyoosh; 03-27-2009 at 04:26 PM..
# 11  
Old 03-27-2009
Quote:
Originally Posted by dariyoosh
Maybe what I'm asking seems a bit trivial, which of the following is the format of your data?
No I think that is a very good point...I had the same doubt and wanted to ask the OP about it but you beat me to it.
# 12  
Old 03-27-2009
As dariyoosh brought up a very valid point so I changed the script...
Code:
#!/usr/bin/sh

var1="var1line1
var1line2
var1line3
var1line4"

var2="var2line1
var2line2
var2line3"

echo "$var1" |&
exec 3>&p; exec 4<&p

echo "$var2" |&
exec 5>&p; exec 6<&p

while true
do
  read -u4 v1
  read -u6 v2

  if [ -z "$v1" -a -z "$v2" ]; then
     break
  else
     if [ -z "$v1" ]; then
        echo "$v2"
     elif [ -z "$v2" ]; then
        echo "$v1"
     else
        echo "$v1 $v2"
     fi
  fi
done

# 13  
Old 03-27-2009
Assuming that you don't explicitly write \n at the end of each line, I think the following KornShell script does the job.

Code:
#!/bin/ksh

# The fact that we set IFS only on new line is that in this question
# space and tabs are not considered as delimiters, only new lines
IFS='
'

FINAL_RESULT=""
RESULT=""

function concatenate
{
    VARIABLE1=$1
    VARIABLE2=$2
    
        
    CURRENT_LINE_NUMBER_IN_VARIABLE1=1
    CURRENT_LINE_NUMBER_IN_VARIABLE2=1
    
    for ITERATOR1 in $VARIABLE2
    do
        CURRENT_LINE_NUMBER_IN_VARIABLE1=1
        for ITERATOR2 in $VARIABLE1
        do
            if (( CURRENT_LINE_NUMBER_IN_VARIABLE1 < 
                 CURRENT_LINE_NUMBER_IN_VARIABLE2 ))
            then
                (( CURRENT_LINE_NUMBER_IN_VARIABLE1 = 
                      CURRENT_LINE_NUMBER_IN_VARIABLE1 + 1 ))
                
            elif (( CURRENT_LINE_NUMBER_IN_VARIABLE1 == 
                       CURRENT_LINE_NUMBER_IN_VARIABLE2 ))
            then
                FINAL_RESULT="$FINAL_RESULT\n$ITERATOR2$ITERATOR1"
                (( CURRENT_LINE_NUMBER_IN_VARIABLE1 = 
                      CURRENT_LINE_NUMBER_IN_VARIABLE1 + 1 ))
                break;
                      
            else
                break 2;
            fi
        done
    
    (( CURRENT_LINE_NUMBER_IN_VARIABLE2 = 
            CURRENT_LINE_NUMBER_IN_VARIABLE2 + 1 ))
    done
    
    
    CURRENT_LINE=1
    for ITERATOR1 in $VARIABLE1
    do
        # The following condition ignores the lines in VARIABLE1
        # that were read in the precedent loop
        if (( CURRENT_LINE < CURRENT_LINE_NUMBER_IN_VARIABLE1 ))
        then
            (( CURRENT_LINE = CURRENT_LINE + 1 ))
            
        # Here we concatenate the rest of the lines in VARIABLE1 with
        # the final result.
        else
            FINAL_RESULT="$FINAL_RESULT\n$ITERATOR1"
            (( CURRENT_LINE = CURRENT_LINE + 1 ))
            (( CURRENT_LINE_NUMBER_IN_VARIABLE1 = 
                         CURRENT_LINE_NUMBER_IN_VARIABLE1 + 1 ))
        fi
    done

(( TOTAL_LENGTH_MINUS_FIRST_NEW_LINE = ${#FINAL_RESULT} - 2 ))
FINAL_RESULT=${FINAL_RESULT:2:$TOTAL_LENGTH_MINUS_FIRST_NEW_LINE}
}

# In this example we suppose VARIABLE1 and VARIABLE2 are those lines
# that have to be concatenated, each line by its corresponding in
# another variable
VARIABLE1="var1 first line
var1 second line
var1 third line
var1 forth line
var1 fifth line
var1 sixth line
var1 seventh line"

VARIABLE2="var2 first line
var2 second line
var2 third line
var2 forth line
var2 fifth line"


# As you didn't specify in your problem whether both variables have
# the same number of lines, I considered also the case in which the
# number of lines in each variable is different


NUMBER_OF_NEW_LINES_IN_VARIABLE1=0
NUMBER_OF_NEW_LINES_IN_VARIABLE2=0

for ITERATOR in $VARIABLE1
do
    (( NUMBER_OF_NEW_LINES_IN_VARIABLE1 =
          NUMBER_OF_NEW_LINES_IN_VARIABLE1 + 1 ))
done


for ITERATOR in $VARIABLE2
do
    (( NUMBER_OF_NEW_LINES_IN_VARIABLE2 =
          NUMBER_OF_NEW_LINES_IN_VARIABLE2 + 1 ))
done




if (( $NUMBER_OF_NEW_LINES_IN_VARIABLE1 >=
               $NUMBER_OF_NEW_LINES_IN_VARIABLE2 ))
then
    concatenate "$VARIABLE1" "$VARIABLE2"
else
    concatenate "$VARIABLE2" "$VARIABLE1"
fi


print $FINAL_RESULT

# 14  
Old 03-27-2009
yes both files have the sime number of lines. and \n is not written in variables...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with paste command

Hi, I am facing issue with paste command. It is adding spaces or tab in between. I have say 3 files with below data File_1 TH THI THIS I File_2 IS IS S IS RE S File_3 RECORD 1 CORD 2 IS RECORD 3 (3 Replies)
Discussion started by: Simanto
3 Replies

2. Shell Programming and Scripting

Problems with awk (fatal error) and paste (two variables into one column-by-column)

Hello, I have a script extracting columns of useful numbers from a data file, and manipulating the numbers with awk commands. I have problems with my script... 1. There are two lines assigning numbers to $BaseForAveraging. If I use the commented line (the first one) and let the second one... (9 Replies)
Discussion started by: vgbraymond
9 Replies

3. Shell Programming and Scripting

Need help with paste command using variables

How can I accomplish this? I basically want to merge two variables onto the same line. I can do it with two FILES this way: $ cat /tmp/users_in.list | awk -F "," '{print $2}' | cut -c -1 > first.initial $ awk -F "," '{print $1}' /tmp/users_in.list | awk '{print $1}' > last.name $ paste... (5 Replies)
Discussion started by: greenlightening
5 Replies

4. Shell Programming and Scripting

How do I use paste command in while condition??

there are lot of files where in mostly all the file contains 2columnsAl,1 Ail,13 Al,3 Al,1 Al,3 Al,2 Al,3 Al,1 Al,1 Al,1 My requirement is i wanted only the second column of every file as a column ony in the base file... I used paste command... but it is not helping as I'm using while... (7 Replies)
Discussion started by: nikhil jain
7 Replies

5. Shell Programming and Scripting

echo two variables like the paste command is not working

Dear all, I have two files like this file1 A B C D E F file2 1,2 3,4 5,6 I want this output output_expected A B 1,2 C D 3,4 E F 5,6 (3 Replies)
Discussion started by: valente
3 Replies

6. Shell Programming and Scripting

Can't paste in command line.

Hello. I've made a simple script which asks the user to input a hash and then runs a command that replaces the variable $hash with what the user inserted. The ting is that when the programm asks for input I can't paste anything there..! any clues?? :wall: (8 Replies)
Discussion started by: louboulos
8 Replies

7. Shell Programming and Scripting

Bash-how to properly READ and PASTE variables.

Recently i made a script for a project at molecular dynamics but am stuck at the last step.The thing i want to do is to ask the user to input the number of particles, then replace the bolded numbers at lines 9 and 17.. code #!/bin/bash #read number of particles echo "insert the number of... (2 Replies)
Discussion started by: arisinhell
2 Replies

8. UNIX for Dummies Questions & Answers

Need help with using cut and paste command

I have a file which contains 3 fields separated by tabs example andrew kid baker I need to swap kid and baker using cut and paste commands how is this to be done? Thanks (1 Reply)
Discussion started by: drew211
1 Replies

9. UNIX for Dummies Questions & Answers

paste command

input1 15 150 input2 x 10 100 input3 y 20 200 z 34 44 cmd paste -d "\t" input1 input2 input3 >>output output (1 Reply)
Discussion started by: repinementer
1 Replies

10. UNIX for Advanced & Expert Users

paste command

I wonder if any body can help me with a command i am struggling with. I have a file with around 400 lines in, in a program i have it pulls out each line at a time so that data from the line can be cross referenced with another file. If it finds a match it pulls out a ocde from the second file, this... (5 Replies)
Discussion started by: mariner
5 Replies
Login or Register to Ask a Question