sed to read line by line and input into another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to read line by line and input into another file
# 1  
Old 07-06-2010
sed to read line by line and input into another file

I have two files.

Fileone contains
Code:
text string one
text string two
text string three

Filetwo contains
Code:
Name:
Address:
Summary:
Name:
Address:
Summary:
Name:
Address:
Summary:

I would like to use sed to read each line of file one and put it at the end of the summary line of file two.
Like this
Code:
Name:
Address:
Summary:Text String one
Name:
Address:
Summary:Text string two
Name:
Address:
Summary: Text String three


I have tried several things. But this got close.
Code:
while read line;
do
  sed "/Summary/s/$/$line/g" filetwo 
done < fileone

This puts text string one from file one onto each of the Summary lines. I would in fact like to have text string two put on the second Summary field. I though sed should read a file line by line anyway so the while may not be necessary but like I say it got me closest. I also tried placing double quotes around the "$line" this seemed to take each text string properly but gave an error due to incorrect expression. Any help would be appreciated.

Thanks..

Last edited by Franklin52; 07-08-2010 at 02:43 PM.. Reason: Please use code tags
# 2  
Old 07-06-2010
Code:
#!/bin/bash

exec 3<file1
while read line
do [[ $line == Summary: ]] && {
      read -u 3 toadd
      echo "$line$toadd" >> file3
   } || echo "$line" >> file3
done <file2
mv file3 file1

this also works with ksh93.
# 3  
Old 07-07-2010
Hi


Code:
awk 'NR==FNR{a[i++]=$0;next;}{ if ($0 ~ /Summary/) printf"%s %s\n", $0,a[j++];else print;}' i=1 j=1 file1 file2


Guru
# 4  
Old 07-08-2010
Thanks for the replies. I used the awk solution. It was easier to run and quicker as I changed the "Summary" pattern to match several other patterns as well.

Thanks..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

4. Shell Programming and Scripting

Read input file with in awk script not through command line

Hi All, Do we know how to read input file within awk script and send output toanother log file. All this needs to be in awk script, not in command line. I am running this awk through crontab. Cat my.awk #!/bin/awk -f function test(var){ some code} { } END { print"test code" } (5 Replies)
Discussion started by: random_thoughts
5 Replies

5. Shell Programming and Scripting

sed replace characters in next line with input from a file

Hi, I have a set of strings in filea. I want to search string xyz in fileb and replace next line in file b with the content from filea. #cat filea abc def ghi #cat fileb asdkjdslka sajljskdjoi xyzjjjjkko aaaaaaaa bbbbbbbb cccccccc xyzsdsajd dddddddd eeeeeeee (2 Replies)
Discussion started by: anilvk
2 Replies

6. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

7. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies

8. Shell Programming and Scripting

Read logline line by line with awk/sed

Hello, I have a logfile which is in this format: 1211667249500#3265 1211667266687#2875 1211667270781#1828 Is there a way to read the logfile line by line every time I execute the code and put the two numbers in the line in two separate variables? Something like: 1211667249500#3265... (7 Replies)
Discussion started by: dejavu88
7 Replies

9. Shell Programming and Scripting

sed not outputting last line of input file

I am using sed for a simple substitution (see command syntax below). Everything works fine except that the last line of the input file does not get written to the output file. Has anyone ever seen this and know of way to force the last line to be written? I don't know if it's playing a part in... (3 Replies)
Discussion started by: 2reperry
3 Replies

10. Shell Programming and Scripting

read a file as input and pass each line to another script

Hi, I am trying to write a ftp script which will read a file for filenames and ftp those files to another server. Here's my ftp script, but it's scanning the current directory for file names. My question is how can I pass multiple files (these files will have the name of data files that need to... (0 Replies)
Discussion started by: sajjad02
0 Replies
Login or Register to Ask a Question