0
>cat /tmp/list1
john
jack

>cat /tmp/list2
smith
taylor

It is guaranteed that list1 and list2 will have equal number of lines.

    f(){
        i=1
        while read line
          do
        var1 = `sed -n '$ip' /tmp/list1`
        var2 = `sed -n '$ip' /tmp/list2`
        echo $i,$var1,$var2
        i=`expr $i+1`
            echo $i,$var1,$var2
          done < $INFILE
    }

So output of f() should be:

1,john,smith
2,jack,taylor

But getting

1,p,p
1+1,p,p

If i replace following:

var1 = `sed -n '$ip' /tmp/list1`
var2 = `sed -n '$ip' /tmp/list2` 

with this:

var1=`head -$i /tmp/vip_list|tail -1`
var2=`head -$i /tmp/lb_list|tail -1`

Then output:

1,john,smith
1,john,smith
1
  • 1
    Most shells of the Borne family also accept the notation i=$(( $i + 1 )) for calculations and $(command) for execution. I find this much nicer... besides you can nest them in ways the execution quotes do not allow or makes things clearer to most readers. x="$( foo "$bar" $(( $i + 1 )) )"; Commented Sep 16, 2012 at 15:50

2 Answers 2

2

If you can use paste and awk command, you can achieve the same with a one-liner:

paste -d, /tmp/list1 /tmp/list2 | awk '{print NR "," $0}'

Replace the while script with this line :)

Sign up to request clarification or add additional context in comments.

Comments

2

the $ip is the problem there making ip the name of the variable, you should use ${i}p instead letting the shell know that the variable is i not ip, your code should look like

var1=`sed -n "${i}p" /tmp/list1`

var2=`sed -n "${i}p" /tmp/list2`

2 Comments

How is this supposed to work with the spaces around the = and the substitution-inhibiting single quotes around $i?
edited with double quotes and no spaces, I'm new here and find it hard to type right in this thing, first 3 tries everything was in a single line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.