Copy pattern inside the file

Hi all,

I have files and have a missing record. I need copy the existing record and mark those values up. For example in the below file 11048 is missing. I need to copy 22001 and copy those create the values for 11048 . I have 120 set of files and I need to do that on all files.

Note the only ID I am missing is 11048 and I have to copy the 22001 to it. There are 120 files I have to do which will be on name file1,file2 ----file120

File 1

11047;1;292;;;;;;;111.21;;;;;;;;;;;;;;;;;;;;;;;;;;
11047;2;292;;;;;;;111.21;;;;;;;;;;;;;;;;;;;;;;;;;;
11047;3;292;;;;;;;111.21;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;1;292;;;;;;;8672148.20;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;2;292;;;;;;;2393827.87;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;3;292;;;;;;;212731.80;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;5;292;;;;;;;1127146.04;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;1;292;;;;;;;3255450.44;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;2;292;;;;;;;878702.70;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;3;292;;;;;;;85449.23;;;;;;;;;;;;;;;;;;;;;;;;;;

Expected File1

11047;1;292;;;;;;;111.21;;;;;;;;;;;;;;;;;;;;;;;;;;
11047;2;292;;;;;;;111.21;;;;;;;;;;;;;;;;;;;;;;;;;;
11047;3;292;;;;;;;111.21;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;1;292;;;;;;;8672148.20;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;2;292;;;;;;;2393827.87;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;3;292;;;;;;;212731.80;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;5;292;;;;;;;1127146.04;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;1;292;;;;;;;8672148.20;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;2;292;;;;;;;2393827.87;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;3;292;;;;;;;212731.80;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;5;292;;;;;;;1127146.04;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;1;292;;;;;;;3255450.44;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;2;292;;;;;;;878702.70;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;3;292;;;;;;;85449.23;;;;;;;;;;;;;;;;;;;;;;;;;;

Hello arunkumar_mca,

If you have latest version of awk OR gawk which allows us to save output into Input_file(s) itself then following may help you on same(since I don't have the latest version so I couldn't test it).

Solution 1st: If you want to save the output into Input_file(s) itself then following may help you.

gawk -i inplace -F";" 'FNR==1{close(file);file=FILENAME} /22001/{val=val?val ORS $0:$0;$1=11048;val_11048=val_11048?val_11048 ORS $0:$0;next} /22002/ && val && val_11048{print val_11048 ORS val;val=val_11048="";next} 1' OFS=";" Input_file[0-9]+

Solution 2nd: If you want to save the output into Input_file(s) along with taking the backup of original Input_file(s) too then following may help you on same.

gawk -i inplace -v -F";" 'FNR==1{close(file);file=FILENAME} /22001/{val=val?val ORS $0:$0;$1=11048;val_11048=val_11048?val_11048 ORS $0:$0;next} /22002/ && val && val_11048{print val_11048 ORS val;val=val_11048="";next} 1' OFS=";"  Input_file[0-9]+

Kindly do check the above codes and do let me know how it goes then.

Thanks,
R. Singh

1 Like

How do you know it's 11048 that is missing? And how do you know 22001 should be used to fill in the gap?
If those are constants, and no logics / algorithms needed / applied, it might be easier to just use an editor and do a block copy.

Both gawk didn't worked. I got the below error. I think I am using old version of awk, Below is what I tried my input files in format like LATAM_TRANS so I replace the input file in the command

gawk -i inplace -v -F";" 'FNR==1{close(file);file=FILENAME} /22001/{val=val?val ORS $0:$0;$1=11048;val_11048=val_11048?val_11048 ORS $0:$0;next} /22002/ && val && val_11048{print val_11048 ORS val;val=val_11048="";next} 1' OFS=";"  LATAM_TRANS

Usage: gawk [POSIX or GNU style options] -f progfile [--] file ...
Usage: gawk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:          GNU long options:
        -f progfile             --file=progfile
        -F fs                   --field-separator=fs
        -v var=val              --assign=var=val
        -m[fr] val
        -W compat               --compat
        -W copyleft             --copyleft
        -W copyright            --copyright
        -W dump-variables[=file]        --dump-variables[=file]
        -W exec=file            --exec=file
        -W gen-po               --gen-po
        -W help                 --help
        -W lint[=fatal]         --lint[=fatal]
        -W lint-old             --lint-old
        -W non-decimal-data     --non-decimal-data
        -W profile[=file]       --profile[=file]
        -W posix                --posix
        -W re-interval          --re-interval
        -W source=program-text  --source=program-text
        -W traditional          --traditional
        -W usage                --usage
        -W version              --version

To report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.

gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.

Examples:
        gawk '{ sum += $1 }; END { print sum }' file
        gawk -F: '{ print $1 }' /etc/passwd

Yes the values are constant. I am using VI to do that. I have to do on 120 files on a directory. Like that I am having 18 directories. I have to find where 22001 is in the file and the copy it and then place it above as 11048. So it is taking time. Worked for 7 hours I completed only 96 files where I have 18*120 files

