Awk search for string pattern in delimited file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk search for string pattern in delimited file
# 1  
Old 08-05-2010
Awk search for string pattern in delimited file

I've got a semicolon delimited file. I would like to search for fields that match a pattern, and not hardcoded eg "mth".
*th=something

If the delimited field fulfills this condition, eg. mth=value
I would like to print out both key and value for some number comparison.

eg. if value > "12" print "invalid mth"
if value > "0" && < 13 print "valid mth"

Is it possible to do this without reading the file by byte?

File that I have
Code:
LOG 

1997;Ford;E350;"ac;abs;moon";3000.00;
1999;Chevy;"Venture"";mth=/07;
xtendedEdition""";"";4900.00;
1999;Chevy;"Venture""ExtendedEdition;VeryLarge""";"";5000.00;
1996;Jeep;GrandCherokee;"MUSTSELL!;
air;month=08;loaded";4799.00;

1997;Ford;E350;"ac;abs;moon";3000.00;1999;Chevy;"Venture""Exte 
ndedEdition""";"";4900.001999;Chevy;"Venture""ExtendedEditi
on;VeryLarge""";"";5000.00
1996;Jeep;mth=Aug/2010;"MUSTSELL!
air;moonroof;loaded";4799.00

Output that I'm interested in
Code:
mth=/07
month=08

mth=Aug/2010

Thank you.
# 2  
Old 08-05-2010
Try this:
Code:
awk -F\; '{for(i=1;i<=NF;i++) {if($i ~ /m*th/){print $i}}}' file

# 3  
Old 08-05-2010
Thank you for your prompt reply.

I wish to perform some additional functions in your current command. I would like to get the key and value pair.

Code:
FS="="  
split($i, MTH)
key = MTH[1]
value = MTH[2]

FS="/" # if there is no / separator, can I just check the value?
split($value, MTHCHECK)
oldmth=value[1]
newmth=value[2]
# if the value is null -z just skip the check
if oldmth > "12" print "invalid mth" 
if oldmth > "0" && < "13" print "valid mth"
if newmth> "12" print "invalid mth" 
if newmth > "0" && < "13" print "valid mth"

How do i incorporate all this into an awk command? I am using bash by the way. Thanks again.
# 4  
Old 08-05-2010
Quote:
Originally Posted by alienated
Thank you for your prompt reply.

I wish to perform some additional functions in your current command. I would like to get the key and value pair.

Code:
FS="="  
split($i, MTH)
key = MTH[1]
value = MTH[2]

FS="/" # if there is no / separator, can I just check the value?
split($value, MTHCHECK)
oldmth=value[1]
newmth=value[2]
# if the value is null -z just skip the check
if oldmth > "12" print "invalid mth" 
if oldmth > "0" && < "13" print "valid mth"
if newmth> "12" print "invalid mth" 
if newmth > "0" && < "13" print "valid mth"

How do i incorporate all this into an awk command? I am using bash by the way. Thanks again.
The awk command gives the output as you desire in your first post.

Can you post the exact desired output from the given log file?
# 5  
Old 08-05-2010
Basically, I expected 5 kinds of output after the = sign.

Code:
# '/' slash is the delimiter
month=;  #No Month Value
month=/;  #No Month Value
month=01/02;  #Valid Month/Valid Month
month=/02;  #No Month Value/Valid Month
month=13/;  #Invalid Month/No Month Value

given the file in my first post, and the validation logic in my second post, i would like to print
Code:
#original value followed by validation results
mth=/07  No Month Value/Valid Month 
month=08 Valid Month

mth=Aug/2010 Invalid Month/Invalid Month

I hope you can understand what I am trying to put across
# 6  
Old 08-05-2010
Why do you use another username?

Quote:
Originally Posted by milo7
Basically, I expected 5 kinds of output after the = sign.
Much more I'm afraid..

Maybe it's easier to pipe the output of the first awk command to another awk command, play around with something like:

Code:
awk -F\; '{for(i=1;i<=NF;i++) {if($i ~ /m*th/){print $i}}}' file |
awk -F"[/=]" '{
  if($2=="" && $3==""){print $0, " #No Month Value/ #No Month Value";next}
  if($2=="" && $3==""){print $0, " #No Month Value/ #No Month Value";next}
  if(int($2)!= $2 && $3==""){print $0, " #Invalid Month / #No Month Value";next}
  .
  .
  .
'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. UNIX for Beginners Questions & Answers

Search a string inside a pattern matched block of a file

How to grep for searching a string within a begin and end pattern of a file. Sent from my Redmi 3S using Tapatalk (8 Replies)
Discussion started by: Baishali
8 Replies

3. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies

4. Shell Programming and Scripting

awk read one delimited file, search another delimited file

Hello folks, I have another doozy. I have two files. The first file has four fields in it. These four fields map to different locations in my second file. What I want to do is read the master file (file 2 - 23 fields) and compare each line against each record in file 1. If I get a match in all four... (4 Replies)
Discussion started by: dagamier
4 Replies

5. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

6. Shell Programming and Scripting

awk get search pattern from a file.

Here Is a problem I am facing with awk. Query --> I want to search for a string in a file and print next 15 lines below the matched string. 1.We do not have GNU grep so cannot use grep -A or grep -B commands. 2. Instead of passing the search pattern as a string to awk. I want the awk to... (4 Replies)
Discussion started by: togotutor
4 Replies

7. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

8. UNIX for Dummies Questions & Answers

Search and replace string only in a particular column in a delimited file

I have file with multiple columns. Column values for a record may be same. Now i have to replace a column value(this can be same for the other columns) with new value. File.txt A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D... (1 Reply)
Discussion started by: ksailesh
1 Replies

9. UNIX for Dummies Questions & Answers

Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK?

Hey Everybody, I am having much trouble figuring this out, as I am not really a programmer..:mad: Datafile.txt Column0 Column1 Column2 ABC DEF xxxGHI I am running using WGET on a cronjob to grab a datafile, but I need to cut the first three characters from... (6 Replies)
Discussion started by: rickdini
6 Replies

10. Shell Programming and Scripting

Search Mulitiple String pattern in a file

Hi, I need to search for a multiple string pattern(5 key words) in a file(similar to a flat file) ,and i need to store the output in a another file . In that file we may have mutiple occurrences of the key words.and i need only the unique. kindly help out. Thanks, Mohana Krishnan (2 Replies)
Discussion started by: krishnan_6015@y
2 Replies
Login or Register to Ask a Question