how to append multiple lines to the last line of a file

Hello,

This is what I am trying to achieve:

 
file1
 
a
b
c
d
 
file2
 
e
f
g
h
i
j
k
i
m
 
output
 
a
b
c
d==e==f==g==h==i==j==k==i==m

Just like my last post if you read it, I am doing this using foreach but when the numbers go up it gets really slow. is there a way to achieve this using awk or sed that would be quick when used in a script?

Thanks,

# sed "$ s/.*/&$( awk '{printf ("=%s", $0)} END {printf ("\n")}' 2.txt)/g" 1.txt
a
b
c
d=e=f=g=h=i=j=k=i=m
1 Like

Hi CarloM,

Already logged out of my solaris account, but it seems like its exactly what I need.
I will try it tomorrow and let you know.

Thanks

Hi CarolM,

I am using tcsh to write the script I believe your solution is for Bash, it does not work when I run it.

can you or anyone esle provide a tcsh awk or sed command which would realize this?

Thanks

Tested in tcsh...

awk 'NR==FNR{a[++j]=$0;next}{b=b"=="$0}END{for(i=1;i<j;i++){print a} printf("%s%s\n",a[j],b)}' file1.txt file2.txt

--ahamed

Thanks ahamed but the solution does a little bit more than what I ask for:

 
awk 'NR==FNR{a[++j]=$0;next}{b=b"=="$0}END{for(i=1;i<j;i++){print a} printf("%s%s\n",a[j],b)}' fil1.txt file2.txt
==a==b==c==d==e==f==g==h==i==j==k==l==m==n
 

I want lines from file2 to be added only to the last line of file1 and only the lines that are appended should have 2 equal signs before and 2 after.

your solution puts 2 equal signs even before and after the begining lines of file1 and changes all the lines in both files in to one line.

Its working for me though...

bt:/tmp# echo $0
tcsh
bt:/tmp# cat 1.txt
a
b
c
d
bt:/tmp# cat 2.txt
e
f
g
h
i
j
k
i
m
bt:/tmp# awk 'NR==FNR{a[++j]=$0;next}{b=b"=="$0}END{for(i=1;i<j;i++){print a} printf("%s%s\n",a[j],b)}' 1.txt 2.txt
a
b
c
d==e==f==g==h==i==j==k==i==m

--ahamed

1 Like

ok, it makes sence now, I am using Solaris and I had the same problem previously and it was suggested that I use nawk instead of awk.

using nawk your solutions works, thanks alot

$
$
$ cat file1
a
b
c
d
$
$ cat file2
e
f
g
h
i
j
k
i
m
$
$ perl -pne 'chomp; $_ = $ARGV eq "file2" ? "==$_" : $. > 1 ? "\n$_" : $_; END{print "\n"}' file1 file2
a
b
c
d==e==f==g==h==i==j==k==i==m
$
$

tyler_durden

1 Like