awk searching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk searching
# 1  
Old 06-02-2010
Question awk searching

hi all,


Please can you help me with the awk searching where:

I have a master file where I need the string $12.

the string $ 12 varies between 3 and 4 characters and I need to bring only the characters ending in 68.

try this, but is so bad my search i know

Code:
awk-F, '(if (substr ($  12,3,2) == "68") (print; next) else (print "dont exist"))' 20100525 *

regards


thanks

Last edited by Scott; 06-02-2010 at 06:09 PM.. Reason: Code tags, please...
# 2  
Old 06-02-2010
Not exactly sure, but...

Code:
awk '$12 !~ /^..?68$/ { print "dont match"; next } 1' file

This User Gave Thanks to Scott For This Post:
# 3  
Old 06-02-2010
1. There should be a space between awk and -F
2. There should be no space between $ and 12
3. Since the field can be 3 or 4 characters I would not use substr , but use a pattern match:
4. Use curly braces instead of parentheses in most places
5. If you use 20100525 * it means read file named "20100525" and subsequently read any file in the directory including file "20100525"for the second time if it exists. Do you mean 20100525* (any file that begins with 20100525)?

After applying these changes you end up with something like this:
Code:
awk -F, '{if ($12~/68$/) {print} else print "dont exist"}' 20100525*

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 06-02-2010
Lightbulb

hi all

thanks for ur help, yeah i search the file that begins with 20100525.


i doing execute ur sentences.

regards

---------- Post updated at 05:39 PM ---------- Previous update was at 04:31 PM ----------

hi all

unexpected results..


i extract the file info is very extense.... the strings exits in the file but have 3 or 4 characters

example

1268
368


try the easy example
Code:
awk -F, '$12~/68$/  {print}' 20100525*


the result is root Prompt >


regards
# 5  
Old 06-02-2010
Quote:
Originally Posted by manzi
...
hi all
unexpected results..
i extract the file info is very extense.... the strings exits in the file but have 3 or 4 characters

example
1268
368

try the easy example
Code:
awk -F, '$12~/68$/  {print}' 20100525*

the result is root Prompt >
...
Well, can you cut/paste the portion of your file that you expect to be captured by the awk script ?

The awk one-liner works well with my test files.

Code:
$ 
$ 
$ # how many files have names starting with "20100525" ?
$ ls -1 20100525*
20100525_myfile1.txt
20100525_myfile2.txt
$ 
$ 
$ # show the contents of both these files
$ cat 20100525_myfile1.txt
1,2,3,4,5,6,7,8,9,10,11,1267,13,14
1,2,3,4,5,6,7,8,9,10,11,1268,13,14
1,2,3,4,5,6,7,8,9,10,11,1269,13,14
$ 
$ cat 20100525_myfile2.txt
1,2,3,4,5,6,7,8,9,10,11,367,13,14
1,2,3,4,5,6,7,8,9,10,11,368,13,14
1,2,3,4,5,6,7,8,9,10,11,369,13,14
$ 
$ # now run the awk one-liner on these files
$ awk -F, '$12~/68$/  {print}' 20100525*
1,2,3,4,5,6,7,8,9,10,11,1268,13,14
1,2,3,4,5,6,7,8,9,10,11,368,13,14
$ 
$ # the result is as expected
$

Just a thought - maybe you have white space at the end of the 12th field, after "68" and before the 12th comma ?

Code:
$ 
$ cat 20100525_myfile2.txt
1,2,3,4,5,6,7,8,9,10,11,367,13,14
1,2,3,4,5,6,7,8,9,10,11,368  ,13,14
1,2,3,4,5,6,7,8,9,10,11,369,13,14
$ 
$ awk -F, '$12~/68$/  {print}' 20100525_myfile2.txt
$ 
$ # doesn't work
$ # so try this
$ awk -F, '$12~/68 *$/  {print}' 20100525_myfile2.txt
1,2,3,4,5,6,7,8,9,10,11,368  ,13,14
$ 
$


HTH,
tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 6  
Old 06-03-2010
hi,

dont work the code.

this is the file example:

my file name 20100526_113020_ok. the red number is my result.

