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
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..
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
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.
Hi Elixir,
you always come with some master piece..
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.
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...
(tried adding blank line at the end also)
Don't know what the issue was..
Now working fine...:)(after 5-6 attempts:D)
Hmmm...I'll check out this.
I've changed the script now.
Now that's working like charm...
good one..
Thanks elixir.It works fine and very useful