Sometimes there are multiple "filename containing spaces" in a DIR, such as PDF name, and you want to search by a file name containing a date or WORD and see the results later.
ls-la
-rw-rw-1 admin administrator 11794593 September 3 2014 Cambridge University Press Programming in Mathematica An Introduction 2013.pdf
At this time, if there are no spaces, you can specify a field number for the post-filename, such as ls-la
.
ls-la |awk'/pdf/{print$9}'
Cambridge
However, if there are a few blanks, I am having a hard time because I cannot get the results I wanted.
Thank you for your advice.
LANG=Cls-laN$1|awk'{
re="^([^]+){7}[^]+"
match ($0,re)
num = RLENGTH+1
print substr ($0,num)
}'
When you want to cut out a file that is the result of ls in awk's script, it's troublesome if the file name contains spaces.
The default delimiter for awk is blank, but the number of consecutive delimiters is interpreted as one, so once you break it down into fields, you cannot restore the number of blanks.
When I wanted to use the results of ls, the only solution I could think of was to delete the fields up to the filename.
In this environment, we have removed the first 50 characters from $0, since the 51st character was the file name.
If you run ls without the N option, the N option suppresses the file because it contains quotation marks before and after the file containing the blank space.
Version of ls used for verification.
ls(GNU coreutils) 8.28
Additional
"You pointed out that ""the starting position of the file name changes"" depending on the situation, so I modified the code."
Supports file names that begin with a blank space.
@cubick, if you can specify the information to display with the arguments of the command, you can use it.
If you want to retrieve information from information or commands that you cannot do, or if you want to extract it from a file that contains all the information you have already obtained and has been texted, can you use index
or match
and substr
?
How to use the match() function in awk
cut strings with match() and substr() functions
How to specify the start position of substr() with index() in awk
How to use substr() to cut strings in awk
File name, folder name like this:
(If there are no 9 fields, another field was displayed, so I checked with the minimum digit position.)
#!/bin/sh
ls-la | awk'
{
match ($0,$9)
if(RSTART>=17){
datext = substr ($0, RSTART)
print datext
}
}
'
If you want to cut out of the file date field, simply shift the number:
#!/bin/sh
ls-la | awk'
{
match ($0, $6)
if(RSTART>=11){
datext = substr ($0, RSTART)
print datext
}
}
'
Use index
instead of match
here:
#!/bin/sh
ls-la | awk'
{
column=index($0,6)
if(column>=11){
datext = substr ($0, column)
print datext
}
}
'
As Cubick pointed out, it would be better to use ls-1a (or ls-1A).
It is surprisingly troublesome to extract a file name that reproduces a blank space from the "ls-lA" output.
I tried the following just in case.
The split() function divides the column and separator into segments, and then outputs the necessary parts.
For ease of viewing, we place "|" at the beginning and end of the output.
$touch "000" "aaa" "bbbb" "cccccccccccc" "dddddddddd"
$ ls-lA
Total 0
-rw-rw-r -- 1 hidezz hidezz 0 June 22 11:21 '000'
-rw-rw-r -- 1 hidezzz hidezzz 0 June 22 11:21 aaa
-rw-rw-r -- 1 hidezzz hidezzz 0 June 22 11:21 'bbbbbb'
-rw-rw-r -- 1 hidezzz hidezzz 0 June 22 11:21 'cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
-rw-rw-r--1 hidezzz hidezzz 0 June 22 11:21 'dddddddd'
$ls-lA | awk'NR>1 {split($0, a, "", sep); printf" | "substr(sep[8], 2);for(i=9;i<=length(a);++i){printfa[i]sep[i]}print"|"}}}'
| 000|
|aaa|
| bbbbbb|
| ccc ccc ccc |
|dddddddddd|
© 2024 OneMinuteCode. All rights reserved.