Weird requirement using egrep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Weird requirement using egrep
# 8  
Old 07-21-2011
Since there is only one line in the file, grep outputs only once though you give multiple patterns to be searched. Even if you have the same line twice in the file, grep matches the first pattern and ignores the 2nd pattern as your giving the OR condition 'x.*|y.*'. And IMO i dont think even Sed or AWK would do the job since they output based on the input line they read. If you still need such output then Smilie
Code:
grep -o 'y.*' inputfile; grep -o 'x.*' inputfile

# 9  
Old 07-21-2011
Code:
 
bash-3.00$ ./test.ksh 
x,y,z,abc)
y,z,abc)

bash-3.00$ cat test
ABCDE (x,y,z,abc)

bash-3.00$ cat test.ksh
nawk ' /x/ {print substr($0,index($0,"x"),length($0))} /y/ {print substr($0,index($0,"y"),length($0))}' test

# 10  
Old 07-21-2011
Thanks for the solution
but x and y are dynamic it could have more values.
Basically 'x.*|y.*' is an argument to the script.
Tomorrow it could be 'x.*|y.*|z.*'
Hope you are getting my requirement.
# 11  
Old 07-21-2011
Code:
sed -n 'h; s/^.*\(x.*\).*$/\1/p; x; s/^.*\(y.*\).*$/\1/p' INPUTFILE

You don't need ".*$" for yours patterns but it's a more common solution.

Better:
Code:
sed -n 'h; s/^.*\(x.*\).*$/\1/p; g; s/^.*\(y.*\).*$/\1/p' INPUTFILE

g works for more than two patterns:
Code:
h; s/.../.../p; g; s...; g; s... ; ...


Last edited by yazu; 07-21-2011 at 05:23 AM.. Reason: better
# 12  
Old 07-21-2011
However..just a try but not a robust solution
Code:
sed -e  's/.*\(x.*\)/\1/p' -e 's/.*\(y.*\)/\1/' inputfile

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. Shell Programming and Scripting

UNIX - requirement

Hi All, I have a source file with data 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. ... (8 Replies)
Discussion started by: morbid_angel
8 Replies

3. 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

4. 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

5. 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

6. 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

7. UNIX for Dummies Questions & Answers

search ")" with egrep - egrep: syntax error

Hi Guys, we have a shell script which basically query the Database which retrieves huge data and use the data with "egrep" . Now there is some data which contains characters like "abc)" and the same is used like below : "egrep (.+\|GDPRAB16\|GDPR/11702 96 abc)\|$ temp.txt" now while... (7 Replies)
Discussion started by: sagarjani
7 Replies

8. UNIX for Dummies Questions & Answers

Egrep cheat sheet anywhere? Looking for meaning of egrep -c

Hi I've been searching google and have not found what egrep -c means. Does anyone know where I can get a cheat sheet or what that -c means? thanks, Linda (2 Replies)
Discussion started by: leelm
2 Replies
Login or Register to Ask a Question