Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

sed read contents of file and print value another file

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago   -   Original Discussion by jimmyf
jimmyf's Unix or Linux Image
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 18 May 2018, 12:29 PM EDT
Posts: 112
Thanks: 57
Thanked 2 Times in 1 Post
sed read contents of file and print value another file

Trying to use sed to insert the contents of a file into the end of each line in another file
file1


Code:
This is a line
Here is another line 
This is yet another line
Here is a fourth line

file2


Code:
TEXT

desired output


Code:
This is a line TEXT
Here is another line TEXT
This is yet another line TEXT
Here is a fourth line

This is what I'm trying with sed, thinking the dollar sign would put the text at the end of each line but it isn't. Any suggestions would be appreciated





Code:
sed '/$/ r file2' file1

Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago   -   Original Discussion by jimmyf
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 May 2018, 3:43 AM EDT
Location: Aachen, Germany
Posts: 12,756
Thanks: 417
Thanked 3,923 Times in 3,607 Posts
That's not how it works. It prints the line incl. a <new line> char, and then reads and prints the text, regardless of the $ regex.
How about


Code:
sed '1 {x; d; n}; G; s/\n/ / ' file2 file1
This is a line TEXT
Here is another line  TEXT
This is yet another line TEXT
Here is a fourth line TEXT

The Following User Says Thank You to RudiC For This Useful Post:
jimmyf (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago   -   Original Discussion by jimmyf
jimmyf's Unix or Linux Image
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 18 May 2018, 12:29 PM EDT
Posts: 112
Thanks: 57
Thanked 2 Times in 1 Post
hm command garbled. I'm on Solarice, wonder if there is something about that.
    #4  
Old Unix and Linux 1 Week Ago   -   Original Discussion by jimmyf
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 May 2018, 3:43 AM EDT
Location: Aachen, Germany
Posts: 12,756
Thanks: 417
Thanked 3,923 Times in 3,607 Posts
Please show the error message in its entirety, char by char.
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago   -   Original Discussion by jimmyf
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 28 May 2018, 3:06 AM EDT
Location: Simplicity
Posts: 4,087
Thanks: 351
Thanked 1,381 Times in 1,245 Posts
If file1 is only one line then one read command puts it into a shell variable.


Code:
read text < file1

Then a while read loop or sed can do add the text variable.


Code:
while IFS= read -r line
do
  printf "%s %s\n" "$line" "$text"
done < file2 > file3

Here the output is redirected to a new file.
Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago   -   Original Discussion by jimmyf
jimmyf's Unix or Linux Image
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 18 May 2018, 12:29 PM EDT
Posts: 112
Thanks: 57
Thanked 2 Times in 1 Post
hey RudiC


Code:
> cat file1
This is a line
Here is another line
This is yet another line
Here is a fourth line


> cat file2
TEST


> sed '1 {x; d; n}; G; s/\n/ /' file2 file1
sed: command garbled: 1 {x; d; n}; G; s/\n/ /

Sponsored Links
    #7  
Old Unix and Linux 1 Week Ago   -   Original Discussion by jimmyf
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 May 2018, 3:43 AM EDT
Location: Aachen, Germany
Posts: 12,756
Thanks: 417
Thanked 3,923 Times in 3,607 Posts
I'm not at home on solaris; does this help:



Code:
sed '1 {x; d; n; }; G; s/\
/ /;' file2 file1

The Following User Says Thank You to RudiC For This Useful Post:
jimmyf (1 Week Ago)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace partial contents of file with contents read from other file seeki Shell Programming and Scripting 2 03-12-2012 06:54 AM
script to read the contents of a file and print praveen1516 Shell Programming and Scripting 2 01-05-2011 01:28 PM
Read contents of the file and print sandy1028 Shell Programming and Scripting 4 06-11-2010 07:49 AM
read file-print lines in sed bigben1220 Shell Programming and Scripting 2 04-20-2010 10:56 AM
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 10:17 PM



All times are GMT -4. The time now is 04:23 AM.