Comparing the Value from CSV file and write the data in the another CSV File [on hold]











up vote
0
down vote

favorite












I found this example in Internet when I am trying to execute below mentioned error is accoured.



INPUT:



FILE1:



1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,00006298G1,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,N,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312


FILE2:



000116T3,1AA
000117T3,888
000118T3,201
000122T3,2BB


OBJECTIVE: Reading the FILE 2 and Compare it with FILE 1 to write data in the FILE1 CSV file



#!/bin/bash

while read f2
do
kee2=${f2##*,}
line=$(grep $kee2 file1)
if [[ $line && $(echo $line | cut -d, -f12) == 1 ]]; then
srch=$(echo $line | cut -d, -f6)
repl=${f2%,*}
linenum=$(sed -n "/$kee2/=" file1)
sed -i "$linenum s/$srch/$repl/g" file1
fi
done < file2
cat file1


output:



1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,000118T3,0,000118T3,000118T3,000118T3,000118T3,1,00.30887539,0.00050312


Expected Output



1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312









share|improve this question









New contributor




user322785 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as unclear what you're asking by Scott, muru, Isaac, Kusalananda, αғsнιη Nov 26 at 7:39


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • I can see that some of the field from file2 is inserted into file1, but it seems rather random (nothing is inserted in the 1AA line, and you expect the 5th field on the 2BB line to be modified, but the 3rd field on the 201 line). Could ye describe more closely what you expect to happen? Until then, I'll flag this question as unclear. Also, please indicate whether one or the other, or both, files are large.
    – Kusalananda
    Nov 26 at 7:37

















up vote
0
down vote

favorite












I found this example in Internet when I am trying to execute below mentioned error is accoured.



INPUT:



FILE1:



1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,00006298G1,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,N,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312


FILE2:



000116T3,1AA
000117T3,888
000118T3,201
000122T3,2BB


OBJECTIVE: Reading the FILE 2 and Compare it with FILE 1 to write data in the FILE1 CSV file



#!/bin/bash

while read f2
do
kee2=${f2##*,}
line=$(grep $kee2 file1)
if [[ $line && $(echo $line | cut -d, -f12) == 1 ]]; then
srch=$(echo $line | cut -d, -f6)
repl=${f2%,*}
linenum=$(sed -n "/$kee2/=" file1)
sed -i "$linenum s/$srch/$repl/g" file1
fi
done < file2
cat file1


output:



1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,000118T3,0,000118T3,000118T3,000118T3,000118T3,1,00.30887539,0.00050312


Expected Output



1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312









share|improve this question









New contributor




user322785 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as unclear what you're asking by Scott, muru, Isaac, Kusalananda, αғsнιη Nov 26 at 7:39


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • I can see that some of the field from file2 is inserted into file1, but it seems rather random (nothing is inserted in the 1AA line, and you expect the 5th field on the 2BB line to be modified, but the 3rd field on the 201 line). Could ye describe more closely what you expect to happen? Until then, I'll flag this question as unclear. Also, please indicate whether one or the other, or both, files are large.
    – Kusalananda
    Nov 26 at 7:37















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I found this example in Internet when I am trying to execute below mentioned error is accoured.



INPUT:



FILE1:



1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,00006298G1,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,N,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312


FILE2:



000116T3,1AA
000117T3,888
000118T3,201
000122T3,2BB


OBJECTIVE: Reading the FILE 2 and Compare it with FILE 1 to write data in the FILE1 CSV file



#!/bin/bash

while read f2
do
kee2=${f2##*,}
line=$(grep $kee2 file1)
if [[ $line && $(echo $line | cut -d, -f12) == 1 ]]; then
srch=$(echo $line | cut -d, -f6)
repl=${f2%,*}
linenum=$(sed -n "/$kee2/=" file1)
sed -i "$linenum s/$srch/$repl/g" file1
fi
done < file2
cat file1


output:



1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,000118T3,0,000118T3,000118T3,000118T3,000118T3,1,00.30887539,0.00050312


Expected Output



1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312









share|improve this question









New contributor




user322785 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I found this example in Internet when I am trying to execute below mentioned error is accoured.



INPUT:



FILE1:



1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,00006298G1,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,N,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312


FILE2:



000116T3,1AA
000117T3,888
000118T3,201
000122T3,2BB


OBJECTIVE: Reading the FILE 2 and Compare it with FILE 1 to write data in the FILE1 CSV file



#!/bin/bash

while read f2
do
kee2=${f2##*,}
line=$(grep $kee2 file1)
if [[ $line && $(echo $line | cut -d, -f12) == 1 ]]; then
srch=$(echo $line | cut -d, -f6)
repl=${f2%,*}
linenum=$(sed -n "/$kee2/=" file1)
sed -i "$linenum s/$srch/$repl/g" file1
fi
done < file2
cat file1


output:



1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,000118T3,0,000118T3,000118T3,000118T3,000118T3,1,00.30887539,0.00050312


Expected Output



1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312






shell-script






share|improve this question









New contributor




user322785 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user322785 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 26 at 5:15









muru

35.2k582155




35.2k582155






New contributor




user322785 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 26 at 5:02









user322785

1




1




New contributor




user322785 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user322785 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user322785 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as unclear what you're asking by Scott, muru, Isaac, Kusalananda, αғsнιη Nov 26 at 7:39


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






put on hold as unclear what you're asking by Scott, muru, Isaac, Kusalananda, αғsнιη Nov 26 at 7:39


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • I can see that some of the field from file2 is inserted into file1, but it seems rather random (nothing is inserted in the 1AA line, and you expect the 5th field on the 2BB line to be modified, but the 3rd field on the 201 line). Could ye describe more closely what you expect to happen? Until then, I'll flag this question as unclear. Also, please indicate whether one or the other, or both, files are large.
    – Kusalananda
    Nov 26 at 7:37




















  • I can see that some of the field from file2 is inserted into file1, but it seems rather random (nothing is inserted in the 1AA line, and you expect the 5th field on the 2BB line to be modified, but the 3rd field on the 201 line). Could ye describe more closely what you expect to happen? Until then, I'll flag this question as unclear. Also, please indicate whether one or the other, or both, files are large.
    – Kusalananda
    Nov 26 at 7:37


















I can see that some of the field from file2 is inserted into file1, but it seems rather random (nothing is inserted in the 1AA line, and you expect the 5th field on the 2BB line to be modified, but the 3rd field on the 201 line). Could ye describe more closely what you expect to happen? Until then, I'll flag this question as unclear. Also, please indicate whether one or the other, or both, files are large.
– Kusalananda
Nov 26 at 7:37






I can see that some of the field from file2 is inserted into file1, but it seems rather random (nothing is inserted in the 1AA line, and you expect the 5th field on the 2BB line to be modified, but the 3rd field on the 201 line). Could ye describe more closely what you expect to happen? Until then, I'll flag this question as unclear. Also, please indicate whether one or the other, or both, files are large.
– Kusalananda
Nov 26 at 7:37












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Try this,



while read f2
do
kee2=${f2##*,}
line=$(grep $kee2 file1)
if [[ $line && $(echo $line | cut -d, -f12) == 1 ]]; then
srch=$(echo $line | cut -d, -f6 | sed 's#\#\\#')
repl=${f2%,*}
linenum=$(sed -n "/$kee2/=" file1)
sed -i ''$linenum' s/'$srch'/'$repl'/' file1
fi
done < file2
cat file1

1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312


Correction 1 : Expected output replaces only first occurrence, so remove the global replace in sed



Correction 2 : $srch may having . So replace with \ to nullify the function in while replacing with $repl






share|improve this answer





















  • This will have to be redone in something like awk. You're calling five external utilities in a loop, and rewriting the whole of file1 each iteration. This is going to be really slow for larger files.
    – Kusalananda
    Nov 26 at 7:34


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Try this,



while read f2
do
kee2=${f2##*,}
line=$(grep $kee2 file1)
if [[ $line && $(echo $line | cut -d, -f12) == 1 ]]; then
srch=$(echo $line | cut -d, -f6 | sed 's#\#\\#')
repl=${f2%,*}
linenum=$(sed -n "/$kee2/=" file1)
sed -i ''$linenum' s/'$srch'/'$repl'/' file1
fi
done < file2
cat file1

1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312


Correction 1 : Expected output replaces only first occurrence, so remove the global replace in sed



Correction 2 : $srch may having . So replace with \ to nullify the function in while replacing with $repl






share|improve this answer





















  • This will have to be redone in something like awk. You're calling five external utilities in a loop, and rewriting the whole of file1 each iteration. This is going to be really slow for larger files.
    – Kusalananda
    Nov 26 at 7:34















up vote
0
down vote













Try this,



while read f2
do
kee2=${f2##*,}
line=$(grep $kee2 file1)
if [[ $line && $(echo $line | cut -d, -f12) == 1 ]]; then
srch=$(echo $line | cut -d, -f6 | sed 's#\#\\#')
repl=${f2%,*}
linenum=$(sed -n "/$kee2/=" file1)
sed -i ''$linenum' s/'$srch'/'$repl'/' file1
fi
done < file2
cat file1

1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312


Correction 1 : Expected output replaces only first occurrence, so remove the global replace in sed



Correction 2 : $srch may having . So replace with \ to nullify the function in while replacing with $repl






share|improve this answer





















  • This will have to be redone in something like awk. You're calling five external utilities in a loop, and rewriting the whole of file1 each iteration. This is going to be really slow for larger files.
    – Kusalananda
    Nov 26 at 7:34













up vote
0
down vote










up vote
0
down vote









Try this,



while read f2
do
kee2=${f2##*,}
line=$(grep $kee2 file1)
if [[ $line && $(echo $line | cut -d, -f12) == 1 ]]; then
srch=$(echo $line | cut -d, -f6 | sed 's#\#\\#')
repl=${f2%,*}
linenum=$(sed -n "/$kee2/=" file1)
sed -i ''$linenum' s/'$srch'/'$repl'/' file1
fi
done < file2
cat file1

1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312


Correction 1 : Expected output replaces only first occurrence, so remove the global replace in sed



Correction 2 : $srch may having . So replace with \ to nullify the function in while replacing with $repl






share|improve this answer












Try this,



while read f2
do
kee2=${f2##*,}
line=$(grep $kee2 file1)
if [[ $line && $(echo $line | cut -d, -f12) == 1 ]]; then
srch=$(echo $line | cut -d, -f6 | sed 's#\#\\#')
repl=${f2%,*}
linenum=$(sed -n "/$kee2/=" file1)
sed -i ''$linenum' s/'$srch'/'$repl'/' file1
fi
done < file2
cat file1

1AA,LGV_PONCEY_LES_ATHEE,1,N,1,00020460E1,0,N,N,N,N,2,00.22335321,0.00466628
2BB,LES_POUGES_ASF,N,200,200,000122T3,0,N,N,N,N,1,00.30887539,0.00050312
201,LES_POUGES_ASF,000118T3,201,201,N,0,N,N,N,N,1,00.30887539,0.00050312


Correction 1 : Expected output replaces only first occurrence, so remove the global replace in sed



Correction 2 : $srch may having . So replace with \ to nullify the function in while replacing with $repl







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 at 6:36









msp9011

3,58043862




3,58043862












  • This will have to be redone in something like awk. You're calling five external utilities in a loop, and rewriting the whole of file1 each iteration. This is going to be really slow for larger files.
    – Kusalananda
    Nov 26 at 7:34


















  • This will have to be redone in something like awk. You're calling five external utilities in a loop, and rewriting the whole of file1 each iteration. This is going to be really slow for larger files.
    – Kusalananda
    Nov 26 at 7:34
















This will have to be redone in something like awk. You're calling five external utilities in a loop, and rewriting the whole of file1 each iteration. This is going to be really slow for larger files.
– Kusalananda
Nov 26 at 7:34




This will have to be redone in something like awk. You're calling five external utilities in a loop, and rewriting the whole of file1 each iteration. This is going to be really slow for larger files.
– Kusalananda
Nov 26 at 7:34



Popular posts from this blog

サソリ

広島県道265号伴広島線

Setup Asymptote in Texstudio