sorry for my bad english not my first language. I have a text file with the following content, which is constantly changing in the column "directory":
DIRECTORY FILES SIZE
documents/my_name/directory1/ 2 96M
documents/my_name/directory2/ 4 21M
documents/my_name//directory3/ 1 43M
What I want is to remove the absolute path and leave only the name of the last directory.
Note: The third directory is written correctly, some addresses are written with two backslashes.
The result would be like this:
DIRECTORY FILES SIZE
directory1/ 2 96M
directory2/ 4 21M
directory3/ 1 43M
or this:
DIRECTORY FILES SIZE
directory1 2 96M
directory2 4 21M
directory3 1 43M
Is there a way to do it using awk linux command or some other command?
Remember that the paths can be of different lengths, it is a file that always changes.
check this out :
perl -pe 's:.*/([^/]+)/:\1:;s:/::g' file | column -t
or
perl -F"/" -lane 'print $F[-2], $F[-1] if @F >= 2 or $. == 1' file | column -t
or
perl -lane 'if ($.==1){print}else{($d,$f,$s)=@F;$d=~s/.*\/(.+?)\/$/$1/;print"$d\t$f\t$s"}' file | column -t
output:
DIRECTORY FILES SIZE
directory1 2 96M
directory2 4 21M
directory3 1 43M
You could use match function of awk with regex in it.
awk '
BEGIN{OFS="\t"}
FNR==1{
$1=$1
print
next
}
match($0,/\/[^/]+\/[[:space:]]+.*$/){
print substr($0,RSTART+1,RLENGTH)
}
' Input_file | column -t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With