Replacing line 'i' of file1 with line 'j' of file 2

Hi All,
As mentioned in the title I have two text files and I would like to replace line number 5 of file #1 with line number 4 of file #2

e.g.

file 1

wqwert
4.4464002
3
319
286
369
46.320002
56.150002
45.100002
1
1
1
0.723

file 2

3
4.25450
361.0
351.0
319.0

result

wqwert
4.4464002
3
319
351.0
369
46.320002
56.150002
45.100002
1
1
1
0.723

Any suggestion? I've been looking around but I couldn't find anything specific.
Cheers,
Sarah

line=`sed '4,4p' file2`
sed '5 c\
$line' file1
1 Like
 awk '{if(NR!=5){print;next}system("head -4 file2|tail -1")}' file1
1 Like
nawk '{if(NR==5){system("sed -n '4p' file2")} else {print}}' file1
1 Like

One more thing:
Using the option

awk '{if(NR!=5){print;next}system("head -4 file2|tail -1")}' file1

I would like to have to use variables instead of numbers. E.g. having defined:

POSITIONS_CASE5=("5" "7" "8" "9" "10")
LINES_CASE5=("3" "4" "5" "6" "7")
for (( j=0;j<1;j++)); do
    echo $j ${POSITIONS_CASE5[${j}]} ${LINES_CASE5[${j}]}
    awk '{if(NR!='$POSITIONS_CASE5[$j]'  ){print;next}system("head -$LINES_CASE5[$j]  file02.dat|tail -1")}' ${TMPFILE}1
done

does nothing. Do you know what I'm doing wrong?
Thank you,

Seems you'd like to replace more than one line at same.

create a template file first:

$ cat template
5 3
6 4
7 5
8 6
9 7
10 8

Then run the awk command, you will get the result directly.

awk 'FILENAME=="template" {a[$2]=$1} 
     FILENAME=="file1" {b[FNR]=$0} 
     FILENAME=="file2" {print (FNR in a)?b[FNR]:$0}
    ' template file1 file2
1 Like

Hi,
yes you are correct I hae to replace 'v1' in file ${TMPFILE}1 with line B of file file02.dat
but you script doesn't seem to do that. I mean it replaces some strings but the file that obtain is not what I expect.
This is what I got so far. It doesn't seem to accept the parameter B.

#/bin/bash
POSITIONS_CASE5=("5" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "31" "34" "35" "38" "57" "144" "147" "148" "166" "253" "256" "257" "275" "362" "365" "366" "384" "471" "474" "475" "493" "577")

LINES_CASE5=("3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31" "32" "33" "34" "35")

ELEMENTS_5A=${#LINES_CASE5[@]}  
ELEMENTS_5B=${#POSITIONS_CASE5[@]}  

echo $ELEMENTS_5A $ELEMENTS_5B
###############################################################################
for i in 1 2
do
  read my_data[$i]
done < file02.dat

INFILE="TEST.inp"
TMPFILE=${INFILE}.tmp
OUTFILE=${my_data[1]}

cp ${INFILE} ${TMPFILE}1
echo $INFILE $TMPFILE $OUTFILE
###############################################################################
DATE
for (( j=0;j<$ELEMENTS_5A;j++)); do
    echo $j ${POSITIONS_CASE5[${j}]} ${LINES_CASE5[${j}]}
    B=${LINES_CASE5[${j}]}
    awk '{if(NR!=v1 ){print;next}system("head -v2  v3|tail -1")}' v1=${POSITIONS_CASE5[${j}]} v2=${B} v3=file02.dat ${TMPFILE}1 > ${TMPFILE}2
    mv ${TMPFILE}2 ${TMPFILE}1
done
mv ${TMPFILE}1 ${OUTFILE}
DATE

I get the rror message

head: invalid trailing option -- 2

Thanks,
Sarah

the below line is wrong,

awk '{if(NR!=v1 ){print;next}system("head -v2  v3|tail -1")}' v1=${POSITIONS_CASE5[${j}]} v2=${B} v3=file02.dat 

Instead of this, try this one

awk -v v1=${POSITIONS_CASE5[${j}]} v2=${B} v3=file02.dat  '{if(NR!=v1 ){print;next}system("head -"v2"  "v3"|tail -1")}'  ${TMPFILE}1 > ${TMPFILE}2

THis should work

1 Like

I tried you suggestion but I get this error message

awk: cmd. line:1: fatal: cannot open file `{if(NR!=v1 ){print;next}system("head -"v2"  "v3"|tail -1")}' for reading (No such file or directory)

Try this,

awk -v v1=${POSITIONS_CASE5[${j}]}-v v2=${B}-v v3=file02.dat   '{if(NR!=v1 ){print;next}system("head -"v2"  "v3"|tail -1")}'   ${TMPFILE}1 > ${TMPFILE}2 
1 Like

Sorry, I get the same error message...

Here is the output i have got, using solaris with nawk. Can you check are we on the same page.

user@tonga> (/home/user) $ echo $a $b $f
2 1 test.txt
user@tonga> (/home/user) $ cat test.txt
TABLE1
TABLE1 A
TABLE2
TABLE2 B
TABLE3
TABLE4
TABLE4 C
TABLE4
user@tonga> (/home/user) $ /bin/nawk -v v1=$a -v v2=$b -v v3=$f '{if(NR==v1){system("head -"v2" "v3"|tail -1") }}' test.txt
TABLE1
user@tonga> (/home/user) $
1 Like

I tried to add a space before '-v'

    awk -v v1=${POSITIONS_CASE5[${j}]} -v v2=${B} -v v3=file02.dat   '{if(NR!=v1 ){print;next}system("head -"v2"  "v3"|tail -1")}'   ${TMPFILE}1 > ${TMPFILE}2

now it works.
Thanks a lot