Operating System - Linux
1748151 Members
3504 Online
108758 Solutions
New Discussion юеВ

Re: Quick grep format question

 
SOLVED
Go to solution

Quick grep format question

All - I need some help.

I want to use grep to copy all the words in a file which have uppercase letters into another file.

I just want the uppercase words themselves, not the entire line.

Can someone give me the command?

I'd appreciate it.

Thanks
"...it's turtles all the way down."
4 REPLIES 4
Marcin Golembski_1
Honored Contributor
Solution

Re: Quick grep format question

You can't do this with grep. If you have perl, try creating a script, say script.pl, containing:

while(<>) {
chomp;
foreach $word (split) {
if ($word=~/^[A-Z]+/) { print $word,"\n"; }
}
}

then run it:

perl script.pl yourfile > resultfile

resultfile will contain all words from yourfile starting with an uppercase letter, one word per line. If you want only all-uppercase words, add $ after + and if you want uppercase anywhere in the word, remove ^.

HTH,
Marcin
Volker Borowski
Honored Contributor

Re: Quick grep format question

Hi,
here a solution without perl:

# cat bastel.sh
action()
{
while [ $# -gt 0 ]
do
echo $1 | grep -e '[A-Z]'
shift
done
}

while read LINE
do
action $LINE
done
#

Volker
eugene_6
Advisor

Re: Quick grep format question

echo inputfile | awk 'BEGIN { RS="[\ \t]"} { print $1 }' | grep [A-Z] > outputfile
eugene_6
Advisor

Re: Quick grep format question

short correction
---------------------------

echo inputfile | awk 'BEGIN { RS="[\ \t]"} { print $1 }' | grep ^[A-Z] > outputfile