|
From: 3ashmawy on 13 Aug 2006 04:56 I am trying to locate identifiers in files and i am having trouble adjusting with sed and regular expressions for example [anacad1] syntrain:/home/syntrain/scripts$ cat htest int x87; float 7h; int kljsd; hu87; 908; int 78465;[anacad1] syntrain:/home/syntrain/scripts$ sed -n '/[a-zA-Z]+/p' htest [anacad1] syntrain:/home/syntrain/scripts$ What i am expecting is that it would only print lines that have words consisting of letters only (not numbers not anything else) why doesnt it work ? , and if i wanted to only print with sed lines that contain words having only letters what should i write instead. Thanks for your suggestions in advance
From: Stephane CHAZELAS on 13 Aug 2006 06:38 2006-08-13, 01:56(-07), 3ashmawy(a)gmail.com: [...] > int 78465;[anacad1] syntrain:/home/syntrain/scripts$ sed -n > '/[a-zA-Z]+/p' htest > [anacad1] syntrain:/home/syntrain/scripts$ > > What i am expecting is that it would only print lines that have words > consisting of letters only (not numbers not anything else) why doesnt > it work ? , and if i wanted to only print with sed lines that contain > words having only letters what should i write instead. [...] sed uses basic regular expressions. + is an extended regular expression operator, the BRE equivalent is \{1,\}. sed -n '/[[:alpha:]]\{1,\}/p' htest sed '/[[:alpha:]]\{1,\}/!d' htest grep '[[:alpha:]]\{1,\}' htest grep -E '[[:alpha:]]+' htest But that's the same as: grep '[[:alpha:]]' As a line that contains one or more letters contains 1 letter and vis versa. Maybe you meant: grep -xE '[[:alpha:]]+' or sed -n '/^[[:alpha:]]\{1,\}$/p' i.e. only display lines that contain only letters (and are not empty). -- Stphane
From: 3ashmawy on 13 Aug 2006 07:18 nothing worked ... i am working on a solaris 9 so i dont have access to a new version of grep so that i can use -xE but if the sed commands are right then they should be working on my box normally [anacad1] syntrain:/home/syntrain/scripts$ cat htest int x87; float 7h; int kljsd; hu87; 908; int 78465;[anacad1] syntrain:/home/syntrain/scripts$ sed -n '/[[:alpha:]]\{1,\}/p' htest [anacad1] syntrain:/home/syntrain/scripts$ sed '/[[:alpha:]]\{1,\}/!d' htest d': Event not found [anacad1] syntrain:/home/syntrain/scripts$ grep '[[:alpha:]]\{1,\}' htest [anacad1] syntrain:/home/syntrain/scripts$ grep -E '[[:alpha:]]+' htest grep: illegal option -- E Usage: grep -hblcnsviw pattern file . . . [anacad1] syntrain:/home/syntrain/scripts$ grep '[[:alpha:]]' htest [anacad1] syntrain:/home/syntrain/scripts$ grep -xE '[[:alpha:]]+' htest grep: illegal option -- x grep: illegal option -- E Usage: grep -hblcnsviw pattern file . . . [anacad1] syntrain:/home/syntrain/scripts$ sed -n '/^[[:alpha:]]\{1,\}$/p' htest [anacad1] syntrain:/home/syntrain/scripts$
From: Stephane CHAZELAS on 13 Aug 2006 07:37 2006-08-13, 04:18(-07), 3ashmawy(a)gmail.com: > nothing worked ... > i am working on a solaris 9 so i dont have access to a new version of > grep so that i can use -xE but if the sed commands are right then they > should be working on my box normally Solaris is not Unix by default. You need to set your PATH to something like: PATH=`getconf PATH`:$PATH export PATH and use /usr/xpg4/bin/sh instead of /bin/sh. And don't use csh. In csh, "!" is used for history expansion, so you need to escape it in some way. -- Stphane
From: 3ashmawy on 13 Aug 2006 07:59
Still its not working ... $ PATH=`getconf PATH`:$PATH $ export PATH $ /usr/xpg4/bin/sh $ sed -n '/[[:alpha:]]\{1,\}/p' htest int x87; float 7h; int kljsd; hu87; int 78465; $ Its supposed to output int kljsd; only because its the only line that contains letters only .... 3ashmawy |