shell script to format file based on specific patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to format file based on specific patterns
# 1  
Old 06-17-2009
shell script to format file based on specific patterns

Please help me out and drag me out the deadlock I am stuck into:

I have a file. I want the statements under a if...then condition be listed in a separate file in the manner condition|statement.Following are the different input pattern and corresponding output parameters.any generic code to handle all these would be very very helpful.

Case:1 INPUT
------
if cond1 = true then
parm1=a
parm2=b
parm3=c
if cond2 = true then
parm3=d
parm4=e
end if
end if

OUTPUT
-------
cond1|parm1
cond1|parm2
cond1|parm3
cond2|parm3
cond2|parm4


Case:2 INPUT
------
if cond1 = true then
if cond2 = true then
parm1=a
parm2=b
parm3=c
parm3=d
parm4=e
end if
end if

OUTPUT
-------
cond1|cond2|parm1
cond1|cond2|parm2
cond1|cond2|parm3
cond1|cond2|parm3
cond1|cond2|parm4


Case:3 INPUT
------
if cond1 = true then
if cond2 = true then
end if
parm1=a
parm2=b
parm3=c
parm3=d
parm4=e
end if

OUTPUT
-------
cond1|parm1
cond1|parm2
cond1|parm3
cond1|parm3
cond1|parm4
# 2  
Old 06-17-2009
what have you tried till now??
# 3  
Old 06-18-2009
Can you explain just a little more?

You mention you want the statements to be in a separate file. Are you wanting to read or write to the file? Are these multiple files or a single? Are the parameters command line options or a menu response?

Without the code logic can you explain what you're looking for as far as files and the script command line?

Sorry for all the questions, but the general if..then of the code examples is self-explanatory, but I'm not understanding the ultimate goal here.
# 4  
Old 06-18-2009
Quote:
Originally Posted by bwhitehd
Can you explain just a little more?

You mention you want the statements to be in a separate file. Are you wanting to read or write to the file? Are these multiple files or a single? Are the parameters command line options or a menu response?

Without the code logic can you explain what you're looking for as far as files and the script command line?

Sorry for all the questions, but the general if..then of the code examples is self-explanatory, but I'm not understanding the ultimate goal here.
okay, the intent is to write to a file ( specified as "OUTPUT" ) in original post from the file ( specified as "INPUT" ). There are no command line options or a menu response. Everything is file content and can be treated as records of the file. Did this answer your question ?
# 5  
Old 06-18-2009
Quote:
Originally Posted by vidyadhar85
what have you tried till now??
I am really new to awk and cannot figure it out where I am making the mistake. I tried to handle a situation in which the input file looks like this:

file.txt
------
cond=cond1
cond=cond2
parm=parm1
parm=parm2
parm=parm3
cond=cond3
parm=parm4
parm=parm5

The script which I wrote is given below but it hangs:'

awk '{ x=1
b=0
while ( $x <= NF ) {
if ( $x ~ /cond=/ ) {
if b = 1 {
cond = "" }
cond = cond "|" $x }
else if ( $x ~ /parm=/ ) {
b = 1
parm = cond "|" parm "|" $x
print parm }
x++
}
}' file.txt

I would like to have a generic solution for all the cases.
# 6  
Old 06-18-2009
Yes and No. Can you provide a sample of the input file?

From what I understand so far, a script reads in each line of an input file, then writes to an output file based on the if conditions. Will all 3 cases be within the same script? Will the output be written to the same file and combined in some way or different files? Do you have a preference in language? (bash, perl, ...)

psuedo code for case 1:
Code:
# assuming inputfile format of "value1 value2"
while read -u inputfile var1 var2
do
  if condition1; then
    parm1=a
    parm2=b
    parm3=c
    if condition2; then
      parm3=d
      parm4=e
    fi
    for num in {1..4}
    do
      printf "cond1=%s|cond2%s|parm%s=%s\n" $cond1 $cond2 $num ${parm${num}} >> outputfile
    done
fi

# 7  
Old 06-18-2009
Quote:
Originally Posted by bwhitehd
Yes and No. Can you provide a sample of the input file?

From what I understand so far, a script reads in each line of an input file, then writes to an output file based on the if conditions. Will all 3 cases be within the same script? Will the output be written to the same file and combined in some way or different files? Do you have a preference in language? (bash, perl, ...)

