sed read contents of file and print value another file

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed read contents of file and print value another file
# 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

This User Gave Thanks to RudiC For This Post:
# 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

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to Read the given file contents into a merged one file

Like to have shell script to Read the given file contents into a merged one file with header of path+file name followed by file contents into a single output file. While reading and merging the file contents into a single file, Like to keep the format of the source file. ... (4 Replies)
Discussion started by: Siva SQL
4 Replies

2. Shell Programming and Scripting

How to print contents of file when the file path is in a variable?

The file f1 contains the text "body" (shell prompt is "$"): $ cat ~/path/f1 body How to print contents of f1 when the f1 path is in a variable? Here is my failed attempt: $ f1="~/path/f1" $ echo $f1 ~/path/f1 $ cat $f1 cat: '~/path/f1': No such file or directory (2 Replies)
Discussion started by: wolfv
2 Replies

3. Shell Programming and Scripting

How to read contents in each file and rename the file?

Hello All, Can you help me in writing a script for reading the specific position data in a file and if that data found in that file that particular file should be renamed. Ex: Folder : C:\\test and Filename : CLSACK_112214.txt,CLSACK_112314.txt,CLSACK_112414.txt Contents in the file would... (3 Replies)
Discussion started by: nanduedi
3 Replies

4. Shell Programming and Scripting

Run a program-print parameters to output file-replace op file contents with max 4th col

Hi Friends, This is the only solution to my task. So, any help is highly appreciated. I have a file cat input1.bed chr1 100 200 abc chr1 120 300 def chr1 145 226 ghi chr2 567 600 unix Now, I have another file by name input2.bed (This file is a binary file not readable by the... (7 Replies)
Discussion started by: jacobs.smith
7 Replies

5. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

6. UNIX for Dummies Questions & Answers

How to search two strings in a file and print the contents in between to a file

I have a file called po.txt. Here is the content of the file: <!DOCTYPE PurchaseOrderMessage (View Source for full doctype...)> - <PurchaseOrder> - <Header> <MessageId>cdb3062b-685b-4cd5-9633-013186750e10</MessageId> <Timestamp>2011-08-01T13:47:23.536-04:00</Timestamp> </Header> -... (4 Replies)
Discussion started by: webbi
4 Replies

7. Shell Programming and Scripting

script to read the contents of a file and print

Hi, Need help in writing a script to read the contents of this file test Test 00a 00b 00c 00d 00e 00f where it need to read each line to give a display such as form meta from dev 00a , config=Striped; add dev 00b:00f to meta 00a Can any one help me in writing this script (2 Replies)
Discussion started by: praveen1516
2 Replies

8. Shell Programming and Scripting

Read contents of the file and print

AT ---------- 0 Elapsed: 00:00:00.02 SO ---------- 0 Elapsed: 00:00:00.01 SE ---------- 0 Elapsed: 00:00:00.01 CR ---------- (4 Replies)
Discussion started by: sandy1028
4 Replies

9. Shell Programming and Scripting

read file-print lines in sed

Hello! Im trying to read file contents. Then, print out every line that has "/bens/here" in the file that was read. cat /my/file.now | sed '/bens/here/p' I keep getting the error asking if I need to predeclare sed? What does predeclaring sed mean? Thanks! Ben (2 Replies)
Discussion started by: bigben1220
2 Replies

10. Shell Programming and Scripting

search for the contents in many file and print that file using shell script

hello have a file1 H87I Y788O T347U J23U and file2 J23U U887Y I99U T556U file3 I99O J99T F557J file4 N99I T666U R55Y file5 H87I T347U file6 H77U R556Y E44T file7 Y788O K98U H8I May be using script we can use file1 to search for all the files and have the output H87I file5... (3 Replies)
Discussion started by: cdfd123
3 Replies
Login or Register to Ask a Question