How to parse filename and one level up directory name?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to parse filename and one level up directory name?
# 1  
Old 10-08-2010
How to parse filename and one level up directory name?

Hello Experts,

I need little help with parsing.
I want to parse filename and one level up directory name.
sample $1 will consists of
/home/username/ABC1/rstfiles4.log
/home/username/ABC4/rstfiles2.log
/home/username/EDC7/rstfiles23.log
/home/username/EDC6/rstfiles55.log
/home/username/rstfiles15.log

So in this i want to parse like
ABC,rst*.log
EDC,rst*.log
,rst.log

Please help me to get this output.

Thanks in advance!!
# 2  
Old 10-08-2010
Code:
$ cat test.in
/home/username/ABC1/rstfiles4.log
/home/username/ABC4/rstfiles2.log
/home/username/EDC7/rstfiles23.log
/home/username/EDC6/rstfiles55.log
/home/username/rstfiles15.log

$ sed 's:^/home/username/::' test.in |
sed 's:^\(...\).*/:\1,:' |
sed 's:\,\(...\)[^.]*:,\1*:' |
sed 's:^\([^,][^,][^,]\)[^,]*\.:,\1.:' | uniq
ABC,rst*.log
EDC,rst*.log
,rst.log

# 3  
Old 10-08-2010
Thanks Chubler_XL..
my path is not fixed. my script takes path as input parameter...
and that path contains the common characters like
any path/ABC*/rst*.log
any path/EDC*/rst*.log
any path/rst*.log

Becuase after getting this,ABC* or EDC* i will need to perform action on that like ABC.* == ABC.* then direct it ot csv file

If you can give some idea..will be greatful
# 4  
Old 10-08-2010
This should work for you then, path is in variable mypath (without trailing slash):

Code:
sed "s:^${mypath}/::" test.in |
sed 's:^\(...\).*/:\1,:' |
sed 's:\,\(...\)[^.]*:,\1*:' |
sed 's:^\([^,][^,][^,]\)[^,]*\.:,\1.:' | uniq

# 5  
Old 10-08-2010
I am getting the below output:

exp,201*.
exp,201*
exp,201*.
exp,or *.
exp,201*.
exp,201*
exp,201*.
exp,201*

that is first 3 character of path and then last 3 character of $1 appended by *.
# 6  
Old 10-08-2010
assuming you put everything into a file, exactly as you describe.


Code:
for i in `cat filename`
do
   BASE=`basename $i`
   DIR=`dirname $i | xargs basename`
   echo $DIR "," $BASE
done

# 7  
Old 10-10-2010
Quote:
Originally Posted by Shirisha
I am getting the below output:

exp,201*.
exp,201*
exp,201*.
exp,or *.
exp,201*.
exp,201*
exp,201*.
exp,201*

that is first 3 character of path and then last 3 character of $1 appended by *.
Isn't that what you asked for in your original post? What exactly do you want?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse Directory path - awk

Hi All, Need some help in parsing a directory listing .. output into 2 files Input file level1,/level2/level3/level4/ora001,10,IBB23 level1,/level2/level3/level4/ora001/blu1,,IBB23 level1,/level2/level3/level4/ora001/clu1,,IBB23 level1,/level2/level3/level4/ora002,,IBB24... (10 Replies)
Discussion started by: greycells
10 Replies

2. UNIX for Beginners Questions & Answers

Bash script - Remove the 3 top level of a full path filename

Hello. Source file are in : /a/b/c/d/e/f/g/some_file Destination is : /d/e where sub-directories "f" and "g" may missing or not. After copying I want /a/b/c/d/e/f/g/file1 in /d/e/f/g/file1 On source /a is top-level directory On destination /d is top-level directory I would like... (2 Replies)
Discussion started by: jcdole
2 Replies

3. UNIX for Beginners Questions & Answers

Move all files one directory level up

I want to move all the files in a given directory up one level. For example: Dir1 Subdir1 I want to move all the files in Subdir1 up to Dir1 (then I want to ultimately delete Subdir1) Thanks, Ted (10 Replies)
Discussion started by: ftrobaugh
10 Replies

4. Web Development

.htaccess allow at directory level

Hi Guys I have a web a site at the following directory /var/www/ the website is containing a lot of sub directories. /var/www/mywebsite/folder1 /var/www/mywebsite/folder2 /var/www/mywebsite/folder3 I would like to block all the website expect one user ex: user1 password1 who can see... (3 Replies)
Discussion started by: molwiko
3 Replies

5. Shell Programming and Scripting

How to get top level parent directory

Hi All, I have a directory like this: /u01/app/oracle/11gSE1/11gR203 How do i get the top level directory /u01 from this? Tried dirname and basename but dint help. I can this using echo $ORACLE_HOME | awk -F"/" '{print "/"$2}'. But I am trying to find out if there is a better way of doing it... (4 Replies)
Discussion started by: nilayasundar
4 Replies

6. Shell Programming and Scripting

Parse files in directory and compare with another file

I have two files File 1 in reading directory is of following format Read 1 A T Read 3 T C Read 5 G T Read 7 A G Read 10 A G Read 12 C G File 2 in directory contains Read 5 A G Read 6 T C Read 7 G A Read 8 G A Read 20 A T File2 contains (1 Reply)
Discussion started by: empyrean
1 Replies

7. UNIX for Dummies Questions & Answers

awk to parse a directory name?

Hi, I have a directory file name: /auto/space/user/jen/CED/CED_01MZ/visit1/DCE_2eco/016/echo1 I would like to just get the following outputs into variables such that: variable1 = /auto/space/user/jen/CED/CED_01MZ/visit1/ and variable2 = DCE_2eco/016/echo1 I've tried it with... (2 Replies)
Discussion started by: nixjennings
2 Replies

8. Shell Programming and Scripting

Parse and replace string in filename

Hi, I've a filename of this format: "XXX_XXX_TXT.TAR.AS". Need to change the name into this format: "XXX_XXX.TAR.AS". This file resides in a directory. I'm ok with using the find command to search and display it. Essentially I just need to replace the string "_TXT.TAR.AS" to ".TAR.AS". Is awk... (17 Replies)
Discussion started by: chengwei
17 Replies

9. Shell Programming and Scripting

How to exclude top level directory with find?

I'm using bash on cygwin/windows. I'm trying to use find and exclude the directory /cygdrive/c/System\ Volume\ Information. When I try to use the command below I get the error "rm: cannot remove `/cygdrive/c/System Volume Information': Is a directory. Can someone tell me what I am doing... (3 Replies)
Discussion started by: siegfried
3 Replies

10. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies
Login or Register to Ask a Question