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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing line 'i' of file1 with line 'j' of file 2
# 1  
Old 06-08-2011
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
Code:
wqwert
4.4464002
3
319
286
369
46.320002
56.150002
45.100002
1
1
1
0.723

file 2
Code:
3
4.25450
361.0
351.0
319.0

result

Code:
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
# 2  
Old 06-08-2011
Code:
line=`sed '4,4p' file2`
sed '5 c\
$line' file1

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 06-08-2011
Code:
 awk '{if(NR!=5){print;next}system("head -4 file2|tail -1")}' file1

This User Gave Thanks to kumaran_5555 For This Post:
# 4  
Old 06-08-2011
Code:
nawk '{if(NR==5){system("sed -n '4p' file2")} else {print}}' file1

This User Gave Thanks to Shahul For This Post:
# 5  
Old 06-08-2011
One more thing:
Using the option
Code:
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:
Code:
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,
# 6  
Old 06-09-2011
Seems you'd like to replace more than one line at same.

create a template file first:
Code:
$ 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.
Code:
awk 'FILENAME=="template" {a[$2]=$1} 
     FILENAME=="file1" {b[FNR]=$0} 
     FILENAME=="file2" {print (FNR in a)?b[FNR]:$0}
    ' template file1 file2

This User Gave Thanks to rdcwayx For This Post:
# 7  
Old 06-09-2011
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.

Code:
#/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
Code:
head: invalid trailing option -- 2

Thanks,
Sarah

Last edited by f_o_555; 06-09-2011 at 04:42 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pick a line in file 2 basing on array in file1

Dear friends, I have two files. One with all IDs(in a single field) . And another with data(of which say field 5 is ID). I want to create an array of IDs using first file and while reading second file if the ID appears in the array I need to print $0 else skip. After a long gap I am... (6 Replies)
Discussion started by: paresh n doshi
6 Replies

2. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

3. Shell Programming and Scripting

Replacing a line in a file

Hi all, I need to replace a line export TZ=xxxxxxxx with the line export TZ=$1 Now, "xxxxxxxx" in the above line is some unknown string and $1 is a parameter. I want the content of $1 to be replaced with "xxxxxxxx". Kindly help me how to do this in the shell scripting. (5 Replies)
Discussion started by: ddeeps2610
5 Replies

4. Shell Programming and Scripting

Replacing a line in a file using sed

I have a file which has a list in it pop triangle people slow fast What I want to do is search this list and replace people with humans do the list looks like this: pop triangle human slow fast I think i use something like this.... if cat /list.txt | grep -q 'people' ; then (9 Replies)
Discussion started by: digitalviking
9 Replies

5. UNIX for Dummies Questions & Answers

Replacing first line of file by >filename

Hi All, I have a set of files named S5_SK1.chr01 S5_SK1.chr02 S5_SK1.chr03 ..... and the first line of these files is >SK1.chr01 >SK1.chr02 >SK1.chr03 ..... Can anyone suggest how I can change the first line of all these files with the filename itself? So my expected output for the first lines of... (14 Replies)
Discussion started by: pawannoel
14 Replies

6. UNIX for Advanced & Expert Users

how do you parse 1 line at a time of file1 ie. line(n) each line into new file

File 1 <html>ta da....unique file name I want to give file=>343...</html> <html>da ta 234 </html> <html>pa da 542 </html> and so on... File 2 343 234 542 and so on, each line in File 1 one also corresponds with each line in File 2 I have tried several grep, sed, while .. read, do,... (4 Replies)
Discussion started by: web_developer
4 Replies

7. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

8. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies

9. Shell Programming and Scripting

Replacing a line in a file - HELP!!!

I have a problem in the following code ... while read line do #Get Line Number OLDLINE=`sed -n $Lineno $filename` echo "Un Changed Line : "$OLDLINE echo "Enter a New Pattern : " read NewPattern <&1 echo "NewPattern :"$NewPattern NEWLINE=`cat $filename | sed -n... (1 Reply)
Discussion started by: maxmave
1 Replies

10. UNIX for Advanced & Expert Users

replacing first line or lines in a file

hey guys, how do i replace only a line within a file without messing up the rest of the contents of the file? see, if possible can you guys give me a straight forward way to do this. i dont want a complex command. what i mean is i know i can accomplish this by using sed, well, i think i can,... (3 Replies)
Discussion started by: Terrible
3 Replies
Login or Register to Ask a Question