Problem in Pattern-Specific Actions in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in Pattern-Specific Actions in awk
# 1  
Old 02-07-2013
Problem in Pattern-Specific Actions in awk

Hi

i am in learning phase of unix
i am able to understand basic of awk but not able to understand Pattern-Specific Actions in awk below is the snippet .

Code:
awk '
/ *\$[1-9][0-9]*\.[0-9][0-9] */ { print $1,$2,$3,"*"; }
/ *\$0\.[0-9][0-9] */ { print ; }
' fruit_prices.txt

here i am not getting the use of wild card. what actually they are doing.

Regards.
Scriptor
# 2  
Old 02-07-2013
Hi,

Please post sample input and the output you are getting with this script. It would be easy to explain with sample data.

Regards,
Ravi
# 3  
Old 02-07-2013
Hi Panyam,

the I/P file is
Code:
Banana     $0.89           100
Peach      $0.79            65
Kiwi       $1.50            22
Pineapple  $1.29            35
Apple      $0.99            78

and the O/P is

Code:
Banana                   $0.89       100
Peach                     $0.79       65
Kiwi $1.50 22 *
Pineapple $1.29 35 *
Apple                    $0.99        78

# 4  
Old 02-07-2013
Code:
/              pattern start
 *             zero or more (= any number) spaces
\$             exactly the dollar sign
[1-9]          one digit, excluding (leading) 0
[0-9]*         zero or more digits
\.             exactly the period
[0-9][0-9]     2 digits
  *            zero or more (= any number) spaces
/              pattern end

this pattern would match the kiwi and pineapple prices in your file, which are >= 1$, and print them with a trailing "*"

/ *\$0\.[0-9][0-9] */ this pattern will match all prices below 1$ ("$0."), and print the input line (awk variable $0) unmodified.

Pls note that this pattern matching would be susceptible to false triggers were above strings matched anywhere in $0. It would be safer to match against $2, the price column in your file.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-07-2013
thx a lot Rudic Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find specific pattern and change some of block values using awk

Hi, Could you please help me finding a way to replace a specific value in a text block when matching a key pattern ? I got the keys and the values from a command similar to: echo -e "key01 Nvalue01-1 Nvalue01-2 Nvalue01-3\nkey02 Nvalue02-1 Nvalue02-2 Nvalue02-3 \nkey03 Nvalue03-1... (2 Replies)
Discussion started by: alex2005
2 Replies

2. Shell Programming and Scripting

awk to remove mutiple values from specific pattern, leaving a single value

In the awk below I am trying to remove all instances after a ; (semi-colon) or , (comma) in the ANN= pattern. I am using gsub to substitute an empty string in these, so that ANN= is a single value (with only one value in it the one right after the ANN=). Thank you :). I have comented my awk and... (11 Replies)
Discussion started by: cmccabe
11 Replies

3. Shell Programming and Scripting

awk working inside specific pattern ranges

Hi, I have a text file, which I am trying to parse. File contents: BEG Id Job1 Id Stage1 1 EN Id Job2 Id Stage2 BEG Id2 Job3 Id Stage4 2 EN I have to process the data in this between every BEG and EN. so I am trying to restrict the range and inside every... (1 Reply)
Discussion started by: Kulasekar
1 Replies

4. Shell Programming and Scripting

Execution problem with print out record that follow specific pattern

Hi, Do anybody know how to print out only those record that column 1 is "a" , then followed by "b"? Input file : a comp92 2404242 2405172 b comp92 2405303 2406323 b comp92 2408786 2410278 a comp92 2410271 2410337 a comp87 1239833 1240418 b comp87... (3 Replies)
Discussion started by: patrick87
3 Replies

5. Shell Programming and Scripting

awk to print record not equal specific pattern

how to use "awk" to print any record has pattern not equal ? for example my file has 5 records & I need to get all lines which $1=10 or 20 , $2=10 or 20 and $3 greater than "130302" as it shown : 10 20 1303252348212B030 20 10 1303242348212B030 40 34 1303252348212B030 10 20 ... (14 Replies)
Discussion started by: arm
14 Replies

6. Shell Programming and Scripting

perform actions at specific locations in data frame

Hi everyone, I got a data frame like the one below and and would like to do the following: Ignore the first 3 rows and check in all following rows the second position. If the value is >500, subtract 100. Example DF: ABC 22 DE 12 BCD 223 GH 12 EFG 2104 DH ... (4 Replies)
Discussion started by: TuAd
4 Replies

7. Shell Programming and Scripting

awk to sum specific field when pattern matches

Trying to sum field #6 when field #2 matches string as follows: Input data: 2010-09-18-20.24.44.206117 UOWEXEC db2bp DB2XYZ hostname 1 2010-09-18-20.24.44.206117 UOWWAIT db2bp DB2XYZ hostname ... (3 Replies)
Discussion started by: ux4me
3 Replies

8. Shell Programming and Scripting

Remove specific pattern header and its content problem facing

Input file: >TRACK: Position: 1 TYPE: 1 Pos: SVAVPQRHHPGGTVFREPIIIPAIPRLVPGWNKPIIIGRHAFGDQYRATDRVIPGPGKLE LVYTPVNGEPETVKVYDFQGGGIAQTQYNTDESIRGFAHASFQMALLKGLPLYMSTKNTI LKRYDGRFKDIFQEIYESTYQKDFEAKNLWYEHRLIDDMVAQMIKSEGGFVMALKNYDGD >TRACK: Position: 1 TYPE: 2 Pos: FAHASFQMALLKGLPLYMS... (8 Replies)
Discussion started by: patrick87
8 Replies

9. Shell Programming and Scripting

Added new line before a specific pattern problem asking

Input file: Sample1 Type pattern 842 3150 Sample1 Type range 842 3150 Sample1 Type pattern 842 1127 Sample1 Type option 842 1127 Sample1 Type length 1483 1603 Sample1 Type pattern 1483 1603 Sample1 Type length 1698 1747 Sample1 Type option 1698 1747 Sample1 Type length 1868 1935 Sample1... (13 Replies)
Discussion started by: patrick87
13 Replies

10. Shell Programming and Scripting

Perform a set of actions for a specific file type

Hello, I have a problem that I'm having quite a bit of trouble with. I am trying to create a script that performs a specific sequence of actions for a file of a specific type. This is an abbreviated version of my basic script: #!/bin/sh #coulombic calculations... (2 Replies)
Discussion started by: oehtus
2 Replies
Login or Register to Ask a Question