UNIX - requirement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX - requirement
# 1  
Old 06-17-2013
UNIX - requirement

Hi All,
I have a source file with data

Code:
Name ~ Groups
Muni~abc,was,USA_ax,123
Chaitanya~USA_12,was
Balaji~123,xyz,was
Ramu~123,xyz

From the second column i want to extract only the groups that matches the pattern 'USA_%' or if the group = 'was', and ignore any other columns.


********Expected output ***********
Code:
Name | Groups
Muni|was,USA_ax
Chaitanya|USA_12,was
Balaji|was
Ramu|

How can I do this in UNIX

Last edited by Scrutinizer; 06-17-2013 at 12:45 AM.. Reason: code tags for data samples
# 2  
Old 06-17-2013
What have you tried so far? Share your thoughts to solve this problem.
# 3  
Old 06-17-2013
I am not have any idea about this .Thinking how can I do this by grep
# 4  
Old 06-17-2013
grep is definitely not an option as I see your expected output has different field separator.

I would suggest using awk instead:
Code:
awk -F'[~,]' '
        NR == 1 {
                sub ( /~/, "|" )
        }
        NR > 1 {
                s = $1 "|"
                for( i = 2; i <= NF; i++ )
                {
                        if( $i == "was" || $i ~ /USA.*/ )
                                s = s $i OFS
                }
                sub( /,$/, X, s )
                $0 = s
        }
        1
' OFS=, file

# 5  
Old 06-17-2013
It cannot be done with grep, because the input needs to be transformed..
# 6  
Old 06-17-2013
great logic. Thanks a lot

---------- Post updated at 12:50 PM ---------- Previous update was at 12:26 AM ----------

i hope for removing last character "," you have used this
Code:
sub( /,$/, X, s )

what is the purpose of 1 here .just curious to know the logic .Is it for printing ?

Code:
  NR > 1 {
                s = $1 "|"
                for( i = 2; i <= NF; i++ )
                {
                        if( $i == "was" || $i ~ /USA.*/ )
                                s = s $i OFS
                }
                sub( /,$/, X, s )
                $0 = s
        }
        1


Last edited by Scrutinizer; 06-17-2013 at 02:52 PM.. Reason: code tags
# 7  
Old 06-17-2013
Yes, it is for printing. It's a logical expression that determines whether a line gets printed or not. It could be any expression technically, but a '1' makes it always print.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX requirement

Hi Everyone, Can anyone of you help me on how to get files creation time on ftp server/Remote server in unix. Regards, Avinash. (6 Replies)
Discussion started by: Avinash varma
6 Replies

2. UNIX for Dummies Questions & Answers

Script requirement

Please help in creating script for below requirement. I will be running 1 command and will get below entries in a text file say file44.txt ******************************* DFDL1005 06:30 00:05 ABFL2003 10/22 01:10 CFTL1256 10/24 00:10 10/25 09:20 PM ******************************** .... (3 Replies)
Discussion started by: Vinay_3308
3 Replies

3. Shell Programming and Scripting

Requirement of Spliting a text file in UNIX Programing

Hi, There is a requirement, needs to split the text file based on RC code present in text file. For this, needs to write a unix shell programing script for the above requirement. For example in text file, if there are distinct RC codes, then we needs to split into multiple text files. In... (1 Reply)
Discussion started by: Chandra2678
1 Replies

4. Shell Programming and Scripting

Looping requirement

Hi all, I have little working knowledge in unix shell scripting. I have a requirement where i need to pull out some data in between the strings in the file. Input: TEST a a c f d TEST f e g g TEST Output: (7 Replies)
Discussion started by: satyasrin82
7 Replies

5. Shell Programming and Scripting

URGENT REQUIREMENT

1.Write an automated shell program(s) that can create, monitor the log files and report the issues for matching pattern. (i) Conditions for creating log files. Log file is created with date (example 2010_03_27.log). If the log file size is 10 Mb for a particular day then automatically the log... (3 Replies)
Discussion started by: praveen12
3 Replies

6. Shell Programming and Scripting

Requirement

I am trying to script and came up with a conclusion that I need a do while loop in my statement. I am stuck with the do while syntax. I need to use it alongwith the if then else statement. Can I use it is a big question? I actually need to get all the files that are there from within run_dt to... (1 Reply)
Discussion started by: aronmelon
1 Replies

7. AIX

SPOT requirement

Hey May be a dumb question Can I use a SPOT which is at 5.3 TL6 to boot an LPAR (with 5.3 TL8) in to maintenance mode? Will it work ? Is it mandatory that SPOT should be of same or higher version in such case? Bala (1 Reply)
Discussion started by: balaji_prk
1 Replies

8. UNIX for Dummies Questions & Answers

system requirement for unix

I want to try running the free down load from sun, their solaris 8. I have an intel pentium 3, 550 mhz, 8 GB drive, with 128 ram. I this anough to run a unix os like solaris 8. Also is their a browser that I could use with solaris8 so as to connect to the internet. Does ie have a version for unix.... (3 Replies)
Discussion started by: gparsons70
3 Replies
Login or Register to Ask a Question