String concatenation

Hi,
I have two files.

cat file.txt
a
b
c
d

cat file1.txt
j
k
l
m

I need the output as

a:j
b:k
c:l
d:m

I tried the script but not working

cat test1.sh
#!/bin/sh
for i in `cat file1` ; do
N=0
j=`cat file2`
N=$((N+1))
echo $i:$j >>test2
done

You don't need a script but a simple command ( paste ).
By the way, is it homework?

No Its not homework.I have the single file where I splitted into two files.For simplification I have given the above one.

cat tmp.txt
/sad/a
/sda/b
/sda/c
/sda/d
/sdb/l
/sdb/j
/sdb/k
/sdb/m

I need the output as below as single file.

/sad/a:/sdb/l
/sda/b:/sdb/j
/sda/c:/sdb/k
/sda/d:/sdb/m

awk '{a++;if(a <= 4){p[a]=$0}else{k++;print p[k]":"$0}}' file

This will work for total 8 lines of file mentioned above..:slight_smile:

Why do you simplify your problems? Just state them as they are.

awk '{a[NR]=$0}
END{h=(NR%2)?int(NR/2+1):(NR/2)
j=h+1
for(i=1;i<=h;i++)
 print a,a[j++]
}' OFS=: tmp.txt
1 Like

If i don't know the string length then what will be the script.

Have you checked my script?
It will handle what you require to be handled.

1 Like

Hi Elixir,

you always come with some master piece..:slight_smile:

Just one query. For odd numbers last line is not printing..

No issues in that case.

cat tmp.txt
/sad/a
/sda/b
/sda/c
/sda/d
/sdb/l
/sdb/j
/sdb/k
/sdb/m

awk '...' tmp.txt
/sad/a:/sdb/l
/sda/b:/sdb/j
/sda/c:/sdb/k
/sda/d:/sdb/m

cat tmp1.txt
/sad/a
/sda/b
/sda/c
/sda/d
/sdb/l
/sdb/j
/sdb/k

awk '...' tmp1.txt
/sad/a:/sdb/l
/sda/b:/sdb/j
/sda/c:/sdb/k
/sda/d:

The last line is not having a "partner" there. :slight_smile:
And it will also work for a one-line file, too.

I have tried for 3-4 times. For 9 line input. But not showed any output...:confused:
(tried adding blank line at the end also)
Don't know what the issue was..
Now working fine...:)(after 5-6 attempts:D)

1 Like

Hmmm...I'll check out this.
I've changed the script now.

Now that's working like charm...:slight_smile:
good one..:slight_smile:

Thanks elixir.It works fine and very useful