2470,XXXXXXXXXXX,5-26-2010,37091,5-26-2010,55091,202,304,128D5313490C0A89FFFFFFFFEE9C957D,10465,0,0,0,568,,269,0,0,0,701,1001,101,-1,0, ,0,269,35,3/6/2009 15:8:0,1/1/1900 0:0:0,3/6/2009 15:8:0,0,0,0,0,,0,0,0,0,0,0,1,2,5,213,998252,50,-1,0,XXXXXXXXXXXXX,740010126242881,XXXXXXXXXXX,2,1,740010124932418,
2470,XXXXXXXXXXX,5-26-2010,37091,5-26-2010,55091,202,304,128D5313490C0A89FFFFFFFFEE9C957D,10465,0,0,0,1266,,269,0,0,0,701,1001,101,-1,0, ,0,269,35,3/6/2009 15:8:0,1/1/1900 0:0:0,3/6/2009 15:8:0,0,0,0,0,,0,0,0,0,0,0,1,2,5,213,998252,50,-1,0,XXXXXXXXXXXXX,740010126242881,XXXXXXXXXXX,2,1,740010124932418,
2470,XXXXXXXXXXX,5-26-2010,37091,5-26-2010,55091,202,304,128D5313490C0A89FFFFFFFFEE9C957D,10465,0,0,0,449,,269,0,0,0,701,1001,101,-1,0, ,0,269,35,3/6/2009 15:8:0,1/1/1900 0:0:0,3/6/2009 15:8:0,0,0,0,0,,0,0,0,0,0,0,1,2,5,213,998252,50,-1,0,XXXXXXXXXXXXX,740010126242881,XXXXXXXXXXX,2,1,740010124932418,
2470,XXXXXXXXXXX,5-26-2010,37091,5-26-2010,55091,202,304,128D5313490C0A89FFFFFFFFEE9C957D,10465,0,0,0,569,,269,0,0,0,701,1001,101,-1,0, ,0,269,35,3/6/2009 15:8:0,1/1/1900 0:0:0,3/6/2009 15:8:0,0,0,0,0,,0,0,0,0,0,0,1,2,5,213,998252,50,-1,0,XXXXXXXXXXXXX,740010126242881,XXXXXXXXXXX,2,1,740010124932418,
2470,XXXXXXXXXXX,5-26-2010,37091,5-26-2010,55091,202,304,128D5313490C0A89FFFFFFFFEE9C957D,10465,0,0,0,768,,269,0,0,0,701,1001,101,-1,0, ,0,269,35,3/6/2009 15:8:0,1/1/1900 0:0:0,3/6/2009 15:8:0,0,0,0,0,,0,0,0,0,0,0,1,2,5,213,998252,50,-1,0,XXXXXXXXXXXXX,740010126242881,XXXXXXXXXXX,2,1,740010124932418,
2470,XXXXXXXXXXX,5-26-2010,37091,5-26-2010,55091,202,304,128D5313490C0A89FFFFFFFFEE9C957D,10465,0,0,0,1268,,269,0,0,0,701,1001,101,-1,0, ,0,269,35,3/6/2009 15:8:0,1/1/1900 0:0:0,3/6/2009 15:8:0,0,0,0,0,,0,0,0,0,0,0,1,2,5,213,998252,50,-1,0,XXXXXXXXXXXXX,740010126242881,XXXXXXXXXXX,2,1,740010124932418,
2470,XXXXXXXXXXX,5-26-2010,37091,5-26-2010,55091,202,304,128D5313490C0A89FFFFFFFFEE9C957D,10465,0,0,0,344,,269,0,0,0,701,1001,101,-1,0, ,0,269,35,3/6/2009 15:8:0,1/1/1900 0:0:0,3/6/2009 15:8:0,0,0,0,0,,0,0,0,0,0,0,1,2,5,213,998252,50,-1,0,XXXXXXXXXXXXX,740010126242881,XXXXXXXXXXX,2,1,740010124932418,
2470,XXXXXXXXXXX,5-26-2010,37091,5-26-2010,55091,202,304,128D5313490C0A89FFFFFFFFEE9C957D,10465,0,0,0,668,,269,0,0,0,701,1001,101,-1,0, ,0,269,35,3/6/2009 15:8:0,1/1/1900 0:0:0,3/6/2009 15:8:0,0,0,0,0,,0,0,0,0,0,0,1,2,5,213,998252,50,-1,0,XXXXXXXXXXXXX,740010126242881,XXXXXXXXXXX,2,1,740010124932418,

i execute

Code:
> awk -F, '$12~/68 *$/  {print}' 20100526*

but result is prompt >

doesnt work.

thanks

regards!

Last edited by manzi; 06-03-2010 at 11:47 AM..
# 7  
Old 06-03-2010
Hi.

I think the field you are looking for is field 14, not 12?:

