Searching for three or four Uppercase Letters within a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Searching for three or four Uppercase Letters within a file
# 1  
Old 10-22-2010
Looking how to find only three or four letter strings using grep in a file called hello:

file contains:
Code:
TIT
TAT
RATA
ERAT
RATE
HI
RE
CA
PA
CHANGE
SANDY
ANSWER

I am using the code:
Code:
cat hello|grep [A-Z]\{3,4\}

If anybody could please help that'd be great. thanks

:: Repost because I accidentally deleted the original post ::

Last edited by auerbeck.tyler; 10-24-2010 at 02:37 PM..
# 2  
Old 10-22-2010
Code:
{ while read line ; do [[ "${#line}" = [34] ]] && echo "$line" ; done } <toto

# 3  
Old 10-22-2010
Hi.

Using grep basic regular expressions:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate regex, classes, repetition, boundaries.

# Section 1, setup, pre-solution.
# Infrastructure details, environment, commands for forum posts. 
# Uncomment export command to test script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
C=$HOME/bin/context && [ -f $C ] && . $C specimen grep
set -o nounset
pe

FILE=${1-data1}

# Section 2, display input file.
# Display sample of data file, with head & tail as a last resort.
pe " || start [ first:middle:last ]"
specimen 6 $FILE \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"

# Section 3, solution.
pl " Results:"
grep '^[[:upper:]]\{3,4\}$' $FILE

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
specimen (local) 1.17
GNU grep 2.5.3

 || start [ first:middle:last ]
Whole: 6:0:6 of 12 lines in file "data1"
TIT
TAT
RATA
ERAT
RATE
HI
RE
CA
PA
CHANGE
SANDY
ANSWER
 || end

-----
 Results:
TIT
TAT
RATA
ERAT
RATE

See man grep for details ... cheers, drl
# 4  
Old 10-22-2010
Code:
grep -E '^[A-Z]{3}[A-Z]?$' myt


Code:
[ctsgnb@shell ~]$ cat myt
TIT
TAT
RATA
ERAT
RATE
HI
RE
CA
PA
CHANGE
SANDY
ANSWER
[ctsgnb@shell ~]$ grep -E '^[A-Z][A-Z][A-Z][A-Z]?$' myt
TIT
TAT
RATA
ERAT
RATE
[ctsgnb@shell ~]$ grep -E '^[A-Z]{3}[A-Z]?$' myt
TIT
TAT
RATA
ERAT
RATE
[ctsgnb@shell ~]$

# 5  
Old 10-22-2010
for any 3-4 cap letters
Code:
$ ruby -ne 'print if [3,4].include?($_.scan(/[A-Z]+/)[0].size)' file

for exactly 3-4 cap letters
Code:
$ ruby -ne 'print if [3,4].include?($_.scan(/^[A-Z]+$/)[0].size) rescue 1' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cannot find correct syntax to make file name uppercase letters

I have a file name : var=UsrAccChgRpt I want to make them upper case. Tried: $var | tr Error: tr: Invalid combination of options and Strings. Usage: tr | -ds | -s | -ds | -s ] String1 String2 tr { -d | -s | -d | -s } String1 Could you please help. I am using AIX... (2 Replies)
Discussion started by: digioleg54
2 Replies

2. UNIX for Beginners Questions & Answers

Listing a file/directory with 7 letters long

I know that I can use wild cards:ls ???????to list files 7 characters long, but how do i omit the .?! and spaces? Please use CODE tags when displaying sample input, sample output, and code segments. (2 Replies)
Discussion started by: hiya54
2 Replies

3. Shell Programming and Scripting

Grep/Awk on 1st 2 Letters in 2nd Column of File

Hi everyone. I need to change a script (ksh) so that it will grep on the 1st 2 letters in the second column of a 5 column file such as this one: 192.168.1.1 CAXY0_123 10ABFL000001 # Comment 192.168.1.2 CAYZ0_123 10ABTX000002 # Comment 192.168.2.1 FLXY0_123 11ABCA000001 ... (4 Replies)
Discussion started by: TheNovice
4 Replies

4. Shell Programming and Scripting

Rename file to uppercase except extension

Hi, I am trying to make all file in the directory uppercase but not their extension (ex: image.jpg becoming IMAGE.jpg) here is code i am trying. $ ls | while read file do name=${file%%.*} newfilename=$(echo $name | tr 'a-z' 'A-Z') mv $file $newfilename done any suggestions of... (4 Replies)
Discussion started by: johninweb
4 Replies

5. Shell Programming and Scripting

Trying to find number of uppercase and lowercase letters.

Hello Guys, I am writing a script which check each line for how many Uppercase and Lowercase letter of a given file. Please check my script as follow: l=0 while read line do echo Line `expr $l + 1` has ` tr -cd "" < $line | wc -c` uppercase letters and `tr -cd "" < $line | wc -c`... (3 Replies)
Discussion started by: kasparov
3 Replies

6. Shell Programming and Scripting

changing all characters of a file to capital letters

Hi guys. I have file named output.txt containing file names. one per line. I use this command to convert all characters to capital letters and write to the same file. cat output.txt | tr 'a-z' 'A-Z' > output.txtBut at the end output.txt is emtpy. Could anyone help?? (6 Replies)
Discussion started by: majid.merkava
6 Replies

7. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

8. UNIX for Dummies Questions & Answers

WebDav/davfs mounted file & directory names in all UPPERCASE

Hey, I have a WebDav directory mounted and everything seems fine except for one thing. All file/directory names appear in all UPPERCASE, when in actual fact they are lowercase on the remote machine. For example: foo/bar/baz.html on the remote host, appears on my local machine as... (0 Replies)
Discussion started by: MrMoney
0 Replies

9. Shell Programming and Scripting

How can I find the 3 first letters from the name file

Hello, I have a name file in Unix for example : ABC_TODAYFirst.001 and I want just capture or display the 3 first letters so : ABC. I tried with cut -c,1-3 and the name but it displays the 3 first letters of all lines. Can you help , Thanks a lot (8 Replies)
Discussion started by: steiner
8 Replies

10. UNIX for Advanced & Expert Users

look in file, seperate letters, put in order...

okay, I need some help! Im trying to write a script where it looks in the file you designate, pulls apart all the words so i can count how many of each letter there is in the file, then i need to put them in the order of the most occuring letter to the least. This most likley will need a loop... (3 Replies)
Discussion started by: chekeitout
3 Replies
Login or Register to Ask a Question