2

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

Shivam Sharma
  • 517
  • 1
  • 5
  • 19

3 Answers3

4
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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    this doesn't work if the file contains something like: `drwxrwx---+ h655201 supergroup 0 2019-04-24 09:16 sample` (i.e. a filepath without slash `/` char). You are correct with all other remarks! `awk` itself does the loop, etc. Using `awk` or `sed` is indeed a better solution! – azbarcea Feb 02 '20 at 20:06
  • Not directly, though it would not be very hard to adapt (try -F `[/ \t]'`) – tripleee Feb 02 '20 at 20:08
  • How can I use in a way that file name is separated by comma ? Like sample,sample1 – Shivam Sharma Feb 03 '20 at 10:25
  • 1
    See the section _Parameter Expansion_ in the [POSIX shell specification](https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html). If this doesn't answer your case, please ask a **new** question here, where you specify exactly the format of your input, and what you want to achieve. – user1934428 Feb 03 '20 at 11:00
  • If you are asking how to provide that sort o| output, the Awk script can easily be changed to use `printf` instead of `print`. If you are asking about input, I absolutely agree with the previous comment. – tripleee Feb 03 '20 at 11:11
1

In your code above you have a typo. Your code reads:

    FILE_NAME=($basename  $FILEPATH)

but it should read

    FILE_NAME=$(basename  $FILEPATH)

That should work fine in or outside of a loop

Rob Sweet
  • 184
  • 8
0

Try this:

cat $BASE_PATH/publishfile.txt | awk '{print $7}' | sed 's/.*\///'

the output will be:

OMEGA
sample
sample(1)

UPDATE: I guess cat x.txt | sed 's/.*\///' will still work, if all your files, folders contain at least 1 slash (/).

For the commands used, the manuals are: cat, awk, sed

azbarcea
  • 3,323
  • 1
  • 20
  • 25