3

I'm trying to find the sed command so that I can put filename into filename1. These are my two separate files.

INPUT filename has:

Cindy   11 22 54
Chester 48 12 84

INPUT filename1 has:

 Name  Class1 Class2 Class3
Lee      92     94     88
Chancy   91     85     95
Dora     99     77     96


Jefferry 84     98     90

This is the result that I need (output):

Name   Class1 Class2 Class3
Lee      92     94     88
Chancy   91     85     95
Dora     99     77     96
Cindy    11     22     54
Chester  48     12     84
Jefferry 84     98     90

If I need to clarify anything let me know. Basically Cindy and Chester has to be right in between Dora and Jefferry.

  • What is the order of lines in the output? – jimmij Oct 13 '14 at 23:42
  • There shouldnt be any order if i remember correctly. I'm not 100% sure, I just know that the output has to look like that. Still an amateur at this so everything is sort of new. – TayshaunS Oct 13 '14 at 23:49
7

To add all lines from filename to filename1 using sed command you can do the following:

sed r filename1 filename

Please note however that the result will be slightly different from the output in your question, namely:

Name  Class1 Class2 Class3
Lee      92     94     88
Chancy   91     85     95
Dora     99     77     96
Jefferry 84     98     90
Cindy    11     22     54
Chester  48     12     84

Edit

Some additional sed information useful for this question:

  • To add filename after 4th line of filename1:

    sed '4 r filename' filename1

  • To add filename after line which starts from "Dora" in filename1:

    sed '/^Dora/ r filename' filename1

  • To add filename after 4th line and remove any blank lines from filename1:

    sed '/^$/d;4 r filename' filename1

| improve this answer | |
  • Cindy and Chester has to be after Dora. I did sed r filename1 filename but Cindy and Chester are at the end of the order and theres still a space gap. – TayshaunS Oct 14 '14 at 0:09
  • If you want the additional lines to be inserted after the line starting with Dora then you can do sed '/^Dora/ r filename' filename1 – steeldriver Oct 14 '14 at 0:19
  • That works, but after entering that command. I need to eliminate the blank lines that are between Chester and Jefferry. – TayshaunS Oct 14 '14 at 0:24
  • @TayshaunS See the edit of my answer. – jimmij Oct 14 '14 at 0:34
  • You are a life saver, i was trying all sorts of combination of those two /^$/d and /^Dora/ r. But it kept giving me an error. Couldnt figure it out, I appreciate everyones time and help! – TayshaunS Oct 14 '14 at 0:39
2

There shouldnt be any order if i remember correctly.

In that case:

$ cat file2 file1 | column -t
Name      Class1  Class2  Class3
Lee       92      94      88
Chancy    91      85      95
Dora      99      77      96
Jefferry  84      98      90
Cindy     11      22      54
Chester   48      12      84
| improve this answer | |
2

If you do:

sed r file1 file2

...then sed will try and fail to append a read file with a null-length filename to every input line of both its named input files file1 and file2. That command is essentially no different than:

sed '' file1 file2

...except that w/ r it is slower.

This is what I mean:

seq 1  5 > file1
seq 6 10 > file2
strace sed r file[12]

...
write("1\n", 21) = 2
open("", O_RDONLY) = -1 ENOENT (No such file or directory)
write("2\n", 21) = 2
open("", O_RDONLY) = -1 ENOENT (No such file or directory)
write("3\n", 21) = 2
open("", O_RDONLY) = -1 ENOENT (No such file or directory)
write("4\n", 21) = 2
open("", O_RDONLY) = -1 ENOENT (No such file or directory)
write("5\n", 21) = 2
open("", O_RDONLY) = -1 ENOENT (No such file or directory)
read(3, "", 4096) = 0
close(3) = 0
open("file2", RDONLY) = 3
write("6\n", 21) = 2
open("", O_RDONLY) = -1 ENOENT (No such file or directory)
write("7\n", 21) = 2
...

See the open("" ...) failing with ENOENT following every line written out? Thats the empty read command sed runs. You don't notice the failures because sed is spec'd not to complain about a non-existent read file named in its script, but rather to carry on as if nothing happened when it tries to read one.

And maybe it looks like it works because you just want to append a file to the end of another - to concatenate two files - which sed does by default when two input files are named - it reads the first and then the next. But if the read command really worked then the named read file would be appended entirely to every line in input.

Like this:

seq 3 > file
printf %s\\n a b c | 
sed r\ file

a
1
2
3
b
1
2
3
c
1
2
3

To append the contents of one file to another you can do:

cat < file2 >> file1

To append after only a certain point in input you have some options. sed is among them:

sed -e '/match/{r file2' -e:n -e 'n;bn' -e \} file1

...which should work w/ practically any sed.

Or else if you have a POSIX sed:

{ sed /match/q; cat file2 -; } < file1

...which should be far more efficient.

Both methods ensure that the contents of file2 is only read out once and only immediately following the first occurrencd of match in file1.

Another way could look like:

cat file2 | sed /match/r\ /dev/fd/0 file1

...which would read a null-length file out after every match following the first...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.