Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want my output :

VDD.
GND
AGNDSUB 
VMEASPOS.
VMEASNEG
VREFEXT 

File1 having following information :

Power and signal 
VDD Digital Power This pin provides power supply connection for the digital
blocks.
GND Digital Ground This pin provides ground connection for the digital blocks.
AGNDSUB Ground This pin provides substrate connection.
VMEASPOS Digital Power Voltage to be measured.
VMEASNEG Ground Ground for the voltage to be measured.
VREFEXT Digital Power Reference voltage input of 1.024V %for VSENS calibration.
operating voltage
share|improve this question
1  
Where are the dots at the end of VDD. and VMEASPOS. meant to come from? –  Stéphane Chazelas Aug 12 at 8:43

4 Answers 4

up vote 7 down vote accepted

With Gnu grep:

grep -Eow '^[[:upper:]]+' file
share|improve this answer
    
-w is GNU extended. –  cuonglm Aug 12 at 6:13
    
@Gnouch, -o is a GNU extension. -w however though not standard is widespread and I believe predates GNU grep. –  Stéphane Chazelas Aug 12 at 6:50
    
@StéphaneChazelas: I think that it's not so important what locale OP's computer is using as what OP wants to match. In a C/POSIX locale, the two example commands do the same thing. The first one works if it is desired to match the current locale's upper-case letters; the second one works if it is desired to match exactly the 26 upper-case ASCII letters. –  rici Aug 12 at 6:55
    
Even in a typical en_US.utf8 locale, [A-Z] matches É as well, and Ý, but not Ź. It's also been known to match b on some systems. Which is why I say that A-Z only makes sense in the POSIX/C locales where it is equivalent to [ABCDEFGHIJKLMNOPQRSTUVWXZ]. –  Stéphane Chazelas Aug 12 at 7:02
1  
@Gnouc and @StephaneChazelas: By the way, freebsd.org/cgi/… shows a manpage for Berkeley grep dating back to 1979; it includes the -w flag. I don't know why that never got into Posix. I think Stéphane's right about -o originating with Gnu; iirc, the original meaning of -o was what is now -H –  rici Aug 12 at 7:10

You can use awk:

awk '/^[A-Z]+\>/ { print $1 }' < data

/^[A-Z]+\>/ matches complete words in capital letters at the start of the line. {print $1} then prints out the first field on those lines.

share|improve this answer
    
\> only works in some implement like gawk, nawk. –  cuonglm Aug 12 at 6:05
1  
And matches at the transition between a [[:alnum:]_] and [^[:alnum:]_] (or the eol) while $1 prints up to the first blank (or eol). –  Stéphane Chazelas Aug 12 at 6:54

There are several ways to tackle the problem.

You could print the first word as long as it contains only uppercase letters:

awk '$1 ~ /^[[:upper:]]+$/ {print $1}'

(would print VDD, but not VDD+DDV)

print the first word as long as it doesn't contain lower case letters.

awk 'NF && $1 !~ /[[:lower:]]/ {print $1}'

That would print VDD+DDV or U.S.A. but not VDDfoo, but would print +++.

You could print the sequence of letters at the start of the line as long as they're all uppercase:

sed 's/[^[:alpha:]].*//;/^[[:upper:]]\{1,\}$/!d'

Or to ignore leading blanks:

sed 's/^[[:blank:]]*//;s/[^[:alpha:]].*//;/^[[:upper:]]\{1,\}$/!d'

Would match VDD in VDD+xxx but not in VDDxxx

share|improve this answer

A perl solution:

$ perl -Mopen=locale -anle 'print $F[0] if /^[[:upper:]]+\b/' file 
VDD
GND
AGNDSUB
VMEASPOS
VMEASNEG
VREFEXT
share|improve this answer
    
Like for Michael's, you're matching on the line until a word boundary but printing a field (so up to a blank). So, for instance, it would print FOO.bar for a line like FOO.bar baz. –  Stéphane Chazelas Aug 12 at 11:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.