Organization data based on two conditions applied problem asking...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Organization data based on two conditions applied problem asking...
# 1  
Old 04-01-2010
Organization data based on two conditions applied problem asking...

Input file:
Code:
HS04636  type    header  836     7001    ID=g1
HS04636  type    status  836     1017    Parent=g1.t1
HS04636  type    location        966     1017    ID=g1.t1.cds;Parent=g1.t1
HS04636  type    location        1818    1934    ID=g1.t1.cds;Parent=g1.t1
HS04636  type    status  1818    1934    Parent=g1.t1
HS04636  type    location        2055    2198    ID=g1.t1.cds;Parent=g1.t1
HS04636  type    status  2055    2198    Parent=g1.t1
HS08198  type    header  421     2105    ID=g2
HS08198  type    status  421     582     Parent=g2.t1
HS08198  type    location        445     582     ID=g2.t1.cds;Parent=g2.t1
HS08198  type    location        758     894     ID=g2.t1.cds;Parent=g2.t1
HS08198  type    status  758     894     Parent=g2.t1
.
.

Desired output file:
Code:
HS04636  type    header  836     7001    ID=g1
HS04636  type    status  836     1017    Parent=g1.t1
HS04636  type    location        966     1017    ID=g1.t1.cds;Parent=g1.t1
HS04636  type    status  1818    1934    Parent=g1.t1
HS04636  type    location        1818    1934    ID=g1.t1.cds;Parent=g1.t1
HS04636  type    status  2055    2198    Parent=g1.t1
HS04636  type    location        2055    2198    ID=g1.t1.cds;Parent=g1.t1

HS08198  type    header  421     2105    ID=g2
HS08198  type    status  421     582     Parent=g2.t1
HS08198  type    location        445     582     ID=g2.t1.cds;Parent=g2.t1
HS08198  type    status  758     894     Parent=g2.t1
HS08198  type    location        758     894     ID=g2.t1.cds;Parent=g2.t1
.
.

Two condition to follow:
1. Add a newline before "header" word in column 3;
2. When column 3 is "status" and "location", either one of the content of column 4 and column 5 of them is same. Then must be shown the content of "status" before the content "location" word.
Thanks a lot for any advice to solve this troubles.
# 2  
Old 04-01-2010
Patrick,
I am not getting your query. I am also not able to find difference between i/p file & o/p file. Can you explain little more?

-Nithin.
# 3  
Old 04-01-2010
Hi bsnithin, actually the input and output file are exactly the same.
The difference just their arrangement in input and output file.
If you notice, you will find out that based on the 2 conditions. Desired output is start with "header", then must be follow by "status", "location"
Hope you can get what I mean Smilie
# 4  
Old 04-02-2010
A bash working example. Using a similar logic awk experts could certainly make it shorter Smilie
It works by filling an array from a header to another and then to the end.
#!/bin/bash
Code:
PrintOutput()    {
    for ((i=0; i<${#OUT[@]}; i++))
    do    echo -e "${OUT[$i]}"
    done
    unset OUT; i=0
}
i=0
while read IN
do
    N1=$(echo "$IN" | awk '{print $4}')
    N2=$(echo "$IN" | awk '{print $5}')
    case $(echo "$IN" | awk '{print $3}') in
        header)
            ((i)) && PrintOutput
        ;;
        location)
            M1=$(echo "$IN" | awk '{print $4}')
            M2=$(echo "$IN" | awk '{print $5}')
        ;;
        status)
            if [ "$N1" = "$M1" ] || [ "$N2" = "$M2" ]
            then
                OUT[$i]="${OUT[$((i-1))]}"
                OUT[$((i-1))]="$IN"
                ((i++)); continue
            fi
            unset M1 M2
        ;;
    esac
    OUT[$i]="$IN"
    ((i++))
done < infile
PrintOutput

