Egrep how to make sure string is after 5 comma


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Egrep how to make sure string is after 5 comma
# 1  
Old 10-10-2013
IBM Egrep how to make sure string is after 5 comma

Hello,

Need help...

using egrep how do I make sure string is after 5th comma

example:
Code:
a,b,c,d,e,f,g,h,i

Suppose i want to search letter f but want to make sure it is after 5th comma.
Is there any way to check string is after 5th comma?

Thanks !!

Last edited by radoulov; 10-10-2013 at 06:16 AM..
# 2  
Old 10-10-2013
This will print the line if the letter "f" is in field 6 or more:
Code:
echo a,b,c,d,e,f,g,h,i | awk -F, '{ p=0; for (i=6; i<=NF; i++) { if ($i ~ /f/) p=1 } } p'


Last edited by Subbeh; 10-10-2013 at 04:21 AM..
This User Gave Thanks to Subbeh For This Post:
# 3  
Old 10-10-2013
Thanks but i want to use only egrep for this.
# 4  
Old 10-10-2013
Not sure if this is the best way to do it, but it works:
Code:
egrep '.*?,.*?,.*?,.*?,.*?,.*f' file

Edit: This is a better way to approach it:
Code:
grep '^\([^,]*,\)\{5\}\(.*f\)' file


Last edited by Subbeh; 10-10-2013 at 05:23 AM..
This User Gave Thanks to Subbeh For This Post:
# 5  
Old 10-10-2013
Why not use awk???
awk is a good tool for this job. Here is another version.
Code:
echo a,b,c,d,e,f,g,h,i | awk 'NR>5 && $1~/f/ {print "yes"}' RS=,
yes

This prints yes if an f is found after 5th ,
This User Gave Thanks to Jotne For This Post:
# 6  
Old 10-10-2013
Somehow cleaner with extended grep (egrep):
Code:
grep -E '(.*,){5,}f' infile

If you're on Solaris/Oracle OS use /usr/xpg4/bin/grep.
# 7  
Old 10-10-2013
Why not using cut : is it home work ?

Code:
cut -d, -f6- infile


Last edited by ctsgnb; 10-10-2013 at 06:51 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Make multiple lines into single quoted comma separated Linux

Hi, I want to change a file file1.txt: 1234 3456 2345 6789 3456 2333 4444 As, file2.txt in Linux: '1234','3456','2345','6789','3456','2333','4444' Could someone please help me. (Single liner sed, awk will be welcome!) (7 Replies)
Discussion started by: wiweq05
7 Replies

3. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

4. Shell Programming and Scripting

Replace a value after 13th comma in a string

Suppose b=50,0,0,40,1,0,5000,gold,0,0,0,0,32,9,2,0,10000,0,0,0,0,0,0,0,0,0,BSNL_SMS_Bundle ,null,null,null,null,null,null,null,null,null,0,0,0,0,0,0,0,0,0,0,50,0,0,0,0,0,0,0,0,0,null,null,0,0,405564245,0 c=11 After 13th comma, the value of 9 needs to be changed and to be filled by another... (4 Replies)
Discussion started by: karan23kohli
4 Replies

5. Shell Programming and Scripting

Awk: Remove comma at the end of the string

Hi i had String like UID: ABC345QWE678GFK345SA90, LENGTH 32 when I used awk ' FS, {print $1}' prints ABC345QWE678GFK345SA90, how can i getrid of that coma at the end of the string. Thanks in advance.. (14 Replies)
Discussion started by: Reddy482
14 Replies

6. Shell Programming and Scripting

egrep string except when preceded by underscore

having trouble with this seemingly simple taks.. :mad: Test data: we want to keep lines 2,3 & 4 -- want to exclude when ${.*schema} is preceded with an underscore; Valid {.*schema} should always be preceded spaces or be found at the beginning of a line. egrep... (5 Replies)
Discussion started by: danmauer
5 Replies

7. Shell Programming and Scripting

Delete a comma from string

Hey guys, Its a simple question though, but since I'm new to this shell scripting world ... it's kind of difficult. Say I have some string as : ListenAddress=ABCServer1,ABCServer2 I want to output the value of ListenAddress as ... ABCServer1 ABCServer2 so, basically, I want to... (6 Replies)
Discussion started by: MisterKhan
6 Replies

8. Shell Programming and Scripting

comma separated string manipulation

hi, i have a script where i am accepting a comma separated string from the user, i have to separated those strings on the basis of comma and store it in variables.. below is the script #!/bin/ksh clear echo "Enter the strings seperated by commas :- \c " read strn echo $strn... (2 Replies)
Discussion started by: saharookiedba
2 Replies

9. Shell Programming and Scripting

how to make menu of result from egrep

hi, im just starting with scripting , i have de following situation im doing a search in a data folder with egrep egrep -i "title.regu." `find . -name "*.dat"` the result is : ./20080816212245/index.dat:title Regular Expressions ./20080811212216/index.dat:title ... (5 Replies)
Discussion started by: tvrman
5 Replies

10. Shell Programming and Scripting

Comma Delimited Output w/ egrep

Hi all, I have an input file that I am pulling out certain phases using the following commands: cat /nodes.txt | egrep -e 'OSVersion|PrimaryNodeName' Currently the output looks like this: OSVersion - 5.0 PrimaryNodeName - serverA OSVersion - 5.0 PrimaryNodeName - serverB OSVersion... (2 Replies)
Discussion started by: indianadoug
2 Replies
Login or Register to Ask a Question