How do I apply awk to the path within the shell script?

Asked 1 years ago, Updated 1 years ago, 95 views

In the shell script, I wanted to get the directory at the bottom of the path specified in the argument, so I wrote:

■test.sh

#!/bin/sh

myfol=`awk-F"/"{print$NF}${1}`
mylog=2016_${myfol}.txt
touch${mylog}

■ Runtime

test.sh/var/log/aaa_sever

■Error Message

awk:warning:command line argument /var/log/aaa_server is directory:skipped

I thought awk might not work, so I changed it to sed, but I still get the same error.
How do I create a file called 2016_aaa_server.txt?

linux bash shellscript awk

2022-09-30 17:30

1 Answers

If you are not interested in awk, the command basename is exactly the last part of the path.Incidentally, another pair of commands is the dirname command, which takes out anything other than the last part.

myfol=`basename"${1}"`

If you're using awk, try to make it possible for awk to read the text from standard input, and it's easy to pipe in the text displayed in echo. The same goes for sed.

myfol=`echo-n"${1}"|awk-F"/'{print$NF}'`


2022-09-30 17:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.