awk - how to compare part of the string?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - how to compare part of the string?
# 8  
Old 10-14-2013
Doesn't work on my VM ubuntu ...

$ cat tst2
abc,def,data, data,0011234567890, data,data
abc,def,data, data,00112345678901, data,data
abc,def,data, data,001123456789, data,data
abc,def,data, data,001123456789x, data,data
abc,def,data, data,0021234567890, data,data
$ awk -F, -v c=001 '$5~"^"c"[0-9]{10}"' tst2
$ awk -F, -v c=001 '$5~"^"c"..........$"' tst2
abc,def,data, data,0011234567890, data,data
abc,def,data, data,001123456789x, data,data
$ uname -a
Linux <anonymized> 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux

That one works
$ awk -F, -v c=001 '$5~"^"c"..........$"&&$5!~/[^0-9]/' tst2
abc,def,data, data,0011234567890, data,data

Last edited by ctsgnb; 10-14-2013 at 03:02 PM..
# 9  
Old 10-14-2013
ERE/BRE, maybe? How about
$ awk -F, -v c=001 '$5~"^"c"[[:digit:]]\{10\}$"' file
awk: cmd. line:1: warning: escape sequence `\{' treated as plain `{'
awk: cmd. line:1: warning: escape sequence `\}' treated as plain `}'
abc,def,data, data,0011234567890, data,data

(which my GNU awk 4.1.0 doesn't like much)

Or the looong version, of course Smilie.
$ awk -F, -v c=001 '$5~"^"c"[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"' file
abc,def,data, data,0011234567890, data,data

# 10  
Old 10-14-2013
Of course the long version works fine Smilie
( i also thought about it earlier but it was so long, i had preferred to put the one with &&$5!~/[^0-9]/
$ awk -F, -v c=001 '$5~"^"c"[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"' tst2
abc,def,data, data,0011234567890, data,data

But the backslashed short one still doesn't :
$ awk -F, -v c=001 '$5~"^"c"[[:digit:]]\{10\}$"' tst2

# 11  
Old 10-14-2013
Try adding --re-interval to the flags.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Pass column number as variable to awk and compare with a string.

Hi All, I have a file test.txt. Content of test.txt : 1 vinay se 2 kumar sse 4 kishore tl I am extracting the content of file with below command. awk '$2 ~ "vinay" {print $0}' test.txt Now instead of hardcoding $2 is there any way pass $2 as variable and compare with a... (7 Replies)
Discussion started by: Girish19
7 Replies

3. Shell Programming and Scripting

String compare using awk - what am I doing wrong?

Hi all, I was doing some string manipulation in my script and wanted to try using awk. However, I have been stuck with string compare. A simplified form of my conundrum is given below: The below prints expected result (prints "Completed because that is the second element"): $ echo... (5 Replies)
Discussion started by: faraway
5 Replies

4. Shell Programming and Scripting

awk: process a part of string, leaving others unchanged

Hello, I have a bunch of strings of the following format: x.y.z. What I want to do is format each string in this way: x.y.z i.e. the string conists of two columns, where delimiter is space. I need to output modified first column (delimiter inside... (7 Replies)
Discussion started by: zerorange
7 Replies

5. Shell Programming and Scripting

awk if statement to evaluate string and compare

I have the following simplified code that I am planning on putting into a larger shell script. I have been butchering it to try and make work amongst google searches and reading awk documentation. amixer sset Master toggle | awk '{ if ( /^ Front Left/ { print $7 } == // ) print "MUTED" }'I... (2 Replies)
Discussion started by: jelloir
2 Replies

6. Shell Programming and Scripting

[awk]compare a number in a string with a list

Hi, I have a program written in awk and I want to extend it to do another task. My program is a list of CVS log reports of a repository. For each file, I have some fields. One of the fields is the comment field. I want to know how I can check if a comment (which is a free text field)... (8 Replies)
Discussion started by: sandeepk1611
8 Replies

7. Shell Programming and Scripting

Need Awk command to get part of string based on delimeter

HI, Need awk command to get date and time alone from Input : "15:29:15 28.08.2010|SCHEDULE: Started program POSG1" Output expected : "15:29:15 28.08.2010" Please help. (9 Replies)
Discussion started by: shanneykar
9 Replies

8. Shell Programming and Scripting

How to get part of string in awk from match

Hi, Im an awk noob and I am having trouble trying to get matches. Here is my script: #!/bin/gawk -f BEGIN {} $0 ~ /<a href=".*">.*<\/a>/{print} Ideally I want to be able to get the actual link and print it. In PHP you can do preg_replace and get the match you want by using \\1 where 1... (2 Replies)
Discussion started by: adsyuk
2 Replies

9. Shell Programming and Scripting

Extract Part of string from 3rd field $3 using AWK

I'm executing "wc -lc" command in a c shell script to get record count and byte counts and writing them to a file. I get the result with the full pathname of the file. But I do not want the path name to be printed in the output file. I heard that using Awk we can get this but I don't have any... (4 Replies)
Discussion started by: stakuri
4 Replies
Login or Register to Ask a Question