Match pattern in a field, print pattern only instead of the entire field


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Match pattern in a field, print pattern only instead of the entire field
# 8  
Old 03-02-2012
Quote:
Originally Posted by lucasvs
Code:
awk '$2 {gsub(/ /,"");print}' file.tab

awk splits the line into fields based on whitespaces by default. So $2 is not "bb11 toma toes 111b bbbbb" but just "bb11". Tell awk to split fields based on tab '\t' and then $2 would be what you think it is (unless ofcourse, columns are tab separated).

Code:
awk -F'\t' 'BEGIN{OFS="\t"} {gsub(/ /,"",$2); print}' file.tab

This User Gave Thanks to balajesuri For This Post:
# 9  
Old 03-02-2012
Thanks it is very helpful. I understand now.

I notice that this work as well:
Code:
awk -F'\t'  '{gsub(" ","",$6); print $0}' file.tab

---------- Post updated at 02:33 AM ---------- Previous update was at 02:25 AM ----------

About the initial perl command you gave, sometimes I have this error message bout the quantifiers:
Code:
Nested quantifiers in regex; marked by <-- HERE in m/([1-9]?{ <-- HERE 0,10}tomatoes[1-9]?{0-10})/ at -e line 1.

# 10  
Old 03-02-2012
Please post the exact perl one-liner you used.

From the error message, I can only guess that you used something like this: /([1-9]?{0,10}tomatoes[1-9]?{0,10}/.

What did you want to achieve? Why would you give '?' and then define intervals too?

? and {0,10} are both quantifiers.
? refers to zero or one match of the previous character.
{0,10} refers to zero to 10 matches of previous character.

? is a quantifier and so is {0,10}. In your regex, ? is followed by {0,10}, and hence the error message 'Nested quantifiers in regex'
# 11  
Old 03-03-2012
ok !

Actually I just remove the "?" as it is the same than "{0,x}".

I tested the perl command you gave earlier without the "?":
Code:
perl -lane 'if ($F[1] =~ /([13]{1,5}tomatoes[13]{1,5})/) {$F[1] =~ s/.*?($1).*/$1/; print join ("\t", @F)}' file.tab

It works on the example I gave in the post, but returns a blank file when I use it on my real files (the pattern is not in the 3rd but the 6th field).
Is your perl command dependent on the field in which the pattern is?
In other words, do I have to modify it if I want to search the pattern in the 6th field?
# 12  
Old 03-03-2012
If you are using gawk, it does not understand the regex repetition operator by default, even though it is specified in POSIX. Use gawk --posix
# 13  
Old 03-04-2012
* Thanks Scrutinizer !
I get it now.

* About balajesuri's perl command:
Code:
perl -lane 'if ($F[1] =~ /([13]{1,5}tomatoes[13]{1,5})/) {$F[1] =~ s/.*?($1).*/$1/; print join ("\t", @F)}' file.tab

It doesn't work on the following type of file (pattern to search/extract/replace in the 6th field, instead of the 3rd like in the example; total of 8 fields instead of 3 like in the example):
Code:
Field1   Field2   Field3   Field4   Field5   Field6   field7   Field8
A1   A2   A3   A4   A5   bb11tomatoes111bbbbbb   A7   A8
B1   B2   B3   B4   B5   eeee2222apples2eeeeeeeee   B7   B8
C1   C2   C3   C4   C5   hhhhhh33333tomatoes33333hhh   C7   C8

It returns the same entire file (matching and non-matching lines):
Code:
Field1   Field2   Field3   Field4   Field5   Field6   field7   Field8
A1   A2   A3   A4   A5   bb11tomatoes111bbbbbb   A7   A8
B1   B2   B3   B4   B5   eeee2222apples2eeeeeeeee   B7   B8
C1   C2   C3   C4   C5   hhhhhh33333tomatoes33333hhh   C7   C8

I tried, in the command, to divide the pattern into compartments using brackets:
Code:
perl -lane 'if ($F[1] =~ /(([13]{1,5})(tomatoes)([13]{1,5}))/) {$F[1] =~ s/.*?($1).*/$1/; print join ("\t", @F)}' file.tab

,and it returns a blank file.
# 14  
Old 03-04-2012
You are selecting on the sixth column instead of the second column in the example of the starting post, so then you need to replace F[1] with F[5] and are these fields tab separated? Slight modification of balajesuri's code:
Code:
perl -lane 'if ($F[5] =~ s/.*?([13]{1,5}tomatoes[13]{1,5}).*/$1/) {print join ("\t", @F)}' in file

Code:
perl -F\\t -lane 'if ($F[5] =~ s/.*?([13]{1,5}tomatoes[13]{1,5}).*/$1/) {print join ("\t", @F)}' infile


Last edited by Scrutinizer; 03-04-2012 at 10:11 PM..
This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get output of multiple pattern match from first field to a file

Hi All, Greetings! I have a file of 40000+ lines with different entries, I need matching entries filterd out to their files based on first filed pattern for the matching : For example: All server1 entries (in field1) to come together with its path in 2nd field. The best output I want... (9 Replies)
Discussion started by: rveri
9 Replies

2. Shell Programming and Scripting

Print field after pattern in all lines

data: hello--hello1--hello2--#growncars#vello--hello3--hello4--jello#growncars#dello--gello--gelloA--gelloB#growncars# I want to be able to print all the values that are found between the patterns "#growncars#" and the next "#growncars#" on the same line. so the output should be: ... (8 Replies)
Discussion started by: SkySmart
8 Replies

3. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

4. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

5. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

6. Shell Programming and Scripting

Displaying the first field if the second field matches the pattern using Perl

Hi, I am trying with the below Perl command to print the first field when the second field matches the given pattern: perl -lane 'open F, "< myfile"; for $i (<F>) {chomp $i; if ($F =~ /patt$/) {my $f = (split(" ", $i)); print "$f";}} close F' dummy_file I know I can achieve the same with the... (7 Replies)
Discussion started by: royalibrahim
7 Replies

7. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

8. Shell Programming and Scripting

print the last line of an recurring pattern on the 3rd field

How can i awk/sed to print the last line of an recurring pattern on the 3rd field? Input lines: 123456.1 12 1357911 11111.1 01 123456.2 12 1357911 11111.2 02 123456.3 12 1357911 11111.3 03 123456.4 12 1357911 11111.4 04 123456.5 12 1357911 11111.5 05 246810.1 12 1357911 22222.1 01... (4 Replies)
Discussion started by: ux4me
4 Replies

9. Shell Programming and Scripting

Print line if first Field matches a pattern

Hi All, I would like my code to be able to print out the whole line if 1st field has a dot in the number. Sample input and expected output given below. My AWK code is below but it can;t work, can any expert help me ? Thanks in advance. {if ($1 ~ /*\.*/) { print $0 }} Input: ... (2 Replies)
Discussion started by: Raynon
2 Replies

10. Shell Programming and Scripting

how do i pattern match a field with awk?

hi, let's say $numbers = "324 350 587" an so on... what i'm trying to do is this: awk -v numbers="$numbers" '{if (numbers ~ /$2/) print $0, "bla bla"}' file # file looks like this: 214 ..... 215 ... 216 .... 250 ... 324 325 ... 350 something ... ... 587 ... (4 Replies)
Discussion started by: someone123
4 Replies
Login or Register to Ask a Question