Comparing the Value from CSV file and write the data in the another CSV File [closed]
Clash Royale CLAN TAG#URR8PPP
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
shell-script
closed 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.
add a comment |
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
shell-script
closed 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 fromfile2
is inserted intofile1
, but it seems rather random (nothing is inserted in the1AA
line, and you expect the 5th field on the2BB
line to be modified, but the 3rd field on the201
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
add a comment |
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
shell-script
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
shell-script
edited Nov 26 at 5:15
muru
35.3k582155
35.3k582155
asked Nov 26 at 5:02
user322785
1
1
closed 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.
closed 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 fromfile2
is inserted intofile1
, but it seems rather random (nothing is inserted in the1AA
line, and you expect the 5th field on the2BB
line to be modified, but the 3rd field on the201
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
add a comment |
I can see that some of the field fromfile2
is inserted intofile1
, but it seems rather random (nothing is inserted in the1AA
line, and you expect the 5th field on the2BB
line to be modified, but the 3rd field on the201
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
add a comment |
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
This will have to be redone in something likeawk
. You're calling five external utilities in a loop, and rewriting the whole offile1
each iteration. This is going to be really slow for larger files.
– Kusalananda
Nov 26 at 7:34
add a comment |
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
This will have to be redone in something likeawk
. You're calling five external utilities in a loop, and rewriting the whole offile1
each iteration. This is going to be really slow for larger files.
– Kusalananda
Nov 26 at 7:34
add a comment |
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
This will have to be redone in something likeawk
. You're calling five external utilities in a loop, and rewriting the whole offile1
each iteration. This is going to be really slow for larger files.
– Kusalananda
Nov 26 at 7:34
add a comment |
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
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
answered Nov 26 at 6:36
msp9011
3,57643862
3,57643862
This will have to be redone in something likeawk
. You're calling five external utilities in a loop, and rewriting the whole offile1
each iteration. This is going to be really slow for larger files.
– Kusalananda
Nov 26 at 7:34
add a comment |
This will have to be redone in something likeawk
. You're calling five external utilities in a loop, and rewriting the whole offile1
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
add a comment |
I can see that some of the field from
file2
is inserted intofile1
, but it seems rather random (nothing is inserted in the1AA
line, and you expect the 5th field on the2BB
line to be modified, but the 3rd field on the201
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