LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Commands and Common Expressions-ls/grep (https://www.linuxquestions.org/questions/linux-newbie-8/commands-and-common-expressions-ls-grep-705153/)

rtrahan 02-16-2009 03:23 PM

Commands and Common Expressions-ls/grep
 
I didn't know exactly where to post this but since I am a newbie studying for the Linux+ figured here was best choice...

Scenario:
I have a file named novowel, with the following words; efgh ijkl abcd

I won't to run the expression [^abcd] which should bring all the words that don't include abcd. I tried ls [^abcd] novowel: but got ls: cannot access [^abcd]: No such file or directory novowel and I tried ls novowel [^abcd]
but got same error.

I tried grep [^abcd] novowel
and got - efgh ijkl abcd
so it brought back all the words.

Just trying to find command that works with the [^...] expression.

Thanks in advance

colucix 02-16-2009 03:40 PM

Code:

sed 's/ /\n/g' testfile | grep [^abcd]
The sed command put words on different lines, so you can use grep this way.

rtrahan 02-17-2009 04:26 AM

Yes, but what if I wanted to use the command to search a document. I would not have the flexibility to move anything around? I'll keep looking for a command that works with
[^abcd]. If you or anyone have a different input I would appeciate it.

ZAMO 02-17-2009 04:47 AM

rtrahan,

Can you explain your need on a more elaborate way?

Code:

just trying to find command that works with the [^...] expression.
All BRE's , ERE's can do that.

colucix 02-17-2009 07:48 AM

Using awk you can split each line of a file into words and each word into single characters. If and only if a character in a word does not match the complement of abcd, the word is deleted:
Code:

awk '{
  for ( i = 1; i <= NF; i++ )
    for ( j = 1; j <= length($i); j++ )
      if ( substr($i,j,1) !~ /[^abcd]/ )
        gsub($i,"",$i)
  print
}' novowel

I'm not really sure this is what you're looking for, anyway!


All times are GMT -5. The time now is 07:07 PM.