-1

I have tried

awk '{print FILENAME}'

And the result was full path of the file. I want to get only the file name, example: from "test/testing.test.txt" I just want to get "testing" without ".test.txt".

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Bams
  • 31
  • 4
  • does it have to be with awk... how about with basename – z atef Mar 15 '16 at 03:22
  • yes, cause i have these line of the records in a file, and i want to insert the filename into those line records. and awk was the fastest way to do it, i think, and im using it now. basename what? could you more spesific? or type the command for that? – Bams Mar 15 '16 at 03:25
  • There's a command `basename "$file" .test.txt` that would remove the directory prefix and the `.test.txt` suffix from the file name, leaving just `testing` for the sample name. It takes one file name at a time. What is the basis for removing `.text.txt`? What should be done with `testing/.test.txt`? With `testing/abc.def`? With `testing/test.txt.extra`? With `testing.text.txt.extra`? – Jonathan Leffler Mar 15 '16 at 04:07
  • I amy be missing something, but `echo "test/testing.test.txt" | awk '{n=split($0, ar, /\//); split(ar[n], ar2, /\./); print ar2[1]}'`? – dawg Mar 15 '16 at 04:13
  • @JonathanLeffler Do you mean that the command was ask ' basename FILENAME' ? inside of the testing folder was contains many of file with format abd.def.txt, as my understanding, your explanation about using basename function in awk can get the name file like i wanted, cmmiw? – Bams Mar 15 '16 at 04:33
  • @dawg Your code was running as i expected, but im talking for the file name inside the folder, not path inside the file. using $0 its look like refer for path inside the file. but actually i mean is the file name inside the folder. – Bams Mar 15 '16 at 04:45
  • @dawg I have folder test, the full path was D:\test\testing.test.txt inside test folder many files with format abc.def.txt, like testing.test.txt, testingA.testA.txt, and so on. my main goal was to get only the file name such as abc,testing,testingA . – Bams Mar 15 '16 at 04:51
  • There is a command called `basename` just like there is a command called `awk`. There isn't a `basename` function in `awk`, AFAIK. – Jonathan Leffler Mar 15 '16 at 05:01
  • @dawg Im using this script :awk 'function basename(file) { sub(".*/", "", file) return file } {print basename(FILENAME)}' but the result was full path – Bams Mar 15 '16 at 08:53
  • AWK 'END{ var=FILENAME;n=split (var,a,/\//); print a[n]}' , but the result still the same, D:\Latihan\test1.txt and D:\Latihan\test2.txt. all i wanted was test1 and test2. – Bams Mar 15 '16 at 09:26

2 Answers2

1

Use -F to delimit by the period and print the first string before that delimiter:

awk -F'.' '{ print $1 }'

Alternatively,

ls -l | awk '{ print $9 }' | awk -F"." '{ print $1 }'

will run through the whole folder

(there's a fancier way to do it, but that's easy).

C K
  • 11
  • 1
0

Use the sub and/or split functions to extract the part of FILENAME you want.

peak
  • 105,803
  • 17
  • 152
  • 177
  • how the right format? im new in awk, could you give me an example? – Bams Mar 15 '16 at 05:11
  • im using this script: awk 'function basename(file) { sub(".*/", "", file) return file } {print basename(FILENAME)}' but still return a full path, do you have any solution? – Bams Mar 15 '16 at 08:54
  • You're on the right track. Look at @dawg's comment. – peak Mar 15 '16 at 12:50