Find pattern with AWK

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Find pattern with AWK

#1 Post by darioit » 04 Dec 2010 00:55

Hello,

a few time ago someone help me with searching max lrecl in a file with awk script like this

Code: Select all

SET /a rec1=10
gawk "length>%rec1%{N+=1};END{exit N}" "C:\filename.txt"
echo %errorlevel%

and this return the number of line greather than lrecl rec1

Now I try do file all character non equal "pattern" to column 0,2 like this

Code: Select all

SET pattern=AA
gawk "substr(0,2)!=%pattern%{N+=1};END{exit N}" "C:\filename.txt"
echo %errorlevel%


content of "C:\filename.txt"
AA123456789
BB123456789

Thanks
Dario

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Find pattern with AWK

#2 Post by darioit » 07 Dec 2010 04:21

It's well accepted also Find and Findstr :lol:

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find pattern with AWK

#3 Post by aGerman » 11 Dec 2010 08:05

I've no idea how gawk works and thats why I don't know what output you need. But I think it should be one of these three data. Works with FINDSTR.

Code: Select all

@echo off &setlocal

echo(
echo( *  Display the lines:
findstr /vbc:"AA" "C:\file.txt"

echo(
echo( *  Display the line numbers:
for /f "delims=:" %%a in ('findstr /nvbc:"AA" "C:\file.txt"') do echo(%%a

echo(
echo( *  Display the number of lines:
set /a n=0
for /f %%a in ('findstr /vbc:"AA" "C:\file.txt"') do set /a n+=1
echo(%n%

pause>nul


Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Find pattern with AWK

#4 Post by ghostmachine4 » 26 Dec 2010 18:53

darioit wrote:Hello,

a few time ago someone help me with searching max lrecl in a file with awk script like this

Code: Select all

SET /a rec1=10
gawk "length>%rec1%{N+=1};END{exit N}" "C:\filename.txt"
echo %errorlevel%

and this return the number of line greather than lrecl rec1

Now I try do file all character non equal "pattern" to column 0,2 like this

Code: Select all

SET pattern=AA
gawk "substr(0,2)!=%pattern%{N+=1};END{exit N}" "C:\filename.txt"
echo %errorlevel%


content of "C:\filename.txt"
AA123456789
BB123456789

Thanks
Dario


pass your DOS variable to gawk using the -v option

Code: Select all

gawk -v pattern=%patterrn%  "$1 != pattern { N+=1}; END {exit N}" .......

you can use findstr but it will only find strings for you. No mechanism for programming. you have to use the DOS shell to do your addition.
gawk provides you both and much more!. So using gawk is better than findstr.

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Find pattern with AWK

#5 Post by rfpd » 26 Dec 2010 19:08

this is old post don't repost here please.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Find pattern with AWK

#6 Post by ghostmachine4 » 26 Dec 2010 19:50

rfpd wrote:this is old post don't repost here please.

Wrong, i can post whenever I want and its none of your business. Further more, the question asked about gawk, and I am the one who posted the original solution. So I am merely replying OP since no one provides a gawk solution. Nothing wrong with that.

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Find pattern with AWK

#7 Post by rfpd » 27 Dec 2010 05:48

Maybe he already found the solution months ago if he didn't he posted in a new thread not left the old. That's why i'm saying that.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Find pattern with AWK

#8 Post by ghostmachine4 » 27 Dec 2010 07:22

rfpd wrote:Maybe he already found the solution months ago if he didn't he posted in a new thread not left the old. That's why i'm saying that.

wrong. Even if he did find a solution, unless he said its solved, otherwise, every one can still post. You don't assume anything here. Also, an old thread could be revived if there are alternative solutions to a problem. Maybe the solution uses a method that is considered old in 2 years time and some one posted the new way to solve the same problem ,or if the posted solution uses a very inefficient technique, like the one posted by aGerman using 2 calls to findstr ( as compared to just one call of gawk) , there is nothing wrong with that.

It will something wrong if one revives an old thread but solves nothing. Again, there is really none of any one's business if i want to post solutions to an thread or not. Only the moderator can tells me so. Period.

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Find pattern with AWK

#9 Post by rfpd » 27 Dec 2010 10:40

First i didn't say i assume anything here, i was just saying that the post was old and the post owner maybe already find a way or he doesn't need this code anymore

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Find pattern with AWK

#10 Post by ghostmachine4 » 27 Dec 2010 18:52

rfpd wrote:First i didn't say i assume anything here, i was just saying that the post was old and the post owner maybe already find a way or he doesn't need this code anymore

dude, there are already a few things you are assuming here.
1) that the OP has already found a way.
2) that the OP doesn't need the code anymore.

Whether he already found a way or not, we don't know since he did not say so himself. Second, how do you know he doesn't need the code anymore? maybe its because nobody posted a gawk solution?

Whatever it is, its not your business if i want to post the solution or not. I am posting in direct response to the OP because I was the one who provided the gawk solution for him in the first place, there is nothing wrong with that. If I am posting nonsense that doesn't solve the problem, then you can rebut , otherwise, stay out of my business.

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Find pattern with AWK

#11 Post by rfpd » 28 Dec 2010 06:07

ghostmachine4 wrote:
rfpd wrote:First i didn't say i assume anything here, i was just saying that the post was old and the post owner maybe already find a way or he doesn't need this code anymore

dude, there are already a few things you are assuming here.
1) that the OP has already found a way.
2) that the OP doesn't need the code anymore.

Whether he already found a way or not, we don't know since he did not say so himself. Second, how do you know he doesn't need the code anymore? maybe its because nobody posted a gawk solution?

Whatever it is, its not your business if i want to post the solution or not. I am posting in direct response to the OP because I was the one who provided the gawk solution for him in the first place, there is nothing wrong with that. If I am posting nonsense that doesn't solve the problem, then you can rebut , otherwise, stay out of my business.

End.

Post Reply