Score:0

Find and replace the entire line of the source file

ma flag

I am working on a batch script which will call the .awk file to execute the find and replace.

  1. batch file script.cmd file -->

    @gawk -f "%Modify.awk%" "%Temp.csv%" < "%Source.csv%" > "%Output.csv%".
    
  2. Temp.csv contains,

    | UID | Name | Num | Loc | Addr | Str
    |95 | Bank| Amal| | Che| | KKKK
    
  3. 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

SEWTGIYWTKHNTDS avatar
cn flag
This is an awk script
Score:1
cn flag

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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.