Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk remove all characters before match "/"

Tags:

bash

shell

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.

like image 838
sylar Avatar asked Oct 22 '25 05:10

sylar


2 Answers

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
like image 108
Freeman Avatar answered Oct 24 '25 19:10

Freeman


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
like image 23
RavinderSingh13 Avatar answered Oct 24 '25 20:10

RavinderSingh13