psuedo code for case 1:
Code:
# assuming inputfile format of "value1 value2"
while read -u inputfile var1 var2
do
  if condition1; then
    parm1=a
    parm2=b
    parm3=c
    if condition2; then
      parm3=d
      parm4=e
    fi
    for num in {1..4}
    do
      printf "cond1=%s|cond2%s|parm%s=%s\n" $cond1 $cond2 $num ${parm${num}} >> outputfile
    done
fi

Probably my question is not clear. Here is what I want to do. I want to parse a file with if then....end if blocks. I want to have the if condition added in front of the statements which are within the if..end if block e.g.

in the simplest form

if cond1 then
statement1
statement2
end if

So I want a new file of the form:
cond1 | statement1
cond1 | statement2

So, now if we have a nested if...end if block
if cond1 then
if cond2 then
statement1
statement2
end if
end if

So the new file should have:

cond1|cond2|statement1
cond1|cond2|statement2.

Did it explain the problem scenario ? Is it possible using korn shell ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Linux/Shell script - How to compare 2 arrays based on patterns and get the differences

I have FILE 1 (This file has all master columns/headers) A|B|C|D|E|F|G|H|STATUS FILE 2 A|C|F|I|OFF_STATUS 3|4|5|4|Y 6|7|8|5|Y Below command give me all headers of FILE 2 into array2.txt file paste <(head -1 FILE2.txt | tr '|' '\n')>array2.txt So I would like to compare... (2 Replies)
Discussion started by: jmadhams
2 Replies

2. UNIX for Beginners Questions & Answers

Using grep to select specific patterns in text file?

How do I use grep to select words that start with I or O, end in box, and contain at least one letter in between them? the text file mailinfo.txt contains Inbox the Inbox Is a match box Doesn't match INBOX Outbox Outbox1 InbOX Ibox I box If the command works correctly it... (4 Replies)
Discussion started by: steezuschrist96
4 Replies

3. UNIX for Dummies Questions & Answers

File merging based on column patterns

Hello :) I am in this situation: Input: two tab-delimited files, `File1` and `File2`. `File2` (`$2`) has to be parsed by patterns found in `File1` (`$1`). Expected output: tab-delimited file, `File3`. `File3` has to contain the same rows as `File2`, plus the corresponding value in... (5 Replies)
Discussion started by: dovah
5 Replies

4. Shell Programming and Scripting

Bash shell script not working-picking segment patterns from a file

Hi All, I have to pick particular segments from a file and I have prepared below shell script.But its not working and I am not able to find out whats the issue.could you guys pls help? Sample file: TS3*1451575*12*20151231*4*482.44 NM1*QC*1*CUTLER*BETTY DTM*472*20150808... (4 Replies)
Discussion started by: Venkata Prasad
4 Replies

5. Shell Programming and Scripting

script to arrange file in specific format

Hi All, I am new to forum, I am looking to arrange a file in specific format but unable to get the formula to do it, already googled for the same, but didnt find the answer :(. hope to get help here :o:o:o:o:o I have to files : $ cat Dev_List2 0685 0686 0687 0688 0689 068A 068B 068C... (2 Replies)
Discussion started by: prasan_Aix
2 Replies

6. UNIX for Dummies Questions & Answers

Locating and Extracting Specific Patterns from a file

Hi all, 1. I have a file that is getting continously refreshed (appended) I want to grep all the strings containing substring of the type abcdf123@aaa.xxx.yyy.zzz:portnumber: where, before @, any letters or numbers combination, after @, IP address then symbol : then port... (4 Replies)
Discussion started by: kokoras
4 Replies

7. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

8. Shell Programming and Scripting

segregate the file based on matching patterns

print 'test' SETUSER 'dbo' go create proc abc as /Some code here/ go SETUSER go print 'test1' SETUSER 'dbo' go Create Procedure xyz as /some code here/ go SETUSER go print 'test2' SETUSER 'dbo' (2 Replies)
Discussion started by: mad_man12
2 Replies

9. Shell Programming and Scripting

Splitting a file based on two patterns

Hi there, I've an input file as follows: *START 1001 a1 1002 a2 1003 a3 1004 a4 *END *START 1001 b1 1002 b2 1004 b4 *END *START 1001 c1 1004 c4 *END (6 Replies)
Discussion started by: kbirde
6 Replies

10. Shell Programming and Scripting

script to edit strings based on patterns

Hello All, Here is the file which I want to edit. The script should look for DB2 and if found then delete all lines related to DB2 connection string. Is there way this can be done using script ? DB1 = (DESCRIPTION = (SDU = 32768 (enable = broken) (ADDRESS = (PROTOCOL =... (2 Replies)
Discussion started by: deepakc_in
2 Replies
Login or Register to Ask a Question