Printing lines with non-redundant itens in the output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing lines with non-redundant itens in the output
# 1  
Old 07-02-2012
Printing lines with non-redundant itens in the output

Hi all,
I'm trying to select lines of a file and at the end do not have redundant itens:

Input
A_B
K_A
C_T
A_O
U_B
P_C
D_F
Z_G
W_U

Output
A_B
C_T
D_F
Z_G
W_U

Note: You can see that the output does not have the itens K_A, A_O, U_B and P_C because A, B and C were present at least one time in output. Moreover, the W_U is present because U is combined with W and B, but in the later case the letter B was alread present in the output. In other words, each item must be present just one time in the output.
I tried a lot of things, but I was not able to rigth this script.

Regards

V
# 2  
Old 07-02-2012
Have a go with this:

Code:
awk '
    {
        split( $1, a, "_" );
        if( !printed[a[1]]  && !printed[a[2]] ) 
        {
            printed[a[1]] = printed[a[2]] = 1;
            print;
        }
    }
' input-file >output-file

# 3  
Old 07-03-2012
A much less readable and maintainable version of agama's solution which is guaranteed to make whoever inherits the code mutter under their breath:
Code:
awk -F_ '!a[$1] && !a[$2] && ++a[$1] && ++a[$2]' file

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Printing out lines that have the same value in the first column but different value in the second

Hi, I have a text file that looks like the following: ILMN_1343291 6 74341083 74341772 ILMN_1343291 6 74341195 74341099 ILMN_1343295 12 6387581 6387650 ILMN_1651209 1 1657001 1657050 ILMN_1651209 5 83524260 83524309 I... (1 Reply)
Discussion started by: evelibertine
1 Replies

2. Shell Programming and Scripting

printing lines before and after a record

Hello Everyone, I want to print out the records after and before a certain record. I am able to figure out how to print that particular record but not the ones before and after. Looking for some advice Thank you (6 Replies)
Discussion started by: danish0909
6 Replies

3. Shell Programming and Scripting

Printing the lines that appear in an other file, and the three lines after them

Hi ! I need some help with a script I am writing. I am trying to compare two files, the first file being in this format : Header1 Text1-1 Text1-2 Text1-3 Header2 Text2-1 etc... For each header, I want to check if it appears in the second file, and if it is the case print the header... (4 Replies)
Discussion started by: jbi
4 Replies

4. Shell Programming and Scripting

Printing all lines before a specific string and a custom message 2 lines after

Hello all, I need to print all the lines before a specific string and print a custom message 2 lines after that. So far I have managed to print everything up the string, inclusively, but I can't figure out how to print the 2 lines after that and the custom message. My code thus far is:... (4 Replies)
Discussion started by: SEinT
4 Replies

5. Shell Programming and Scripting

Help required on grep command(Skip the first few lines from printing in the output)

Hi experts I want the proper argument to the grep command so that I need to skip the first few lines(say first 10 lines) and print all the remaining instances of the grep output. I tried to use grep -m 10 "search text" file*. But this gives the first 10 instances(lines) of the search string.... (7 Replies)
Discussion started by: ks_reddy
7 Replies

6. Shell Programming and Scripting

Printing coloured lines

I want to print "Hello,How are you" in green colour using printf. How to do so? (5 Replies)
Discussion started by: proactiveaditya
5 Replies

7. Shell Programming and Scripting

selective printing of lines

Hi all , i need to grep for a string in a text file and print the string and the 3rd line above it. As always , Thanks. (4 Replies)
Discussion started by: okiedokie
4 Replies

8. Shell Programming and Scripting

Printing specified lines only

hi, i m having 2 files say F1 and F2. there are some joining conditions like: Column 4 from F1= Column 29 from F2; Column 10 from F1= Column 165 in F2; and if value in column 8 from F2='3' than i should get Column 4,5,8,10 from F1 and 29,165 from F2. I cant provide the files. but the... (4 Replies)
Discussion started by: Mohit623
4 Replies

9. Shell Programming and Scripting

printing output more than 13

i want to print the idle time of the users more than 10 days. for eg: my "w" command output is like below. -sh-3.00$ w 03:47:41 up 13 days, 16:59, 3 users, load average: 10.00, 10.00, 10.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root :0 - ... (2 Replies)
Discussion started by: Krrishv
2 Replies

10. Programming

Suppress last N lines printing

Hi, I want to know different ways of suppressing printing of last N lines. Can anyone help? Thanks, Sree (1 Reply)
Discussion started by: chakri400
1 Replies
Login or Register to Ask a Question