Home Man
Search
Today's Posts
Register

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

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 05-17-2018
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

# 2  
Old 05-18-2018
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 (05-18-2018)
# 3  
Old 05-18-2018
hm command garbled. I'm on Solarice, wonder if there is something about that.
# 4  
Old 05-18-2018
Please show the error message in its entirety, char by char.
# 5  
Old 05-18-2018
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.
# 6  
Old 05-18-2018
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/ /

# 7  
Old 05-18-2018
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 (05-18-2018)
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to print contents of file when the file path is in a variable? wolfv Shell Programming and Scripting 2 02-15-2018 01:44 PM
How to read contents in each file and rename the file? nanduedi Shell Programming and Scripting 3 12-08-2014 08:22 AM
Run a program-print parameters to output file-replace op file contents with max 4th col jacobs.smith Shell Programming and Scripting 7 01-24-2013 01:08 PM
Replace partial contents of file with contents read from other file seeki Shell Programming and Scripting 2 03-12-2012 06:54 AM
read contents of a file using AWK RDR Shell Programming and Scripting 6 02-27-2012 01:30 AM
how to read contents of file? nishrestha Shell Programming and Scripting 9 10-23-2011 07:19 AM
How to search two strings in a file and print the contents in between to a file webbi UNIX for Dummies Questions & Answers 4 08-03-2011 01:03 PM
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
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 02:53 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password