Visit Our UNIX and Linux User Community


Regex matching column awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Regex matching column awk
# 1  
Old 06-25-2015
Regex matching column awk

Hi all,

I want to extract rows with the pattern ALPHANUMERIC/ALPHANUMNERIC in the 2nd column.
I dont wan rows with more than 1 slash or without any slash in 2nd column.


Code:
a a/b
b a/b/c
c a/b//c
d t/y
e r
f /f

I came up with the regex

Code:
grep '[a-zA-Z0-9]\/[a-zA-Z0-9]$' file
a a/b
b a/b/c
d t/y

but I don't want "a/b/c", since it has 2 slashes.

Also since I want to match only the 2nd column, (column 197 in the real data), I need to use awk to match the regex.


In that case

Code:
awk  'match($2,/[a-zA-Z0-9]\/[a-zA-Z0-9]$/){ print }' file

where I can replace $2 with $197 in the real data,

but this doesn't produce any output at all.

Please assist where I`m going wrong.
# 2  
Old 06-25-2015
Code:
awk '$2~"^[^/]/[^/]$"' file

Since $2 is the 2nd (white-space-separated) field, you can use the ^ and $ anchors.
For your attempt, simply add the left anchor:
Code:
awk  'match($2,/^[a-zA-Z0-9]\/[a-zA-Z0-9]$/){ print }' file

With egrep (or grep -E) you have to insist on a leading space:
Code:
egrep '[[:space:]][^/]/[^/]$' file


Last edited by MadeInGermany; 06-25-2015 at 01:04 PM..
# 3  
Old 06-25-2015
Using your regex
Code:
awk '$2 ~ /^[a-zA-Z0-9]\/[a-zA-Z0-9]$/ {$2=$197}1' file


Last edited by Aia; 06-25-2015 at 01:46 PM.. Reason: add file
# 4  
Old 06-25-2015
Try also
Code:
awk '2==split($2,T,"/")' file
a a/b
d t/y
f /f

The last line would fulfill your requirement of exactly one slash, but no alphanumeric char in front.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #442
Difficulty: Easy
84% of the women surveyed by Cosmopolitan Magazine in 2018 indicated they would prefer to date a computer programmer than a rock star.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching column value from 2 different file using awk and append value from different column

Hi, I have 2 csv files. a.csv HUAWEI,20LMG011_DEKET_1296_RTN-980_IDU-1-11-ISV3-1(to LAMONGAN_M),East_Java,20LMG011_DEKET_1296_RTN-980_IDU-1,20LMG011,20LMG 027_1287_LAMONGAN_RTN980_IDU1,20LMG027,1+1(HSB),195.675,20LMG011-20LMG027,99.9995,202.6952012... (7 Replies)
Discussion started by: tententen
7 Replies

2. Shell Programming and Scripting

How to use regex on particular column (Removing comma from particular column)?

Hi, I have pipe separated file which contains some data having comma(,) in it. I want to remove the comma(,) only from particular column without changing data in other columns. Below is the sample data file, I want to remove the comma(,) only from 5th column. $ cat file1 ABC | DEF, HIJ|... (6 Replies)
Discussion started by: Prathmesh
6 Replies

3. Shell Programming and Scripting

awk split columns after matching on rows and summing the last column

input: chr1 1 2 3 chr1 1 2 4 chr1 2 4 5 chr2 3 6 9 chr2 3 6 10 Code: awk '{a+=$4}END{for (i in a) print i,a}' input Output: chr112 7 chr236 19 chr124 5 Desired output: chr1 1 2 7 chr2 3 6 19 chr1 2 4 5 (1 Reply)
Discussion started by: jacobs.smith
1 Replies

4. Shell Programming and Scripting

Column string matching in awk

Hello, I want pick up rows from input with conditions: 1) col2 is 2 replicate of col1 joint with "/" 2) col3 is a joint string as replicate of each side of the "/" symbol 3) col2 not equal to col3 input: TG TG/TG TG/TGG C C/C C/CG C C/G CA/CA C C/C CA/CA AG AG/AG... (3 Replies)
Discussion started by: yifangt
3 Replies

5. UNIX for Dummies Questions & Answers

Regex matching with grep -l

I am trying to find patterns in files using grep -l -e. I specifically am searching for abc. I want any file that has abc in it, but not just the letters abc. I am searching for a pattern a followed by b followed by c. I have tried egrep -l and also I have tried the following: grep -el... (2 Replies)
Discussion started by: newbie2010
2 Replies

6. UNIX for Advanced & Expert Users

awk print all fields except matching regex

grep -v will exclude matching lines, but I want something that will print all lines but exclude a matching field. The pattern that I want excluded is '/mnt/svn' If there is a better solution than awk I am happy to hear about it, but I would like to see this done in awk as well. I know I can... (11 Replies)
Discussion started by: glev2005
11 Replies

7. Shell Programming and Scripting

awk print non matching lines based on column

My item was not answered on previous thread as code given did not work I wanted to print records from file2 where comparing column 1 and 16 for both files find rows where column 16 in file 1 does not match column 16 in file 2 Here was CODE give to issue ~/unix.com$ cat f1... (0 Replies)
Discussion started by: sigh2010
0 Replies

8. Shell Programming and Scripting

Perl: Regex, string matching

Hi, I've a logfile which i need to parse and get the logs depending upon the user input. here, i'm providing an option to enter the string which can be matched with the log entries. e.g. one of the logfile entry reads like this - $str = " mpgw(BLUESOAPFramework):... (6 Replies)
Discussion started by: butterfly20
6 Replies

9. Shell Programming and Scripting

awk pattern matching problem -not recognizing a column

Hi all, I am new to awk. I want to print the line numbers if the column has a particular value. For example I have: cat FILE1 COL1 COL2 X114 0 X116 0 X117 0 X120 0 X121 0 X125 0 X126 0 X127 0 X131 1 X132 0 X135 0 X136 0 (3 Replies)
Discussion started by: newpro
3 Replies

10. Shell Programming and Scripting

REGEX: Matching Null?

I'm using the URL Regex feature of Squid for allowing sites via a list of regex strings to match allowed domains. The regex was actually copied from our previous proxy solution and it seemed to "just work". But, we've recently discovered that some domains (likely due to virtual hosts or host... (2 Replies)
Discussion started by: deckard
2 Replies

Featured Tech Videos