While reading results of a ls instance, I'm trying to display a specific term from a specific line using sed :
Here is what I managed to do until now :
echo $(ls -lAtro | sed -n '3p' | cut -d' ' -f 6 )
This is reading the 6th term of the 3rd line. What I'm trying to do is replace the '3p' by a more simple variable which would permit to do something like this :
linetoread=3
echo $(ls -lAtro | sed -n "$linetoread" | cut -d' ' -f 6 )
The above example doesn't work of course, but you get the idea. What can I do to achieve this ? Thanks in advance.
Try awk
instead-
line=3 ;
ls -lAtro | awk -v var="$line" 'NR==var {print $6}'
This takes the variable line=3
and feeds it into awk
(the awk -v
part).
The NR==var
ensures that you get the line corresponding to your variable.
Then you can change the print $6
part to match whichever column you want. Note that the default delimiter is a space.
Of course, as pointed out in the comments, you shouldn't be parsing the output of ls
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.