sed to awk (regex pattern) how?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to awk (regex pattern) how?
# 1  
Old 11-05-2009
sed to awk (regex pattern) how?

Hello,

I am trying to covert a for statement into a single awk script and I've got everything but one part.

I also need to execute an external script when "not found", how can I do that ?

Code:
for TXT in `find debugme -name "*.txt"` ;do
FPATH=`echo $TXT | sed 's/\(.*\)\/\(.*\)/\1/'`

how to add it to the following awk code?

Code:
find debugme -name "*.txt" | awk 'BEGIN{
    while( (getline line < "debug.files" ) > 0 ) {
        a[++d]=line
    }
    close("debug.files")
}
{
    filename=$0
    #FPATH=?????
    while( (getline line < filename ) > 0 ){
        f=0
        m=split(line,t," ")
        if ( t[1] ~ "(rar|r[0-9]+)" ){
            for(i=1;i<=d;i++){
                if( a[i] ~ t[1] ){
                    print found
                    f=1
                }
            }
            if (f==0){ print not found }
        }
    }
    close(filename)
}'

# 2  
Old 11-05-2009
sed is just doing substitution. the equivalent of substitution in awk is gsub() or sub(). read the doc on how to use it. you will also find in the doc how to execute external commands in awk. its system() or the use of getline. If you are wondering where the doc is , see here
# 3  
Old 11-05-2009
Quote:
Originally Posted by ghostdog74
sed is just doing substitution. the equivalent of substitution in awk is gsub() or sub(). read the doc on how to use it. you will also find in the doc how to execute external commands in awk. its system() or the use of getline. If you are wondering where the doc is , see here
Thanks for pointing them out, I've looked into the docs and while the system() part is easy... I still don't know to get the same results with gsub() or sub()

I'm not even sure if there's a need of using gsub() / sub()

All I need is the fullpath that find gives without the name.txt part..

So

/BLA/ABC/123.txt = /BLA/ABC

/BLA/123.txt = /BLA

and so on...
# 4  
Old 11-05-2009
This perhaps?
Code:
    #FPATH=?????
    fpath=$0
    sub(/\/[^\/]*$/,"",fpath)

# 5  
Old 11-06-2009
Thanks alot buddy, now I'm having another problem...

Basically befor the awk code there are some other variables in my shell script...

Example:

TEST="TESTING"

find debugme -name "*.txt" | awk 'BEGIN{.................
.........

system("echo $TEST") doesn't work...

How can I get the variables to work within the awk script? I need those informations to execute another script.. like system("exscript $TEST t[1]"....)
# 6  
Old 11-06-2009
what you need is documented in the doc. See here. read the docs !
# 7  
Old 11-06-2009
Quote:
Originally Posted by ghostdog74
what you need is documented in the doc. See here. read the docs !
I've just figured it out, thanks alot guys!

Code:
system("echo ""'"$VAR1"'"" "fpath" "t[1]"")

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed/awk to delete a regex between range of lines

Hi Guys I am looking for a solution to one problem to remove parentheses in a range of lines. Input file module bist_logic_inst(a, ab , dhd, dhdh , djdj, hdh, djjd, jdj, dhd, dhp, dk ); input a; input ab; input dhd; input djdj; input dhd; output hdh; output djjd; output jdj;... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

REGEX help required and some sed/awk help as well

Hi guys, I am coding a bash script that makes use of php scripts to pull URL's from a website. These url links will have numbers in them like 0.2.3 I want to make a regex that will yield me such numbers if I use a command like preg_grep. Question1: I need a regex that will tell my preg_grep... (2 Replies)
Discussion started by: mojoman
2 Replies

3. UNIX for Advanced & Expert Users

sed REGEX to print multiple occurrences of a pattern from a line

I have a line that I need to parse through and extract a pattern that occurs multiple times in it. Example line: getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed,... (4 Replies)
Discussion started by: Vidhyaprakash
4 Replies

4. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

5. Shell Programming and Scripting

Regex in sed to find specific pattern and assign to variable

(5 Replies)
Discussion started by: radioactive9
5 Replies

6. Shell Programming and Scripting

To print lines between 2 timestamps using awk|sed and regex

Hi, I am using the following code to fetch lines that are generated in last 1 hr . Hence, I am using date function to calculate -last 1 hr & the current hr and then somehow use awk (or sed-if someone could guide me better) with some regex pattern. dt_1=`date +%h" "%d", "%Y\ %l -d "1 hour... (10 Replies)
Discussion started by: sarah-alikhan31
10 Replies

7. Shell Programming and Scripting

Question on awk/sed/regex

I plan to do something like this in awk that's embedded in a shell script. I have extracted a text file using awk and the output is before example: <USDOLLARS|xxx> I want to use get rid of the starting "<" and anything after the pipe "|" after example: USDOLLARS How do I do that?? I... (8 Replies)
Discussion started by: deadyetagain
8 Replies

8. Shell Programming and Scripting

SED: Pattern repitition regex matching

Fairly straightforward, but I'm having an awful time getting what I thought was a simple regex to work. I'll give the command I was playing with, and I'm aware why this one doesn't work (the 1,3 is off the A-Z, not the whole expression), I just don't know what the fix is: Actual Output(s): $... (5 Replies)
Discussion started by: Vryali
5 Replies

9. Shell Programming and Scripting

Selecting a part of the text (regex pattern, awk, sed)

Hello, let's start by giving you guys a few examples of the text: "READ /TEXT123/ABC123" "READ /TEXT123/ABC123/" "READ TEXT123/ABC123" "READ TEXT123/ABC123/" "READ TEXT123/TEXT456/ABC123" "READ /TEXT123/TEXT456/ABC123" "READ /TEXT123/TEXT456/ABC123/" TEXT and ABC can be and I... (5 Replies)
Discussion started by: TehOne
5 Replies

10. UNIX for Dummies Questions & Answers

blank space in regex pattern using sed

why does sed 's/.* //' show the last word in a line and sed 's/ .*//' show the first word in a line? How is that blank space before or after the ".*" being interpreted in the regex? i would think the first example would delete the first word and the next example would delete the second... (1 Reply)
Discussion started by: glev2005
1 Replies
Login or Register to Ask a Question