# getting basename inside awk script

**URL:** <https://community.unix.com/t/getting-basename-inside-awk-script/243305>\
**Category:** Shell Programming and Scripting\
**Created:** [June 25, 2009, 7:54am UTC](https://community.unix.com/t/getting-basename-inside-awk-script/243305 "2009-06-25T07:54:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anteus](https://community.unix.com/letter_avatar/anteus/32/6_449c533f586939b6e5f246632a46421f.png) [@Anteus](https://community.unix.com/u/Anteus)\
**Post date:** [June 25, 2009, 7:54am UTC](https://community.unix.com/t/getting-basename-inside-awk-script/243305/1 "2009-06-25T07:54:18Z")

</div>

hi if we have to use basename how can we do this in awk?  
did the below but is not working..

```nohighlight
 
psg -t "?"| awk '{
                        command=($5 ~ /^[A-Z]/)? $9:$8
                       # cmd_name=`basename $command` (gives error)
                        system("basename $command >> cmd_name") (isnot working )
                        }'

```

thanks

---

<div class="post-metadata">

**Author:** ![clx](https://community.unix.com/letter_avatar/clx/32/6_449c533f586939b6e5f246632a46421f.png) [@clx](https://community.unix.com/u/clx)\
**Post date:** [June 25, 2009, 7:57am UTC](https://community.unix.com/t/getting-basename-inside-awk-script/243305/2 "2009-06-25T07:57:57Z")

</div>

why not using as the last field of awk?

---

<div class="post-metadata">

**Author:** ![panyam](https://community.unix.com/letter_avatar/panyam/32/6_449c533f586939b6e5f246632a46421f.png) [@panyam](https://community.unix.com/u/panyam)\
**Post date:** [June 25, 2009, 8:20am UTC](https://community.unix.com/t/getting-basename-inside-awk-script/243305/3 "2009-06-25T08:20:31Z")

</div>

Something like this ?..

```nohighlight
 
psg -t "?"| awk '{
                        command=($5 ~ /^[A-Z]/)? $9:$8
                        exe="basename " cmd_name " >>" cmd_name
                        system(exe)                         
                       }'

```

---

<div class="post-metadata">

**Author:** ![Anteus](https://community.unix.com/letter_avatar/anteus/32/6_449c533f586939b6e5f246632a46421f.png) [@Anteus](https://community.unix.com/u/Anteus)\
**Post date:** [June 26, 2009, 1:02am UTC](https://community.unix.com/t/getting-basename-inside-awk-script/243305/4 "2009-06-26T01:02:49Z")

</div>

> [@panyam](#):
>
> Something like this ?..
> 
> ```plaintext
>  
> psg -t "?"| awk '{
> command=($5 ~ /^[A-Z]/)? $9:$8
> exe="basename " cmd_name " >>" cmd_name
> system(exe)                         
> }'
> 
> ```

```plaintext
 
psg -t "?"| awk '{
             command=($5 ~ /^[A-Z]/)? $9:$8
             exe="basename " command " >> command"
             system(exe)
             printf(" second name %s \n",command)
             }'

```

its not printing the basename!!

---

<div class="post-metadata">

**Author:** ![panyam](https://community.unix.com/letter_avatar/panyam/32/6_449c533f586939b6e5f246632a46421f.png) [@panyam](https://community.unix.com/u/panyam)\
**Post date:** [June 26, 2009, 3:05am UTC](https://community.unix.com/t/getting-basename-inside-awk-script/243305/5 "2009-06-26T03:05:09Z")

</div>

Check in the current directory( where you executed the script) , a file with name "command" . The content of the file will be basename's output.

How ever if you want the system output to be seen on screen then make the following change in your script:

```nohighlight
exe="basename " command 
          system(exe)

```

---

<div class="post-metadata">

**Author:** ![Anteus](https://community.unix.com/letter_avatar/anteus/32/6_449c533f586939b6e5f246632a46421f.png) [@Anteus](https://community.unix.com/u/Anteus)\
**Post date:** [June 26, 2009, 7:18am UTC](https://community.unix.com/t/getting-basename-inside-awk-script/243305/6 "2009-06-26T07:18:17Z")

</div>

> [@panyam](#):
>
> Check in the current directory( where you executed the script) , a file with name "command" . The content of the file will be basename's output.
> 
> How ever if you want the system output to be seen on screen then make the following change in your script:
> 
> ```plaintext
> exe="basename " command 
> system(exe)
> 
> ```

I want to use the command variable like this than what i have to do??

```plaintext
 
psg -t "?"| awk '{
             command=($5 ~ /^[A-Z]/)? $9:$8
             exe="basename " command " >> command"
             system(exe)
             if (command == "tec")
             {
             printf(" second name %s \n",command)
             }
             }'

```

---

<div class="post-metadata">

**Author:** ![panyam](https://community.unix.com/letter_avatar/panyam/32/6_449c533f586939b6e5f246632a46421f.png) [@panyam](https://community.unix.com/u/panyam)\
**Post date:** [June 26, 2009, 7:27am UTC](https://community.unix.com/t/getting-basename-inside-awk-script/243305/7 "2009-06-26T07:27:03Z")

</div>

You can use ..the same working fine for me.

print "command" value and check wat ur getting ?..

I did n't understand the logic behind your script.

If you post input and output your expecting , then i hope you can get

more responces.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [June 26, 2009, 7:27am UTC](https://community.unix.com/t/getting-basename-inside-awk-script/243305/8 "2009-06-26T07:27:10Z")

</div>

once again - why do you need to invoke an external command when you can do it all within awk natively?

---

<div class="post-metadata">

**Author:** ![Anteus](https://community.unix.com/letter_avatar/anteus/32/6_449c533f586939b6e5f246632a46421f.png) [@Anteus](https://community.unix.com/u/Anteus)\
**Post date:** [June 26, 2009, 7:49am UTC](https://community.unix.com/t/getting-basename-inside-awk-script/243305/9 "2009-06-26T07:49:31Z")

</div>

> [@panyam](#):
>
> You can use ..the same working fine for me.
> 
> print "command" value and check wat ur getting ?..
> 
> I did n't understand the logic behind your script.
> 
> If you post input and output your expecting , then i hope you can get
> 
> more responces.

```plaintext
 
psg -t "?"| awk '{
             command=($5 ~ /^[A-Z]/)? $9:$8
             exe="basename " command " >> cmd_Name"
             system(exe)
              if (cmd_Name == "tec")
              {
                kill=1
              }
              if (kill ==1)
              {
              op=kill -9 " pid ";
              system(op)
              }
             }'

```

I want get the name of the process and compare to check if it is "tec" process or not if it is ,then i would kill it.

---------- Post updated at 05:19 PM ---------- Previous update was at 05:13 PM ----------

> [@vgersh99](#):
>
> once again - why do you need to invoke an external command when you can do it all within awk natively?

how can we do it?  
by doing natively do you mean to say using reg expression.?

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [June 26, 2009, 7:53am UTC](https://community.unix.com/t/getting-basename-inside-awk-script/243305/10 "2009-06-26T07:53:15Z")

</div>

one way......

```nohighlight
myPathname="/a/b/c/d/myExec"
n=split(myPathname, a, "/")
print a[n]

```

---

<div class="post-metadata">

**Author:** ![Anteus](https://community.unix.com/letter_avatar/anteus/32/6_449c533f586939b6e5f246632a46421f.png) [@Anteus](https://community.unix.com/u/Anteus)\
**Post date:** [July 2, 2009, 5:44am UTC](https://community.unix.com/t/getting-basename-inside-awk-script/243305/11 "2009-07-02T05:44:48Z")

</div>

one more way..

```nohighlight
 
              psg -t "?"| awk '{
              cname=($5 ~ /^[A-Z]/)? $9:$8
              len=length(cname)
              regex="/"
              for(i=1;i<=len;i++)
              {
                  where = index(cname,regex)
                  if(where)
                  {
                       cname= substr(cname,where+1)
                  }
              }
              printf("%s \n",cname)
          }'

```
