printf / regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printf / regex
# 1  
Old 12-31-2008
printf / regex

Morning folks,

I need help with the following issue:

Let's say we I have the following output:
Code:
First Name:  Test
Last Name:   Test2
Number:   T1234
Number2:  T1234

Needed Output:
Code:
T1234


Now I want to grep/nawk/printf/sed out the Number:

- I like to search for T[0-9][0-9][0-9][0-9]
- Aditionally the script should just print out one T-Number (needed Output)


Can you help me to iplement this requirements into a one liner?

Cheers,

Domi
# 2  
Old 12-31-2008
Code:
awk '/T[0-9]{4}/{print $2}' file | uniq

On Linux you have to add the '--posix' parameter for the regex to work correctly
# 3  
Old 12-31-2008
Hi,

code:
Code:
grep -o "T[0-9]\{4\}" file | uniq

output:
Code:
T1234

HTH Chris
# 4  
Old 12-31-2008
Thank you guys.

@pludi

Your script does not work:

Code:
[user@host] ~
 # more file
iFirst Name:  Test
Last Name:   Test2
Number:   T1234
Number2:  T1234
[user@host] ~
 # awk '/T[0-9]{4}/{print $2}' file | uniq
[user@host] ~

@Christop Spoher
i can not use grep -o (I'm running Solaris 10 so I'm using -e instead)

Code:
[user@host] ~
 # /usr/xpg4/bin/grep -e "T[0-9]\{4\}" file | uniq
Number:   T1234
Number2:  T1234
[user@host] ~

@pludi, Christoph Spohr

What is when i have the following output:
Code:
First Name: Test
Last Name: Test2
Number: asd asdsf  234 T1234 ...dasda
Number 2: asd asdsf  234 T1234 ...dasda

So basically I'm looking for a solution without using "print $2"
# 5  
Old 12-31-2008
Code:
Code:
sed -n '/T[0-9]\{4\}/{s/.*\(T[0-9]\{4\}\).*/\1/g;p}' file

Input:
Code:
First Name: Test
Last Name: Test2
Number: asd asdsf  234 T1234 ...dasda
Number 2: asd asdsf  234 T1234 ...dasda

Output:
Code:
T1234

If your sed doesn't support the ";"shortcut, you have to use a sedfile.
# 6  
Old 12-31-2008
Hmm...

Code:
[user@host] /
 # more file
First Name: Test
Last Name: Test2
Number: asd asdsf  234 T1234 ...dasda
Number 2: asd asdsf  234 T1234 ...dasda
[user@host] /
 # sed -n '/T[0-9]\{4\}/{s/.*\(T[0-9]\{4\}\).*/\1/g;p}' file
sed: command garbled: /T[0-9]\{4\}/{s/.*\(T[0-9]\{4\}\).*/\1/g;p}
[user@host] /
 #

Do I really have to use a 'sedfile' in this case? Is there no other way?

Thx very much for your help.
# 7  
Old 12-31-2008
Chris,

On Linux (Debian) it does work!

Code:
tester:~# more file
First Name: Test
Last Name: Test2
Number: asd asdsf  234T1234 ...dasda
Number 2: asd asdsf  234 T1234 ...dasda
tester:~# sed -n '/T[0-9]\{4\}/{s/.*\(T[0-9]\{4\}\).*/\1/g;p}' file | uniq
T1234
tester:~# uname -a
Linux tester 2.6.24-etchnhalf.1-486 #1 Tue Dec 2 07:03:38 UTC 2008 i686 GNU/Linux
tester:~#

Any idea what I can do on solaris?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

4. Programming

printf quirk

Hi, Could anyone explain me the logic behind the following program's output? int main() { printf("%d\n", printf("%d %d", 2, 2) & printf("%d %d", 2, 2)); printf("%d\n", printf("%d %d\n", 2, 2) & printf("%d %d\n", 2, 2)); } Ans: 2 22 23 2 2 2 2 4 (2 Replies)
Discussion started by: royalibrahim
2 Replies

5. Shell Programming and Scripting

find + printf help

Hi, I have a scripting assignment for an intro to linux class and I'm really confused about how to do something seemingly simple. I am supposed to Print the name of each file in the /data/dir16/subdir1 directory in the following format: "My name is: bin" The desired output example looks like:... (1 Reply)
Discussion started by: danschmidt
1 Replies

6. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

7. UNIX for Dummies Questions & Answers

Need help with printf

Hi, I have just completed my first script (:D) and now i just need to format it with printf. This is what I have: #!/bin/ksh TOTB=0 TOTF=0 TOTI=0 HOST=`hostname` echo " FSYSTEM BLKS FREE INUSE MOUNTEDON" df -m | grep -v ":"|grep -v Free|grep -v "/proc"| while read FSYSTEM... (2 Replies)
Discussion started by: compan023
2 Replies

8. Shell Programming and Scripting

printf

How to print output in following format? A..................ok AA................ok AAA..............ok AAAAAA........ok "ok" one under one (4 Replies)
Discussion started by: mirusnet
4 Replies

9. Programming

printf

What is the output of the following program considering an x86 based parameter passing sequence where stack grows towards lower memory addresses and that arguments are evaluated from right to left: int i=10; int f1() { static int i = 15; printf("f1:%d ", i); return i--; } main() {... (2 Replies)
Discussion started by: arunviswanath
2 Replies

10. Shell Programming and Scripting

printf question

can you take input from another command and do printf? such as awk '{print $2,$1}' | sort -k1,1 -k2,2 | printf "%-10s,%15s" this does not work.. but there must be a way.. please help me.. thank you. (3 Replies)
Discussion started by: hankooknara
3 Replies
Login or Register to Ask a Question