# 5  
Old 04-02-2010
Hi frans,
Thanks for your reply. I just try your script.
But it seems like no work?
Thanks to correct my mistakes ^^
# 6  
Old 04-02-2010
Quote:
Originally Posted by patrick87
Hi frans,
Thanks for your reply. I just try your script.
But it seems like no work?
What happens exactly, because 'not work' is a bit unprecise...
# 7  
Old 04-02-2010
After running the script, it seems like still print out the contents exactly like my previous input file Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need script for making files based on some conditions.

Hi All, I have a text file (code_data.txt) with the followig data. AMAR AB123456 XYZ KIRAN CB789 ABC RAJ CS78890 XYZ KAMESH A33535335 ABC KUMAR MD678894 MAT RITESH SR3535355... (26 Replies)
Discussion started by: ROCK_PLSQL
26 Replies

2. Shell Programming and Scripting

Help with Creating file based on conditions

Can anyone please assist? I have a .txt file(File1.txt) and a property file(propertyfile.txt) . I have to read the vales from the property file and .txt file and create the output file(outputfile.txt) mentioned in the attachment. For each record in .txt file,the below mentioned values shall be... (20 Replies)
Discussion started by: vinus
20 Replies

3. Shell Programming and Scripting

Split File based on different conditions

I need to split the file Conditions: Ignore any record that either starts with 1 or 9 Split the file at position 404 , if position 404 is abc or def then write all the records in a file > File 1 , the remaining records should go in to a file > File 2 Further I want to split the... (7 Replies)
Discussion started by: protech
7 Replies

4. UNIX for Dummies Questions & Answers

Shell script to extract data from csv file based on certain conditions

Hi Guys, I am new to shell script.I need your help to write a shell script. I need to write a shell script to extract data from a .csv file where columns are ',' separated. The file has 5 columns having values say column 1,column 2.....column 5 as below along with their valuesm.... (1 Reply)
Discussion started by: Vivekit82
1 Replies

5. Shell Programming and Scripting

Report generation based on certain conditions

Hi I recently joined a project where I have been asked to generate a report using shell script accessing UNIX box. I have no idea on how to do it as I am a beginner and learning shell scripts. Suppose I have a XML: Code: ... (3 Replies)
Discussion started by: vat1kor
3 Replies

6. Shell Programming and Scripting

Help with re-organization data

Input file DATA2.2 POSITION_152486.2 COLUMN689699.2 DATA2.2 ROW00000342066 UNIT00000342313 DATA7.2 POSITION_017891.4 COLUMN060361.4 DATA7.2 ROW00000379319 UNIT00000368623 DATA7.2 ROW00000421241 UNIT00000400736 DATA8.1 POSITION_153254.2 COLUMN694986.2 DATA8.1 ROW00000379288... (1 Reply)
Discussion started by: perl_beginner
1 Replies

7. Shell Programming and Scripting

Help with sort data based on descending order problem

Input file 9.99331e-13 8.98451e-65 9.98418e-34 7.98319e-08 365592 111669 74942.9 0 Desired output 365592 111669 74942.9 7.98319e-08 1.99331e-13 6.98418e-34 (2 Replies)
Discussion started by: perl_beginner
2 Replies

8. Shell Programming and Scripting

validating a file based on conditions

i have a file in unix in which the records are like this aaa 123 233 aaa 234 222 aaa 242 222 bbb 122 111 bbb 122 123 ccc 124 222 In the output i want only the below records aaa ccc The validation logic is 1st column and 2nd column need to be considered if both columns values are... (8 Replies)
Discussion started by: trichyselva
8 Replies

9. Shell Programming and Scripting

extract lines based on few conditions

Hi, I need to extract lines based on some conditions as explained below: File format details: notes: 1. each set starts with AAA only 2. number of columns is fixed 3. number of rows per set may vary (as one set is upto DDD - 4 rows) Now, if any BBB's 5th column is blank then then... (4 Replies)
Discussion started by: prvnrk
4 Replies
Login or Register to Ask a Question