Script to change the file content using some conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to change the file content using some conditions
# 1  
Old 02-08-2017
Script to change the file content using some conditions

Hello,

I would like to change the content of the file that has few blocks starts with 10 and ends with ";"

File1.txt

Code:
10 bonuses D
   20 MATCHED
   30 UPD COL
   40 (SOL=30) 
   20 NOT MATCHED
   30 INS COL
   40 (SOL=30)
;


10 bonuses D
   20 MATCHED
   30 UPD COL
   40 (SOL=20) 
   20 NOT MATCHED
   30 INS COL
   40 (SOL=50)
;

I've tried using awk to match the sets but could not able to get the output as below - each block has some lines starts with 40 that need to be merged with line starts with 20 with AND and then 40 line needs to be deleted.

output.txt

Code:
10 bonuses D
   20 MATCHED AND (SOL=30)
   30 UPD COL
   20 NOT MATCHED AND (SOL=30)
   30 INS COL
;


10 bonuses D
   20 MATCHED AND (SOL=20)
   30 UPD COL
   20 NOT MATCHED AND (SOL=50)
   30 INS COL
;

I'm trying it to get it achieved. Please provide your valuable suggestions or approches to get it reach the expected output. Thanks!
# 2  
Old 02-08-2017
Are these sections really all formatted and separated like this? Or is it actually a lot messier?
# 3  
Old 02-08-2017
The structure of the file is same and each section is separated by space - Yes much longer than the example here but separated by space.
# 4  
Old 02-08-2017
Works for the data you showed:
Code:
BEGIN {
        RS=ORS="\n\n"
        FS=OFS="\n"
}

{
        A=B=0

        for(N=1; N<=NF; N++) {
                if(!A && $N ~ /^[ \t]*20[ \t]/) A=N
                if(!B && $N ~ /^[ \t]*40[ \t]/) B=N

                if(A && B)
                {
                        sub(/^[ \t]*40[ \t]+/, "", $B);
                        $A = $A " AND " $B

                        for(M=B; M<NF; M++) $M=$(M+1)
                        NF--;

                        A=B=0
                        N--
                }
        }
} 1

Code:
awk -f sect.awk data

10 bonuses D
   20 MATCHED AND (SOL=30)
   30 UPD COL
   20 NOT MATCHED AND (SOL=30)
   30 INS COL
;


10 bonuses D
   20 MATCHED AND (SOL=20)
   30 UPD COL
   20 NOT MATCHED AND (SOL=50)
   30 INS COL
;

$


Last edited by Corona688; 02-08-2017 at 02:20 PM.. Reason: Changed && to AND
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 02-08-2017
Another approach:
Code:
awk '{A[$1]=$0} $1==40{print A[20] " AND " $2 RS A[30]} $1!~/^[234]0$/' file



---
or
Code:
awk '{A[i=$1]=$0; $1=x} i==40{print A[20] " AND" $0 RS A[30]} i!~/^[234]0$/' file



---
sed approach:
Code:
sed '/^ *20 /{N; N; s/\(.*\)\(\n.*\)\n *40\(.*\)/\1 AND\3\2/;}' file

Whether this works, will depend on how much the sample differs from real life data...

Last edited by Scrutinizer; 02-08-2017 at 03:10 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-08-2017
How about
Code:
tac file | awk '$1 == 40 {TMP = $2} $1 == 20 {print $0, "AND",  TMP} $1 !~ /^[24]0$/' | tac
10 bonuses D
   20 MATCHED AND (SOL=30)
   30 UPD COL
   20 NOT MATCHED AND (SOL=30)
   30 INS COL
;


10 bonuses D
   20 MATCHED AND (SOL=20)
   30 UPD COL
   20 NOT MATCHED AND (SOL=50)
   30 INS COL
;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help - delete content inside square brackets under conditions

I have the file sed1.txt and I need to strip the brackets (]) and content inside them only when I have two or three letters followed by a colon. for example,it may be any letter, not just abc ] ] #-- cat sed1.txt 1 ] FISICA 2 ]PORTUGUES 3 ] ]MATEMATICA 4 ]]INGLES ] 5 ]QUIMICA 6... (2 Replies)
Discussion started by: dperboni
2 Replies

2. Shell Programming and Scripting

Change the file name and copy old file content to new file names.

Hi, I have a files in a directory as below :- ls -1 mqdepth-S1STC02 proc-mq-S1STC01 proc-mq-S1STC02 proc-mq-S1STC03 Whereever i have S1STC i need to copy them into new file with file name S2STC. expected output :- ls -1 mqdepth-S2STC02 proc-mq-S2STC01 proc-mq-S2STC02... (3 Replies)
Discussion started by: satishmallidi
3 Replies

3. Shell Programming and Scripting

Change a file content format using awk

Hi, i have a file input.txt Continent North America Country USA Capital Washington D.C. Country Canada Capital Ottawa Continent South America Country Argentina Capital Buenos Aires Country Brazil Capital Brasília Coutry Colombia Capital Bogotá and i want to get an output.txt ... (3 Replies)
Discussion started by: fastlane3000
3 Replies

4. Shell Programming and Scripting

Change file content 4 column to one Column using script

Hi Gurus, I have file content sample: ,5113955056,,TAgent-Suspend ,5119418233,,TAgent-Suspend ,5102119078,,TAgent-Suspend filenames 120229H5_suspend, 120229H6_unsuspend I receive those files one of directory /home/temp/ I need following: 1. Backup first /home/temp/ file to... (5 Replies)
Discussion started by: thepurple
5 Replies

5. Linux

when SCP, does file content change?

Good day to you all, Just want to check here, i know when scping a file, size might change due to space issue. it might sound silly, but does file content get change too? if so, what kind of situation that might be? (1 Reply)
Discussion started by: ahtat99
1 Replies

6. Shell Programming and Scripting

Scan and change file data content problem

Input file >Read_1 XXXXXXXXXXSDFXXXXXDS (condition 1: After the last "X" per line, if the distance is less than or equal to 3 letter, replace those not "X" letter with "X") TREXXXXXXXSDFXXXXXDS (condition 2: Before the first "X" per line, if the distance is less than or equal to 3 letter,... (12 Replies)
Discussion started by: patrick87
12 Replies

7. Shell Programming and Scripting

Change file content based on data

I have a Transaction File coming into the system. In this file, in all records the relevant data is as follows- Position 1:10 -> Transaction Code Position 252:255 -> 4 digit business code Now based on these 2 fields I have to alter value in Transaction code (Position 1:10)... (6 Replies)
Discussion started by: varunrbs
6 Replies

8. Shell Programming and Scripting

How to change a file's content by row?

Greetings. So the question is basically the same as it's in the title. I'd like to write a program that changes a file by rows. So to clarify it. (i know i shouldn't use code,/code here but i would like to separate it) So for example a text file looks like something like this: Happy... (5 Replies)
Discussion started by: buddhist
5 Replies

9. Shell Programming and Scripting

Need help to change the content for remote located file

Hi All, I have a file that sits on 4 diffrent servers, those servers are diffrent region based and they are authentication protected and that file has a diff port numbers, so when run the script it must ask my login details,region of server and port no for that file once it took from me it... (1 Reply)
Discussion started by: tmarjuna
1 Replies

10. UNIX for Advanced & Expert Users

Need help to change the content for remote located file

Hi All, I have one file that sits on 4 diffrent servers, those servers are diffrent region based and they are authentication protected and that file has a diff port numbers, so when run the script it must ask my login details,region of server and port no for that file once it took from me... (1 Reply)
Discussion started by: tmarjuna
1 Replies
Login or Register to Ask a Question