Code:
$ awk -F, '$14~/68 *$/' file1
2470,XXXXXXXXXXX,5-26-2010,37091,5-26-2010,55091,202,304,128D5313490C0A89FFFFFFFFEE9C957D,10465,0,0,0,568,,269....
15:8:0,0,0,0,0,,0,0,0,0,0,0,1,2,5,213,998252,50,-1,0,XXXXXXXXXXXXX,740010126242881,XXXXXXXXXXX,2,1,740010124932418,
2470,XXXXXXXXXXX,5-26-2010,37091,5-26-2010,55091,202,304,128D5313490C0A89FFFFFFFFEE9C957D,10465,0,0,0,768,,269,...
15:8:0,0,0,0,0,,0,0,0,0,0,0,1,2,5,213,998252,50,-1,0,XXXXXXXXXXXXX,740010126242881,XXXXXXXXXXX,2,1,740010124932418,
2470,XXXXXXXXXXX,5-26-2010,37091,5-26-2010,55091,202,304,128D5313490C0A89FFFFFFFFEE9C957D,10465,0,0,0,1268,,269,...
15:8:0,0,0,0,0,,0,0,0,0,0,0,1,2,5,213,998252,50,-1,0,XXXXXXXXXXXXX,740010126242881,XXXXXXXXXXX,2,1,740010124932418,
2470,XXXXXXXXXXX,5-26-2010,37091,5-26-2010,55091,202,304,128D5313490C0A89FFFFFFFFEE9C957D,10465,0,0,0,668,,269,...
15:8:0,0,0,0,0,,0,0,0,0,0,0,1,2,5,213,998252,50,-1,0,XXXXXXXXXXXXX,740010126242881,XXXXXXXXXXX,2,1,740010124932418,

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching content of a variable using awk

so i'm running a variation of the following command a few times in my script: echo "${TOTALRunning}" | awk -F"" '/'"${PROCSEARCH}"'/ {print $2}' | tr '\n' '|' unfortunately i cant paste the content of the variable TOTALRunning into this thread for security reasons. what i want to do is... (9 Replies)
Discussion started by: SkySmart
9 Replies

2. Shell Programming and Scripting

Searching value in table through awk

I need to create one script in which I want to search in txt file , this txt file is having 10 columns , I want to check 4th column value if "BOY" & 10th column value =>500 it will print 4th column row value ,1st column row value & 10th column row value & store the same value in one file as... (2 Replies)
Discussion started by: dravi_laxmi
2 Replies

3. Shell Programming and Scripting

Searching multiple patterns using awk

Hello, I have the following input file: qh1adm 20130710111201 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qh1adm 20130711151154 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qx1adm 20130711151154 : tp count QX1 u6 -Dsourcesystems=B17,E17,EE7 qh1adm 20130711151155 : tp import all... (7 Replies)
Discussion started by: kcboy
7 Replies

4. Shell Programming and Scripting

awk: searching for non-breaking-space

This code shal search for the non-breaking space 0xA0 though it returns the error "fatal: attempt to use scalar 'nbs' as array" Can somebody help? awk --non-decimal-data -v nbs="0xA0" '{if($0 in nbs) {print FILENAME, NR}}' *.txt (1 Reply)
Discussion started by: sdf
1 Replies

5. Shell Programming and Scripting

Searching a file using awk or sed

I want to search a file in a specific location and I don't want to use find command. I want to give the path also where the file is for searching it. Pls help (3 Replies)
Discussion started by: maitree
3 Replies

6. Shell Programming and Scripting

Searching using awk - Help required

Hi... I am working on script to search some records in a file based on certain fields and each record is a ASCII fixed size. I was using awk to search based on certain condition. But the length of the record is too much that awk is giving syntax error near unexpected token `(' Request... (5 Replies)
Discussion started by: ysrikanth
5 Replies

7. Shell Programming and Scripting

awk and searching within a block ?

Hi, I wonder if anybody could help. How do i awk out (or indeed using another utility) a particular value that exists within a defined block, for example if i have a file that looks like the one below and i want to get at the "Product Serial" for the block referring to "mb.fru" (bolded and coloured... (4 Replies)
Discussion started by: rethink
4 Replies

8. Shell Programming and Scripting

Searching word in a file with awk

Hello everyone! I use a script to query for installed packages with yum (I use RHEL 4 with yum installed) and the output is redirected to a file. My script scan this file to find a package and the version of it. The script works fine until I search for a package name with special characters.... (3 Replies)
Discussion started by: studieu
3 Replies

9. Shell Programming and Scripting

awk searching between two files

hi there, im writing some script in awk; in few words i have a list from router (mac address- ip address) and the second list with only the mac addresses. the thing is that i want search list from router for the first mac address; if found - print the ip address, if not print error; then search... (1 Reply)
Discussion started by: mac7
1 Replies

10. Shell Programming and Scripting

Searching for + sign in a string using awk

Hi All, My query is: I have string say xyz+ how to determine that whether it ends with a + sign or not using awk command. (5 Replies)
Discussion started by: satyajit2512
5 Replies
Login or Register to Ask a Question