3

i have 2 txt files : file1 and file2 i would like to replace the first line of file2 with the first line of file1, with a bash command

file1:

aaaaaaaa
bbbbbbbb
cccccccc

file2:

zzzzzzzz
yyyyyyyy
wwwwwwww

expected result of file2:

aaaaaaaa
yyyyyyyy
wwwwwwww

this can't be done with sed as you don't know what to replace with what...i'm right? so how to do this ?

EDIT:

so in my particular case (i do it in my openwrt router), what worked is :

sed -i "1c $(sed 1q file1)" file2

Thanks to @Sundeed for the link explaining why some commands were only displaying the results in the shell but not writing in the file : https://mywiki.wooledge.org/BashPitfalls#cat_file_.7C_sed_s.2Ffoo.2Fbar.2F_.3E_file

cetipabo
  • 453
  • 5
  • 12
  • 2
    What did you try for yourself? – Inian Feb 26 '18 at 13:40
  • i'm not a bash guru, i have some examples about how to replace a text you know with another text you know in a file. i didn't find any example with a text you don't know coming from a file into another file with a text you don't know too. that's why i'm asking. – cetipabo Feb 26 '18 at 13:54

4 Answers4

4

Simply use head and tail for this task:

head -n 1 Input_file1 && tail -n 2  Input_file2

Output will be as follows:

aaaaaaaa
yyyyyyyy
wwwwwwww
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 2
    Minor nit: the `-1` notation is considered obsolete and not specified by [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/head.html); instead, `-n 1` should be used (see [manual](https://www.gnu.org/software/coreutils/manual/html_node/head-invocation.html), at the very bottom). – Benjamin W. Feb 26 '18 at 14:08
  • @BenjaminW., thank you sir, added the same now to solution too. – RavinderSingh13 Feb 26 '18 at 14:09
  • 1
    thank you very much, this is less "technical" than sed, that's why i like it ;) – cetipabo Feb 26 '18 at 14:10
  • 2
    (And `-2` as well ;) ) – Benjamin W. Feb 26 '18 at 14:16
  • the output in the shell is correct, but it doesn't write the line inside file2 ? i tried to add >file2 at the end of the command but it doesn't work...it makes an empty file – cetipabo Feb 26 '18 at 20:25
  • 1
    @cetipabo see https://mywiki.wooledge.org/BashPitfalls#cat_file_.7C_sed_s.2Ffoo.2Fbar.2F_.3E_file and https://stackoverflow.com/questions/6696842/bash-redirect-input-from-file-back-into-same-file – Sundeep Feb 27 '18 at 04:33
  • 1
    @sundeep Thank you very much for this link ! – cetipabo Feb 27 '18 at 07:34
4

This might work for you (GNU sed):

sed -e '1R file1' -e '1d' file2

Read the first line of file2. Read the first line of file1 and insert it into the output, then delete the first line of file2. Now read and output the rest of file2.

potong
  • 55,640
  • 6
  • 51
  • 83
  • i tried : sed -e '1R /etc/shadow' -e '1d' /overlay/$(cat /proc/banktable/inactive)/etc/shadow and it says : sed: unsupported command R (i'm doing this on an OpenWRT router) – cetipabo Feb 26 '18 at 20:12
2

You certainly can do this with sed, but why would you?

sed "1c\\
$(sed 1q file1)
" file2
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • whouaou, thank you william ! so what's the meaning of 1c and 1q ? i can't even find this in the manual https://www.gnu.org/software/sed/manual/sed.html – cetipabo Feb 26 '18 at 14:03
  • 1
    @cetipabo https://www.gnu.org/software/sed/manual/sed.html#index-c-_0028change-to-text-lines_0029-command and https://www.gnu.org/software/sed/manual/sed.html#index-q-_0028quit_0029-command – Benjamin W. Feb 26 '18 at 14:07
  • when i do this: sed "1c $(sed 1q /etc/shadow)" /overlay/$(cat /proc/banktable/inactive)/etc/shadow it doesn't change the line and just display the content of the file in the shell...i'm doing this in an OpenWrt router so maybe sed is not compiled with all possible options ? – cetipabo Feb 26 '18 at 20:09
  • this is working : sed "1c $(sed 1q file1)" file2 | tee file3 but i don't want to create a file3 ! if i use | tee file2 i just create an empty file2, so looks like i can't overwrite file2 if it is opened by sed ?! – cetipabo Feb 26 '18 at 20:57
  • well so finaly this is what worked ! sed -i "1c $(sed 1q file1)" file2 – cetipabo Feb 26 '18 at 21:08
  • 1
    potong's sed solution is really much better than this. My solution is something I would never consider using, potong's is actually reasonable. – William Pursell Feb 26 '18 at 23:12
  • unfortunately his solution gives me a : unsupported command R – cetipabo Feb 27 '18 at 07:18
1

Or with ed

f1="file1";f2="file2";printf "%s\n" '2,$d' "r $f2" '2d' "wq $f2" | ed -s "$f1"
ctac_
  • 2,413
  • 2
  • 7
  • 17