Find and replace the entire line of the source file [closed]
I am working on a batch script which will call the .awk file to execute the find and replace.
-
batch file script.cmd file -->
@gawk -f "%Modify.awk%" "%Temp.csv%" < "%Source.csv%" > "%Output.csv%".
-
Temp.csv contains,
| UID | Name | Num | Loc | Addr | Str |95 | Bank| Amal| | Che| | KKKK
-
Source contains
| UID | Name | Num | Loc | Addr | Str |34 | Per | ffff | hhhh | kkkk | llll | KKKK |95 | Bank| ffff | hhhh | XXXX | YYYY | LLLL |100 | Hel | Join | JJJJ | HHHH
After executing batch script i want to have the output as below(i.e) Temp file value should be replaced in source.
Output:
| UID | Name | Num | Loc | Addr | Str
|34 | Per | ffff | hhhh | kkkk | llll | KKKK
|95 | Bank| Amal| | Che| | KKKK
|100 | Hel | Join | JJJJ | HHHH
Modify.awk:
BEGIN{
#
# Define field separator
#
FS="\t";
OFS="\t";
}
{
/^95/
{
getline
print $1
}
}
END{
# print "NReject: ",NReject," on a total of: ",NR-1;
}
NOTE: The Temp & Source file is .csv file fields separated with Tab value
I would create a sed script to replace your temp file because sed is designed for this task. Awk is better at other things.
eg: create a file rep.sed
with the replacement commands, to find and replace the lines starting with 95 you could use:
s/^|95.*/|95 | Bank| Amal| | Che| | KKKK/g
then run sed on your source
sed -f rep.sed sourcefile
Pipe the output to a new file when it is working
sed -f rep.sed sourcefile > updatedsourcefile
adding other lines to rep.sed will perform all replacements in one pass
s/^|95.*/|95 | Bank| Amal| | Che| | KKKK/g
s/^|100.*/|100 | Bank| Amal| | Che| | LLLL/g