By default, awk
parses a string using the whitespace character as a separator.
So if this is done:
echo "20151223152832_alexsandro_felix.txt" | awk '{print $9}'
Nothing will be returned, as there is no blank space in the string, and even if it does, it would take 8 spaces to print something at position 9.
Now try changing the string, including a space:
echo "20151223152832 _alexsandro_felix.txt" | awk '{print $1}'
Will be returned: 20151223152832
echo "20151223152832 _alexsandro_felix.txt" | awk '{print $2}'
Will be returned: _alexsandro_felix.txt
The command ls -lh
returns a string in this format:
-rw-rw-r-- 1 cantoni cantoni 0 Jan 12 10:39 20151223152832_alexsandro_felix.txt
So, by making a awk '{print $9}'
the file name is printed. The danger of this approach is if the file name contains spaces. If this happens then awk '{print $9}'
will not return the whole file name.
Explained, one way to resolve this problem without using awk
would be by using the following command:
ls -A 20151*
In the context of the question question would this be:
array=($(ls -A 20151*));
You can change the separator by which awk
does split in a string, see example below:
echo "20151223152832:_alexsandro_felix.txt" | awk -F':' '{print $2}'
Will be returned: _alexsandro_felix.txt
In this case, the separator was the character ':'