The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
printf mirusnet Shell Programming and Scripting 4 01-23-2008 10:09 AM
AwK printf question whatisthis UNIX for Dummies Questions & Answers 4 12-10-2007 01:17 PM
printf arunviswanath High Level Programming 2 09-19-2007 09:31 PM
printf command in ksh cin2000 Shell Programming and Scripting 1 12-21-2005 02:48 PM
Using printf to center CSGUY Shell Programming and Scripting 2 04-19-2005 09:15 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-31-2008
domi55 domi55 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 23
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 (permalink)  
Old 12-31-2008
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,839
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 (permalink)  
Old 12-31-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

code:
Code:
grep -o "T[0-9]\{4\}" file | uniq
output:
Code:
T1234
HTH Chris
  #4 (permalink)  
Old 12-31-2008
domi55 domi55 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 23
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 (permalink)  
Old 12-31-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
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 (permalink)  
Old 12-31-2008
domi55 domi55 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 23
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 (permalink)  
Old 01-02-2009
ArthurWaik ArthurWaik is offline
Registered User
  
 

Join Date: Apr 2007
Location: England
Posts: 4
Quote:
Just "RLENGTH +3" is not an Option.
Why?

It works for me:

Code:
$ cat zzz.txt
First Name: Test
Last Name: Test2
Number: asd asdsf  234T1234 A1...dasda
Number 2: asd asdsf  234 T1234 A1...dasda

awk '{s=match($0, "T[0-9][0-9][0-9][0-9]");if(s){print substr($0,RSTART,RLENGTH + 3 )}}' zzz.txt
T1234 A1
T1234 A1
When you say:
Quote:
It is everytime the exactly the string "A1"
do you mean it is alway the string "A1" regardless of what is in the file? If so why not just hardcode that in the print?

Code:
 awk '{s=match($0, "T[0-9][0-9][0-9][0-9]");if(s){print substr($0,RSTART,RLENGTH),"A1"}}' zzz.txt
T1234 A1
T1234 A1
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:31 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0