Skip to content
Advertisement

Shell Script to extract only file name

I have file format like this (publishfile.txt)

drwxrwx---+  h655201 supergroup          0  2019-04-24  09:16  /data/xyz/invisible/se/raw_data/OMEGA
drwxrwx---+  h655201 supergroup          0  2019-04-24  09:16  /data/xyz/invisible/se/raw_data/sample
drwxrwx---+  h655201 supergroup          0  2019-04-24  09:16  /data/xyz/invisible/se/raw_data/sample(1)

I just want to extract the name OMEGA****, sample, sample(1) How can I do that I have used basename in my code but it doesn’t work in for loop. Here is my sample code

for line in $(cat $BASE_PATH/publishfile.txt)
do 
      FILE_PATH=$(echo "line"| awk '{print  $NF}' )
done
FILE_NAME=($basename  $FILEPATH)

But this code also doesn’t wor when used outside for loop

Advertisement

Answer

awk -F / '{ print $NF }' "$BASE_PATH"/publishfile.txt

This simply says that the delimiter is a slash and we want the last field from each line.

You should basically never run Awk on each input line in a shell while read loop (let alone a for loop); Awk itself does this by default, much faster and better than the shell.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement