Use to awk to match pattern, and print the pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use to awk to match pattern, and print the pattern
# 1  
Old 09-22-2009
Use to awk to match pattern, and print the pattern

Hi,


I know how to use awk to search some expressions like five consecutive numbers, [0-9][0-9][0-9][0-9][0-9], this is easy.
However, how do I make awk print the pattern that is been matched?

For example:
input: usa,canada99292,japan222,france59664,egypt223
output:99292,59664
# 2  
Old 09-22-2009
Should work for you Smilie
Code:
awk -F, '{for(i=0;++i<=NF;){gsub("[a-z]","",$i);if(length($i)==5){a=(a)?a FS$i:$i}}{print a;a=""}}' file

To keep the forums high quality for all users, please take the time to format your posts correctly.
  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote

Last edited by danmero; 09-22-2009 at 08:14 PM..
# 3  
Old 09-23-2009
Hi Dan,

Thanks a lot for your reply. But the code won't work in instances such as this:
input:
Code:
usa,66 canada99292,japan222,44 france59664,egypt223

because within each column, the field separator actually changes to space

Is it possible to have multiple field separator in place simultaneously, in this case: space and comma. If this is possible, then your code would work .

Last edited by vgersh99; 09-23-2009 at 12:46 PM.. Reason: code tags, PLEASE!
# 4  
Old 09-23-2009
If your version of awk supports regex as RS, try this:

Code:
awk -v RS="[ ,]" -v ORS="," '/[0-9][0-9][0-9][0-9]/ {gsub(/[a-z]/,"");print}' file

# 5  
Old 09-23-2009
With a slightly change of the code of danmero:

Code:
awk -F",| " '{for(i=0;++i<=NF;){gsub("[a-z]","",$i);if(length($i)==5){a=(a)?a "," $i:$i}}{print a;a=""}}'

# 6  
Old 09-23-2009
Quote:
Originally Posted by grossgermany
Hi,


I know how to use awk to search some expressions like five consecutive numbers, [0-9][0-9][0-9][0-9][0-9], this is easy.
However, how do I make awk print the pattern that is been matched?

For example:
input: usa,canada99292,japan222,france59664,egypt223
output:99292,59664

if you have other options like grep or sed:

Code:
grep -o pattern file

or

Code:
sed -n '/pattern/ s/.*\(pattern\).*/\1/p' file

# 7  
Old 09-23-2009
Quote:
Originally Posted by anchal_khare
if you have other options like grep or sed:
Yes but you should check the OP required output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

3. Shell Programming and Scripting

Print only next pattern in a line after a pattern match

I have 2013-06-11 23:55:14 1Umexd-0004cm-IG <= user@domain.com I need sed/awk operation on this, so that it should print the very next pattern only after the the pattern mach <= ie only print user@domain.com (7 Replies)
Discussion started by: anil510
7 Replies

4. 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

5. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

6. Shell Programming and Scripting

Script to match a pattern and print only the pattern and after that

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ...... .......xyz: abz: ..... I have tried using awk and cut, bu the position of these values keep changing, so I can use awk and split it into columns. ... (14 Replies)
Discussion started by: Serena
14 Replies

7. Shell Programming and Scripting

awk Help: Horizontal to Vertical print with pattern match

Hi AWK Experts, Following is the data : BRH113 DD AA HH CA DD DD AA HH BRH091 A4 A6 AH H7 67 HH J8 9J BRH0991 AA D8 C23 V5 H7 BR2 BRH991 AA HH GG5 BT0 JJ0 I want the output to be alligned with the pattern matching "BRH" inthe line. The output should be look like: A]... (4 Replies)
Discussion started by: rveri
4 Replies

8. Shell Programming and Scripting

Print strings that match pattern with awk

I have a file with many lines which contain strings like .. etc. But with no rule regarding field separators or anything else. I want to print ONLY THE STRING from each line , not the entire line !!! For example from the lines : Flow on service executed with success in . Performances... (5 Replies)
Discussion started by: black_fender
5 Replies

9. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

10. Shell Programming and Scripting

AWK, print no of records after pattern match.

Hi ALL :). i have a file, cat 3 + dog 5 + rat 6 - i want to print no of record having pattern "+". thanks in advance :confused:. (2 Replies)
Discussion started by: admax
2 Replies
Login or Register to Ask a Question