printf / regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printf / regex
# 8  
Old 12-31-2008
Use a sedfile:

sedfile:
Code:
/T[0-9]\{4\}/{
    s/.*\(T[0-9]\{4\}\).*/\1/g
    p
}

command:
Code:
sed -n -f sedfile file | uniq

This should work.
# 9  
Old 12-31-2008
Chris,

Thanks for your help. I decided to do it this way:

Code:
nawk '{s=match($0, "T[0-9][0-9][0-9][0-9]");if(s){print substr($0,RSTART,RLENGTH)}}'

Here comes the next Problem:

At the moment I've the following output:

Code:
T1234

I now want to expand the script that i get the following:

Code:
T1234 A1

This means something like: T1234[''][A][1] (<-- It is everytime the exactly the string "A1")

How can i implement this in the script? Just "RLENGTH +3" is not an Option. Ist sould be something like:

Code:
nawk '{s=match($0, "T[0-9][0-9][0-9][0-9]");if(s){print substr($0,RSTART,RLENGTH + [][A][1])}}'

But this does not work and its just to show you my problem.

BTW: File now looks like:

Code:
ester:~# more file
First Name: Test
Last Name: Test2
Number: asd asdsf  234T1234 A1...dasda
Number 2: asd asdsf  234 T1234 A1...dasda


Last edited by domi55; 01-01-2009 at 11:05 AM..
# 10  
Old 01-01-2009
Do I have to open a new Thread for this awk question?
# 11  
Old 01-02-2009
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

# 12  
Old 01-02-2009
Arthur,

Probably I have to clarify my problem.

Files to parse:

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

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

I now want to extend my nawk script that i get the following output:

From File 1:
Code:
T1234

From File 2:
Code:
T1234 A1

So basically the nawk/awk script has to search if there is a "A1" after the "T1234" or not and it has to print out the "A1" just if there is an "A1" like in File 2.

My current nawk script:
Code:
nawk '{s=match($0, "T[0-9][0-9][0-9][0-9]");if(s){print substr($0,RSTART,RLENGTH)}}'

Thank you guys!
# 13  
Old 01-02-2009
How about this...
Code:
awk 'match($0, "T[0-9][0-9][0-9][0-9]"){out1=substr($0,RSTART,RLENGTH);if(substr($0,RSTART+RLENGTH,3)==" A1"){out2="A1"};{print out1,out2}}'

...finds the Tnnn and stores it (out1) then goes looking for the " A1" and stores that (out2) if found. Then prints both stored strings if they have been set. This works but is getting a bit long for a one-liner Smilie
# 14  
Old 01-02-2009
Yes! That is now exactly what I need! Thany you so much!

Script:
Code:
/usr/xpg4/bin/awk 'match($0, "T[0-9]{4}"){out1=substr($0,RSTART,RLENGTH);if(substr($0,RSTART+RLENGTH,3) ==" A1"){out2="A1"};{print out1,out2}}' | uniq

BTW: Is there a way to do it in a more easier (shorter) on-liner? Smilie

Thanks again Arthur!

cheers
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