Grep with Regular expression now working on file directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep with Regular expression now working on file directories
# 1  
Old 12-30-2015
Grep with Regular expression now working on file directories

Hello Everyone,

I have a file sam1 with the below content

Code:
SYSYSID;MANDT;/SIE/AD_Z0M_INDX;/SIE/AD_Z0M_KEY1

echo $Regex
\bSYSYSID\b|\bMANDT\b|\b/SIE/AD_Z0M_INDX\b|\b/SIE/AD_Z0M_KEY1\b

cat sam1 | grep -Eo $Regex

I expect the result as
Code:
SYSYSID
MANDT
/SIE/AD_Z0M_INDX
/SIE/AD_Z0M_KEY1

But the command is giving the result as

Code:
SYSYSID
MANDT

Any idea why? I tried to escape forward slash in "/SIE/AD_Z0M_INDX;/SIE/AD_Z0M_KEY1" but still no luck

Regards,
Sam

Last edited by Scrutinizer; 12-30-2015 at 08:15 AM.. Reason: CODE TAGS
# 2  
Old 12-30-2015
Try:
Code:
sed s,";","\n",g sam1

hth

EDIT:

And to save it in the same file:
Code:
sed s,";","\n",g -i sam1

Or, to save as another file:
Code:
sed s,";","\n",g sam1 > sam2


Last edited by sea; 12-30-2015 at 08:00 AM.. Reason: Removed the unrequired '\b' from $regex ;)
# 3  
Old 12-30-2015
With GNU grep, \b matches the empty string at the edge of a word. In the example the \b before the / is not at the edge of a word, because in the example SYSYSID;MANDT;/SIE/AD_Z0M_INDX;/SIE/AD_Z0M_KEY1 it is between a ; and a /. So try:
Code:
Regex='\bSYSYSID\b|\bMANDT\b|\B/SIE/AD_Z0M_INDX\b|\B/SIE/AD_Z0M_KEY1\b'

instead. Then

Code:
grep -Eo "$Regex" sam1

should produce:
Code:
SYSYSID
MANDT
/SIE/AD_Z0M_INDX
/SIE/AD_Z0M_KEY1

# 4  
Old 01-08-2016
Hello ,

Sorry for the delay in response. I tried with \B but no luck..Smilie
Still showing only the first 2 records. I am trying to check with a new file

Regards,
Sumith
# 5  
Old 01-08-2016
What operating system and shell are you using? (As Scrutinizer said, the code he suggested with work with GNU grep. If you're not using a Linux system, there is a good chance that the grep you're using doesn't understand \b or \B.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

SHELL: UNIX : Ls regular expression not working when used with variables

If i do below command in unix prompt which static values (ie 27..97), it is working fine and gives desired output >ls -d $WORKDIR/batch/somefilename_{27..97}.* 2>/dev/null somefilename_27.sometxt somefilename_28.sometxt somefilename_29.sometxt .. somefilename_97.sometxt But if i want... (2 Replies)
Discussion started by: haiderali
2 Replies

2. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

3. Shell Programming and Scripting

Regular Expression not working with sed

Hi All, I am facing some problems with regular expression with sed. I have a .txt file with the contents as below: This is a dummy file # File created to test execution of regular expression. Hope it works out. As in the above contents there is a blank line which does not... (4 Replies)
Discussion started by: bghosh
4 Replies

4. Shell Programming and Scripting

Help with grep / regular expression

Hi, Input file: -13- -1er- -1xyz1- -1xz12- -2ab1- -2ab2-- -143- Code: grep '^*\-' input.txt Wrong output: -13- -1xyz1- -2ab1- -2ab2-- (4 Replies)
Discussion started by: dragon.1431
4 Replies

5. Shell Programming and Scripting

Using grep and regular expression to find class references in a c++ file

I'm trying to math all class references in a C++ file using grep with regular expression. I'm trying to know if a specific include is usuless or not, so I have to know if there is a refence in cpp. I wrote this RE that searches for a reference from class ABCZ, but unfortunately it isn't working... (0 Replies)
Discussion started by: passerby
0 Replies

6. Shell Programming and Scripting

grep and regular expression

Hi, I am executing a svnlook command to check to see if the following line exists. I need a regular expression to represent the line. A /test/test1/qa/test2/index.html A /test/test1/qa/test3/test.jpg A /test/test1/qa/test3/test1.jpg A /test/test1/qa/test4/test.swf I just need to extract... (9 Replies)
Discussion started by: kminkeller
9 Replies

7. UNIX for Dummies Questions & Answers

using regular expression for directories in find command

Hi, I want to find the files available in a directory /var/user/*/*/data/. I tried using the command "find /var/user/ -path '*/*/data/ -name '*' -type f" it says find: 0652-017 -path is not a valid option and then i tried using "find /var/user/ -name '*/*/data/*' -type f" but its not... (3 Replies)
Discussion started by: vinothbabu12
3 Replies

8. Shell Programming and Scripting

regular expression not working.

Hi, I have to match a certain pattern of string in my shell script: 6.0.4.11.9 7.5.1.7.1 First Number can be 6 or 7 Second number can be 0 or 5 Rest all numbers can be between 1-99 I am using following egrep: egrep ^\.\.\.\.\$ filename But why is the above regular... (1 Reply)
Discussion started by: som.nitk
1 Replies

9. UNIX for Dummies Questions & Answers

Create directories with regular expression

Hi guys, can any one tell me how to create directories using regular expression? Let's say that I need to create directories test01, test02, test03.... test10. Can it be done using any regular expression? thanks. (13 Replies)
Discussion started by: mahendrt
13 Replies

10. Shell Programming and Scripting

grep : regular expression

guys, my requirment goes like this: I have a file, and wish to filter out records where 1. The first letter is o or O and 2. The next 4 following letter should not be ther I do not wish to use pipe and wish to do it in one shot. The best expression I came up with is: grep ^*... (10 Replies)
Discussion started by: RishiPahuja
10 Replies
Login or Register to Ask a Question