Page 1 of 1
Find pattern with AWK
Posted: 04 Dec 2010 00:55
by darioit
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
Re: Find pattern with AWK
Posted: 07 Dec 2010 04:21
by darioit
It's well accepted also Find and Findstr
Re: Find pattern with AWK
Posted: 11 Dec 2010 08:05
by aGerman
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
Re: Find pattern with AWK
Posted: 26 Dec 2010 18:53
by ghostmachine4
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.
Re: Find pattern with AWK
Posted: 26 Dec 2010 19:08
by rfpd
this is old post don't repost here please.
Re: Find pattern with AWK
Posted: 26 Dec 2010 19:50
by ghostmachine4
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.
Re: Find pattern with AWK
Posted: 27 Dec 2010 05:48
by rfpd
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.
Re: Find pattern with AWK
Posted: 27 Dec 2010 07:22
by ghostmachine4
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.
Re: Find pattern with AWK
Posted: 27 Dec 2010 10:40
by rfpd
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
Re: Find pattern with AWK
Posted: 27 Dec 2010 18:52
by ghostmachine4
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.
Re: Find pattern with AWK
Posted: 28 Dec 2010 06:07
by rfpd
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.