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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to append multiple lines to the last line of a file
# 1  
Old 11-04-2011
how to append multiple lines to the last line of a file

Hello,

This is what I am trying to achieve:

Code:
 
file1
 
a
b
c
d

Code:
 
file2
 
e
f
g
h
i
j
k
i
m

Code:
 
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,
# 2  
Old 11-04-2011
Code:
# 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

This User Gave Thanks to CarloM For This Post:
# 3  
Old 11-04-2011
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
# 4  
Old 11-06-2011
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
# 5  
Old 11-06-2011
Tested in tcsh...
Code:
awk 'NR==FNR{a[++j]=$0;next}{b=b"=="$0}END{for(i=1;i<j;i++){print a[i]} printf("%s%s\n",a[j],b)}' file1.txt file2.txt

--ahamed
# 6  
Old 11-06-2011
Thanks ahamed but the solution does a little bit more than what I ask for:

Code:
 
awk 'NR==FNR{a[++j]=$0;next}{b=b"=="$0}END{for(i=1;i<j;i++){print a[i]} 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.
# 7  
Old 11-06-2011
Its working for me though...
Code:
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[i]} 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
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append next line to previous lines when NF is less than 0

Hi All, This is very urgent, I've a data file with 1.7 millions rows in the file and the delimiter is cedilla and I need to format the data in such a way that if the NF in the next row is less than 1, it will append that value to previous line. Any help will be appricated. Thanks,... (17 Replies)
Discussion started by: cumeh1624
17 Replies

2. Shell Programming and Scripting

Append Multiple files with file name in the beginning of line

Hi, I have multiple files having many lines like as bvelow: file Name a.txt abc def def xyz 123 5678 file Name b.txt abc def def xyz 123 5678 I would like to append files in the below format to a new file: file Name c.txt (7 Replies)
Discussion started by: rramkrishnas
7 Replies

3. Shell Programming and Scripting

Replacing a single line with multiple lines in a file

Hi Am confused with the usage of "sed" command I want to replace a single line with multiple lines of a file.. eg., A file has Hi, How are you? I need to replace as Am fine What are You doing? I used the script as string1="Hi, How are you?" echo "$string1 is the value"... (4 Replies)
Discussion started by: Priya Amaresh
4 Replies

4. UNIX for Dummies Questions & Answers

append following lines to 1st line, every 3 lines

I have output like this: USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 ... where USER_ID is a unique user login followed by their login timestamp and... (6 Replies)
Discussion started by: MaindotC
6 Replies

5. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (1 Reply)
Discussion started by: erlanq
1 Replies

6. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (2 Replies)
Discussion started by: erlanq
2 Replies

7. Shell Programming and Scripting

help needed with shell script to append to the end of a specific line in a file on multiple servers

Hi Folks, I was given a task to append three IP's at the end of a specific (and unique) line within a file on multiple servers. I was not able to do that with the help of a script. All I could was: for i in server1 server2 server3 server4 do ssh $i done I know 'sed' could be used to... (5 Replies)
Discussion started by: momin
5 Replies

8. Shell Programming and Scripting

Append text to end of line on all lines

Hi, I've spent some time researching for this but can't seem to find a solution. I have a file like this 1234|Test|20101111|18:00|19:00There will be multiple lines in the file with the same kind of format. For every line I need to make it this 1234|Test|20101111|18:00|19:00||create... (5 Replies)
Discussion started by: giles.cardew
5 Replies

9. Shell Programming and Scripting

Find 5 lines and replace with 18 line in sql file where it contains multiple blocks.

My sql file xyz_abc.sql in this file there are multiple sql block in this block I need to find the following block rem Subset Rows (&&tempName.*) CREATE VIEW &&tempName.* AS SELECT * FROM &&tempName.* WHERE f is not null and replace with following code rem Subset Rows... (9 Replies)
Discussion started by: Zaheer.mic
9 Replies

10. Shell Programming and Scripting

Joining lines in reverse. append line 1 to line 2.

Hi I have used many times the various methods to append two lines together in a file. This time I want to append the 1st line to the second and repeat for the complete file.... an example This is the file owns the big brown dog joe owns the small black dog jim What I want is ... (7 Replies)
Discussion started by: dwalley
7 Replies
Login or Register to Ask a Question