Searching pattern and reformatting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching pattern and reformatting
# 1  
Old 02-18-2010
Searching pattern and reformatting

Hi,

I have a file whose sample data is as below:

Code:
Rak 654 JM30 20100215 387898263 7867556789
RPT 654 TM38 20100215 827384652 9786735628 RNP
DCB 342 TM38 20100215 876782372 2323786527
Rak 654 JM30 20100215 328564893 8862573828
RNN 684 SK20 20100215 926738429 8283927457 RPP
TKS 20100215 873927382 7283556278
TPM 666 RS29 8273334256 RTK NFM

Now what I need to do is to print the numeric string whose length is 10 digits and then print the rest of the line. The output of the above sample should be:

Code:
7867556789 Rak 654 JM30 20100215 387898263 7867556789
9786735628 RPT 654 TM38 20100215 827384652 9786735628 RNP
2323786527 DCB 342 TM38 20100215 876782372 2323786527
8862573828 Rak 654 JM30 20100215 328564893 8862573828
8283927457 RNN 684 SK20 20100215 926738429 8283927457 RPP
7283556278 TKS 20100215 873927382 7283556278
8273334256 TPM 666 RS29 8273334256 RTK NFM

Please help. Thanks in advance.

Regards

Last edited by zxmaus; 02-18-2010 at 02:51 PM.. Reason: added code tags
# 2  
Old 02-18-2010
Hi.

Code:
$ cat TestScript
awk '
  { for( I = 1; I <= NF; I++ ) if ( $I ~ /^[0-9]{10}$/ ) $1 = $I FS $1 } 1
' file1

$ ./TestScript
7867556789 Rak 654 JM30 20100215 387898263 7867556789
9786735628 RPT 654 TM38 20100215 827384652 9786735628 RNP
2323786527 DCB 342 TM38 20100215 876782372 2323786527
8862573828 Rak 654 JM30 20100215 328564893 8862573828
8283927457 RNN 684 SK20 20100215 926738429 8283927457 RPP
7283556278 TKS 20100215 873927382 7283556278
8273334256 TPM 666 RS29 8273334256 RTK NFM

# 3  
Old 02-18-2010
Hi scott,

i thought about the same solution in solaris but below part does not work in solaris, is there any good way to do it in solaris? maybe Lenght function? or no choice rather than using 10 times [0-9] Smilie

Code:
/^[0-9]{10}$/

# 4  
Old 02-18-2010
Hi EAGL€.

It should work with /usr/xpg4/bin/awk, and with nawk too.

Failing that, try escaping the { } .... \{ \}

Cheers.
# 5  
Old 02-18-2010
Quote:
Originally Posted by scottn
Hi EAGL€.

It should work with /usr/xpg4/bin/awk, and with nawk too.

Failing that, try escaping the { } .... \{ \}

Cheers.
I realized that the problem was nawk, not my code. Really interesting it didnt work with nawk only with /usr/xpg4/bin/awk Smilie

Code:
/usr/xpg4/bin/awk '{for(i=1;i<=NF;i++) if($i~/^[0-9]{10}$/) {print $i" "$0}}' file

ok but this worked too:

Code:
nawk '{for(i=1;i<=NF;i++) if(length($i)==10) {print $i FS $0}}' file


Last edited by EAGL€; 02-18-2010 at 04:23 PM..
# 6  
Old 02-18-2010
I've noticed more and more things that don't work with nawk, but do with /usr/xpg4/bin/awk.

length() is OK, but it doesn't tell you if it's digits (i.e. numbers) or not.
# 7  
Old 02-18-2010
Quote:
Originally Posted by scottn
I've noticed more and more things that don't work with nawk, but do with /usr/xpg4/bin/awk.

length() is OK, but it doesn't tell you if it's digits (i.e. numbers) or not.
Ok Scott thanks a lot for this usefull knowledge, Im going to use /usr/xpg4/bin/awk for guaranteed solutions anymore. Besides you right length will work there for fields with 10 ALNUMs, not only digits.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for a pattern and extracting records related to that pattern

Hi there, Looking forward to your advice for the below: I have a file which contains 2 paragraphs related to a particular pattern. I have to search for those paragraphs from a log file and then print a particular line from those paragraphs. Sample: I have one file with the fixed... (3 Replies)
Discussion started by: danish0909
3 Replies

2. Shell Programming and Scripting

pattern searching

Hi, Can you please help me out here? I am trying develop a search pattern to extract certain words from the two strings below. I want to extract ericsson_msc_live from the 2 strings and then the date, which is a part of the filename just before the .jar extension. ... (19 Replies)
Discussion started by: danish0909
19 Replies

3. UNIX for Dummies Questions & Answers

searching pattern in VI

in my file i have somthing likre kpk_12 kpk_1 kpk_1.tcl kpk_3.tcl kpk kpk kpk i want search only kpk i am using this cmd /kpk ...results it is showing all . any cmd is ther other then this to search exactword in this example kpk it shoulsnot show kpk_* etc Thanks in Advance ... (2 Replies)
Discussion started by: prakumar
2 Replies

4. UNIX for Dummies Questions & Answers

Pattern searching

Hi, I need small help from you people. In a directory there are around 150 odd files and few them contain the word "TRACK" and few are not. How can I find out the the list of those files which doesn't contain the word "TRACK"? Thanks, Siba (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

5. Shell Programming and Scripting

Searching for a pattern

How do I search for a pattern - N/A in a particular column using awk? (11 Replies)
Discussion started by: rabiu
11 Replies

6. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

7. Shell Programming and Scripting

searching for a pattern

can anybode tell me ? I want to search for a pattern present in a whole directory and subdirectories's files containg " crat" I tried grep -r "crat" */* ; is it right ? (3 Replies)
Discussion started by: pranabrana
3 Replies

8. Shell Programming and Scripting

Pattern searching pattern in c files

I have a problem in searching a specific pattern in c files. My requirement: I have to find all the division operator in all cfiles. The problem is, the multi line comments and single line comments will also have forward slash in it. Even after avoiding these comments also, if both... (6 Replies)
Discussion started by: murthybptl
6 Replies

9. Shell Programming and Scripting

Regarding Searching Pattern

Hi Guys, Can you help with the shell script: I would like to search a fixed width pattern from a file say for each line from a fixed position and lenght it has to return all rows from the file. Example: To search the third column for "def" it has to return 1 and 4th rows only ... (2 Replies)
Discussion started by: sbasetty
2 Replies

10. Programming

pattern searching using C

i think grep can only find char in files matching a pattern without any wildcards like ? correct? It works with * but not ?. how can i write a small c program to find words in a file, like a list, that matches a pattern like ma?y, b??con, etc if grep doesn't understand ? in a pattern search. if... (1 Reply)
Discussion started by: giannicello
1 Replies
Login or Register to Ask a Question