Try

for FN in LATAM_TRANS; do sed '/22001/ {p; s//11048/}' "$FN" | sort > TMP; mv TMP "$FN" ; done
1 Like

It worked. But it replaced the order. If you look below the second position is the order it is like 1,2,3,4. After doing the replace in command it replaced the order

Input file:

11047;7;292;;;;;;;38.68;;;;;;;;;;;;;;;;;;;;;;;;;;
11047;15;292;;;;;;;273.71;;;;;;;;;;;;;;;;;;;;;;;;;;
11047;16;292;;;;;;;273.71;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;1;292;;;;;;;1137724.23;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;2;292;;;;;;;500197.31;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;3;292;;;;;;;51221.56;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;4;292;;;;;;;11.39;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;5;292;;;;;;;296541.27;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;6;292;;;;;;;58789.22;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;7;292;;;;;;;93633.87;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;8;292;;;;;;;300853.43;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;11;292;;;;;;;5.94;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;12;292;;;;;;;209402.79;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;13;292;;;;;;;91444.70;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;15;292;;;;;;;336673.49;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;16;292;;;;;;;11939.95;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;17;292;;;;;;;35419.41;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;18;292;;;;;;;35494.11;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;19;292;;;;;;;26864.34;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;20;292;;;;;;;65904.01;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;21;292;;;;;;;110799.84;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;23;292;;;;;;;45340.23;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;24;292;;;;;;;4911.60;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;1;292;;;;;;;216356.16;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;2;292;;;;;;;94187.78;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;3;292;;;;;;;20145.44;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;5;292;;;;;;;66421.60;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;6;292;;;;;;;55.19;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;7;292;;;;;;;7565.55;;;;;;;;;;;;;;;;;;;;;;;;;;
22002;8;292;;;;;;;78129.97;;;;;;;;;;;;;;;;;;;;;;;;;; 

Output file

11047;5;292;;;;;;;23.97;;;;;;;;;;;;;;;;;;;;;;;;;;
11047;6;292;;;;;;;5.99;;;;;;;;;;;;;;;;;;;;;;;;;;
11047;7;292;;;;;;;38.68;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;11;292;;;;;;;5.94;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;12;292;;;;;;;209402.79;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;13;292;;;;;;;91444.70;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;15;292;;;;;;;336673.49;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;16;292;;;;;;;11939.95;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;17;292;;;;;;;35419.41;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;18;292;;;;;;;35494.11;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;19;292;;;;;;;26864.34;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;1;292;;;;;;;1137724.23;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;20;292;;;;;;;65904.01;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;21;292;;;;;;;110799.84;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;23;292;;;;;;;45340.23;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;24;292;;;;;;;4911.60;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;2;292;;;;;;;500197.31;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;3;292;;;;;;;51221.56;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;4;292;;;;;;;11.39;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;5;292;;;;;;;296541.27;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;6;292;;;;;;;58789.22;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;7;292;;;;;;;93633.87;;;;;;;;;;;;;;;;;;;;;;;;;;
11048;8;292;;;;;;;300853.43;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;11;292;;;;;;;5.94;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;12;292;;;;;;;209402.79;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;13;292;;;;;;;91444.70;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;15;292;;;;;;;336673.49;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;16;292;;;;;;;11939.95;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;17;292;;;;;;;35419.41;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;18;292;;;;;;;35494.11;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;19;292;;;;;;;26864.34;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;1;292;;;;;;;1137724.23;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;20;292;;;;;;;65904.01;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;21;292;;;;;;;110799.84;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;23;292;;;;;;;45340.23;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;24;292;;;;;;;4911.60;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;2;292;;;;;;;500197.31;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;3;292;;;;;;;51221.56;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;4;292;;;;;;;11.39;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;5;292;;;;;;;296541.27;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;6;292;;;;;;;58789.22;;;;;;;;;;;;;;;;;;;;;;;;;;
22001;7;292;;;;;;;93633.87;;;;;;;;;;;;;;;;;;;;;;;;;;

Without being familiar with vi I think you could capture the vi commands you used in order to automate the task for use in a script...

And what do you think you could to prevent that malbehaviour?

Howsoever, try

for FN in LATAM_TRANS; do sed '/22001/ {p; s//11048/}' "$FN" | sort -t";" -k1,1 -k2,2n > TMP; mv TMP "$FN" ; done

I tried to do sort to overcome the problem. It didn't worked. Any idea how to sort the file based on first 2 field

sort -k1,5 -k7,2 LATAM_TRANS >> TEMP

---------- Post updated at 05:37 PM ---------- Previous update was at 05:28 PM ----------

This worked... Thanks thanks thanks a lot

for FN in file1; do sed '/22001/ {p; s//11048/}' "$FN" | sort -t";" -k1,1 -k2,2n > TMP; mv TMP "$FN" ; done