Matching multiples of a single character using sed and awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching multiples of a single character using sed and awk
# 1  
Old 09-02-2008
Matching multiples of a single character using sed and awk

Hi,

I have a file 'imei_01.txt' having the following contents:

$ cat imei_01.txt
a123456
bbr22135
yet223

where I want to check whether the expression 'first single alphabet followed by 6 digits' is present in the file (here it is the first record 'a123456')

I am using the following script, but it does not give output
Code:
sed '/[0-9]\{6\}/p' imei_01.txt

awk '/[a-z][0-9]\{6\}/ {print}' imei_01.txt

# 2  
Old 09-02-2008
For sed this works:

Code:
sed -n '/[a-z][0-9]\{6\}/p' infile


Last edited by zaxxon; 09-02-2008 at 07:53 AM.. Reason: Stripped overhead
# 3  
Old 09-02-2008
Try this:

Code:
sed -n '/[A-Za-z]\([0-9]\{6\}\)/p' file

# 4  
Old 09-02-2008
Code:
egrep '^[a-z][0-9]{6}$' imei_01.txt

Watch the anchors!
# 5  
Old 09-02-2008
Hi.

If you need to use awk, some versions require a switch to enable the interval expressions:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate awk regular expressions.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) awk
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results, enabling interval expressions:"
awk --re-interval '
/[a-z][0-9]{6}/   {print}
' $FILE

echo
echo " Results, enabling interval expressions, simplified:"
awk --re-interval '
/[a-z][0-9]{6}/
' $FILE

echo
echo " Results, enabling interval expressions, simplified, anchors:"
awk --re-interval '
/^[a-z][0-9]{6}/
' $FILE

exit 0

producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
GNU Awk 3.1.4

 Data file data1:
a123456
bbr22135
yet223
Not at beginning of line: a123456

 Results, enabling interval expressions:
a123456
Not at beginning of line: a123456

 Results, enabling interval expressions, simplified:
a123456
Not at beginning of line: a123456

 Results, enabling interval expressions, simplified, anchors:
a123456

See man awk for details ... cheers, drl
# 6  
Old 09-02-2008
Hammer & Screwdriver A few slightly different variant solutions

Code:
> cat infile | grep "^[a-zA-Z]"
a123456
bbr22135
yet223

> cat infile | grep "^[a-zA-Z][0-9][0-9][0-9][0-9][0-9][0-9]"
a123456
> cat infile | grep "^[a-zA-Z][0-9]\{6\}"
a123456
> grep "^[a-zA-Z][0-9]\{6\}" <infile
a123456

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find and replace single character w/awk given conditions

I have a file that looks like this: 14985 DPN verb PPa to spend. 12886 DPNDJN bay tree. 15686 DQ verb to observe 15656 KC ... (7 Replies)
Discussion started by: jvoot
7 Replies

2. Shell Programming and Scripting

sed - filter blocks between single delimiters matching a pattern

Hi! I have a file with the following format:CDR ... MSISDN=111 ... CDR ... MSISDN=xxx ... CDR ... MSISDN=xxx ... CDR ... MSISDN=111 (2 Replies)
Discussion started by: Flavius
2 Replies

3. UNIX Desktop Questions & Answers

How to squeeze multiple pipe character '|' into single '|' using sed?

Hi, I am trying to convert multiple Unix pipe symbol or bar into single |. I have tried with the following sed statements, but, no success :(. I need it using sed only echo "sed 's/\|\+/\|/g' sed 's/*/\|/' sed 's/\|*/|/' sed -r 's/\|+/\|/' However, the below awk code is working fine.... (4 Replies)
Discussion started by: royalibrahim
4 Replies

4. Shell Programming and Scripting

sed or awk delete character in the lines before and after the matching line

Sample file: This is line one, this is another line, this is the PRIMARY INDEX line l ; This is another line The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line... (5 Replies)
Discussion started by: KC_Rules
5 Replies

5. Shell Programming and Scripting

awk - setting fs to equal any single character

Hi Does anyone know how to set any character as the field separator with awk/nawk on a solaris 10 box. I have tried using /./ regex but this doesnt work either and im out of ideas. thanks (7 Replies)
Discussion started by: chronics
7 Replies

6. Shell Programming and Scripting

Multiples commands between pipes and a single process

Hi I have this script: #!/bin/ksh cmd1 | cmd 2 |cmd 3| cmd4 which it creates 4 process.... Is possible to create a single process PID1 which include all commands? Thanks Israel (2 Replies)
Discussion started by: iga3725
2 Replies

7. UNIX for Dummies Questions & Answers

Matching pattern script (sed or awk?)

Hi Guys, I am new to the forum and to scripting so bear with me. Thanks, Gary. I have 3 files - file1, file2, file3 I am trying to come up with a script that will check the output of these files and if the 1st nine fields are matched in all 3 files, echo "The following string had been... (2 Replies)
Discussion started by: gazza-o
2 Replies

8. Shell Programming and Scripting

How do you print a single quote character in AWK

How do you print out a single quote character in AWK? Using the escape character does not seem to work. {printf "%1$s %2$s%3$s%2$s\n" , "INCLUDE", " \' ", "THIS" } does not work. Any suggestions? (6 Replies)
Discussion started by: cold_Que
6 Replies

9. Shell Programming and Scripting

AWK: replace single positional character given variables

I already have accomplished this task using sed and arrays, but since I get the variable using awk, I figured I'd ask this question and maybe I can get a cleaner solution using strictly awk.. I just can't quite grasp it in awk. Story: I'm automating the (re)configuration of network interfaces,... (3 Replies)
Discussion started by: System Shock
3 Replies

10. Shell Programming and Scripting

Single character wildcard for SED

Could someone tell me the single character wildcard for SED? I have the file below: $ more input2 AAA /A/B/C BBB /D/E/F CCC /G/H/I DDD I want to remove all strings which contain forward slashs "/" to get the below: AAA BBB CCC I tried to do it in SED by the command below but I... (8 Replies)
Discussion started by: stevefox
8 Replies
Login or Register to Ask a Question