I'm trying the following based on code samples I found:
echo "file.txt" | awk '{split($0,a,"."); print a[0]}'
But it's returning a blank output.
Any hint?
You code is correct however the array a
starts from 1 with the split
function:
$ echo "file.txt" | awk '{split($0,a,"."); print a[0]}'
$ echo "file.txt" | awk '{split($0,a,"."); print a[1]}'
file
$ echo "file.txt" | awk '{split($0,a,"."); print a[2]}'
txt
From the gawk
manual:
split(string, array [, fieldsep [, seps ] ])
Divide string into pieces separated by fieldsep and store the pieces in array and the separator strings in the seps array. The first piece is stored in array[1], the second piece in array[2], and so forth.
try
echo "file.txt" | awk '{split($0,a,"."); print a[1]}'
awk array index is 1-based.