Copy pattern inside the file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Copy pattern inside the file
# 1  
Old 03-23-2018
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
Code:
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

Code:
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;;;;;;;;;;;;;;;;;;;;;;;;;;


Last edited by arunkumar_mca; 03-24-2018 at 12:04 AM.. Reason: Added more details
# 2  
Old 03-24-2018
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.
Code:
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.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 03-24-2018
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.
# 4  
Old 03-24-2018
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

Code:
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




Quote:
Originally Posted by RudiC
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.
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

Last edited by arunkumar_mca; 03-24-2018 at 08:10 AM.. Reason: spelling correction
# 5  
Old 03-24-2018
Try
Code:
for FN in LATAM_TRANS; do sed '/22001/ {p; s//11048/}' "$FN" | sort > TMP; mv TMP "$FN" ; done

This User Gave Thanks to RudiC For This Post:
# 6  
Old 03-24-2018
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:

Code:
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
Code:
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;;;;;;;;;;;;;;;;;;;;;;;;;;

# 7  
Old 03-24-2018
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string inside a pattern matched block of a file

How to grep for searching a string within a begin and end pattern of a file. Sent from my Redmi 3S using Tapatalk (8 Replies)
Discussion started by: Baishali
8 Replies

2. Shell Programming and Scripting

Copy and paste text inside a xml file

I have a really big XML file. I need copy the value of one tag inside another one tag. I try to publish one example. <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1">Rai 1</channel> <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1 +2HD">Rai 1... (6 Replies)
Discussion started by: Tapiocapioca
6 Replies

3. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

4. Shell Programming and Scripting

Help with matching pattern inside a file

I have a huge file that has roughly 30304 lines. I need to extract specific info from that file. For example, Box 1 > *aaaaaaaajjjj* > hbbvjvj > jdnnfddllll > *dgdfhfekwjh* Box 2 > *aaaaaaa'aj'jjj* > dse hkjuejef bfdw > dyeee > dsewq > *dgdfhfekwjh* >feweiuei Box 3 > *aaaa"aaaaj"jjj* >... (25 Replies)
Discussion started by: Ernst
25 Replies

5. Homework & Coursework Questions

copy files inside a text file

Hi Guys , I am new to this and Hi to all ,Need your help I am trying to copy Files which are inside file.txt The files inside file.txt are inthe below order file1.log file2.log file3.log ....... I want to copy these files to an output Directory , Please help (1 Reply)
Discussion started by: hc17972
1 Replies

6. Homework & Coursework Questions

copy files inside a text file

Hi Guys , I am new to this and Hi to all ,Need your help I am trying to copy Files which are inside file.txt The files inside file.txt are inthe below order file1.log file2.log file3.log ....... I want to copy these files to an output Directory , Please help (1 Reply)
Discussion started by: hc17972
1 Replies

7. Shell Programming and Scripting

how to find the pattern inside the file and replace it

hello everybody, I have a group of file eg- sample1 sample2 sample3 sample4 each file contain this :- cat sample1 SEQ_NUM,1,UPESI1 My requirement is to change the value-UPESI1 to UPE10 in file which contain this pattern -UPESI1. any help is appreciated. (2 Replies)
Discussion started by: abhigrkist
2 Replies

8. Shell Programming and Scripting

pattern replace inside text file using sed

Hi, I have a situation where I want to replace some occurrences of ".jsp" into ".html" inside a text file. For Example: If a pattern found like <a href="http://www.mysite.com/mypage.jsp"> it should be retained. But if a pattern found like <a href="../mypage.jsp"> it should be changed to... (4 Replies)
Discussion started by: meharo
4 Replies

9. Shell Programming and Scripting

Need help in sed command ( Replacing a pattern inside a file with a variable value )

Hello, The following sed command is giving error sed: -e expression #1, char 13: unknown option to `s' The sed command is echo "//-----" | sed "s/\/\/---*/$parChk/g" where parChk="//---ee-" How can i print the variable value from sed command ? And is it possible to replace a... (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. Shell Programming and Scripting

How to search a pattern inside a zipped file ie (.gz file) with out unzipping it

How to search a pattern inside a zipped file ie (.gz file) with out unzipping it? using grep command.. Bit urgent.. pls..help me (2 Replies)
Discussion started by: senraj01
2 Replies
Login or Register to Ask a Question