|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
for example, i have a string like
/root/dir1/dir2/file I want to replace the leading dirs to three tab, and the result is : [tab][tab][tab]file I do this because I want to print a dir tree in a tree format in my screen. How to achive this? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Bo Yang wrote: > for example, i have a string like > /root/dir1/dir2/file > > I want to replace the leading dirs to three > tab, and the result is : > > [tab][tab][tab]file > > I do this because I want to print a dir tree > in a tree format in my screen. > How to achive this? Try this. Put the three tabs in between the two % signs for the substitution part. rashlro1@ichn21>echo /one/two/three/file | sed 's%.*/% %' file |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Bo Yang <struggle@mail.nankai.edu.cn> wrote:
> for example, i have a string like > /root/dir1/dir2/file > > I want to replace the leading dirs to three > tab, and the result is : > > [tab][tab][tab]file echo /root/dir1/dir2/file | sed 's%^/%% s%[^/]*/%YYY%g' Something like that. The YYY has to be replaced by a tab, or whatever should be inserted instead of the three leading dirs. Keep in mind that filenames themselves may contain tabs and even newlines. This could lead to unexpected results with this simple solution. regards, lasse |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Bo Yang wrote: > for example, i have a string like > /root/dir1/dir2/file > > I want to replace the leading dirs to three > tab, and the result is : > > [tab][tab][tab]file > > I do this because I want to print a dir tree > in a tree format in my screen. > How to achive this? Oh, I get you now, you want a tab for each level of the directory. This is as near as damn it. Put the tab between the last two %s. rashlro1@ichn21>echo /one/two/three/four/file | sed 's%[^/]*/% %g' file |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Hi,
> for example, i have a string like > /root/dir1/dir2/file > > I want to replace the leading dirs to three > tab, and the result is : > > [tab][tab][tab]file > > I do this because I want to print a dir tree > in a tree format in my screen. > How to achive this? A solution with awk: $ FILE="/root/dir1/dir2/file" $ echo "$FILE" | awk -F'/' '{for(i=2; i<NF; i++) { printf "\t"}} {print $NF}' Cheers, Loic. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
RolandRB :
> Bo Yang wrote: >> for example, i have a string like >> /root/dir1/dir2/file >> >> I want to replace the leading dirs to three >> tab, and the result is : >> >> [tab][tab][tab]file >> >> I do this because I want to print a dir tree >> in a tree format in my screen. >> How to achive this? > > Oh, I get you now, you want a tab for each level of the directory. This > is as near as damn it. Put the tab between the last two %s. > > rashlro1@ichn21>echo /one/two/three/four/file | sed 's%[^/]*/% %g' > file > Thank you, it works, thanks! |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
loic-dev@gmx.net :
> Hi, > >> for example, i have a string like >> /root/dir1/dir2/file >> >> I want to replace the leading dirs to three >> tab, and the result is : >> >> [tab][tab][tab]file >> >> I do this because I want to print a dir tree >> in a tree format in my screen. >> How to achive this? > > A solution with awk: > $ FILE="/root/dir1/dir2/file" > $ echo "$FILE" | awk -F'/' '{for(i=2; i<NF; i++) { printf "\t"}} > {print $NF}' > Thanks! |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Bo Yang wrote:
> for example, i have a string like > /root/dir1/dir2/file > > I want to replace the leading dirs to three > tab, and the result is : > > [tab][tab][tab]file > > I do this because I want to print a dir tree > in a tree format in my screen. > How to achive this? If you wouldn't mind for spaces, you can use sed 's#[^/]*/# #g' Otherwise you can replace the two space characters by a TAB character. -- Michael Tosch @ hp : com |
|
![]() |
| Outils de la discussion | |
|
|