Question

file.txt contains:

##w##

##wew##

using mac 10.6, bash shell, the command:

cat file.txt | grep [[:alpha:]]* -o

outputs nothing. I'm trying to extract the text inside the hash signs. What am i doing wrong?

Was it helpful?

Solution

(Note that it is better practice in this instance to pass the filename as an argument to grep instead of piping the output of cat to grep: grep PATTERN file instead of cat file | grep PATTERN.)

What shell are you using to execute this command? I suspect that your problem is that the shell is interpreting the asterisk as a wildcard and trying to glob files.

Try quoting your pattern, e.g. grep '[[:alpha:]]*' -o file.txt.

I've noticed that this works fine with the version of grep that's on my Linux machine, but the grep on my Mac requires the command grep -E '[[:alpha:]]+' -o file.txt.

OTHER TIPS

sed 's/#//g' file.txt

/SCRIPTS [31]> cat file.txt
##w##
##wew##

/SCRIPTS [32]>  sed 's/#//g' file.txt
w
wew

if you have bash >3.1

while read -r line
do
  case "$line" in
   *"#"* )
        if [[ $line =~ "^#+(.*)##+$" ]];then
            echo ${BASH_REMATCH[1]}
        fi
  esac    
done <"file"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top