strike last part from list of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting strike last part from list of files
# 1  
Old 07-18-2010
strike last part from list of files

Hi,

I have list of files as following:

/home/abc/x/23344.php
/home/axx/zz/ddddd/abc/7asda/2434.php
/home/zzz/7x/y/114.php
/home/assssc/x/yasyday/23664.php


( last part in each line is <somenumber.php>


I need to somehow get this from the above:

/home/abc/x/
/home/axx/zz/ddddd/abc/7asda/
/home/zzz/7x/y/
/home/assssc/x/yasyday/

i.e. get rid of <somenumber.php>



Please advise.

Thanks
# 2  
Old 07-18-2010
you can use 'dirname' on a fully qualified path to get the directory name.

'basename' will do the opposite


eg.
Code:
# dirname /path/to/file
/path/to

Code:
# basename /path/to/file
file

# 3  
Old 07-18-2010
dirname command does the job:

Code:
$ dirname /home/zzz/7x/y/114.php
/home/zzz/7x/y

Code:
FILE="/home/zzz/7x/y/114.php"
DIR=$(echo $FILE|sed 's#/*.php$##')

# 4  
Old 07-18-2010
thanks a ton
# 5  
Old 07-18-2010
Code:
FILE="/home/zzz/7x/y/114.php"
echo ${FILE%/[0-9]*php}/
/home/zzz/7x/y/

# 6  
Old 07-19-2010
Code:
$ f="/home/zzz/7x/y/114.php"
$ echo ${f%/*}/
/home/zzz/7x/y/

# 7  
Old 07-19-2010
using while

Hi,

Can also be done as follows

cat fil1 |while read in; do var=`dirname $in`;echo $var; done >> fil2 where fil1 is a file comprising of following

Code:
/home/abc/x/23344.php
/home/axx/zz/ddddd/abc/7asda/2434.php
/home/zzz/7x/y/114.php
/home/assssc/x/yasyday/23664.php

and fil2 is your output file

Last edited by maverick_here; 07-19-2010 at 03:41 AM.. Reason: code section
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display the last part of a number list

Hi , i have a file wich have 50+ of numbers like : 0.014544106 0.005464263 0.014526045 0.005484374 0.014539412 0.005467600 0.014558349 0.005452185 i would like to display the list from the 6th bit to the end for example 0.005452185 (should become) 2185. I've tried with ... (4 Replies)
Discussion started by: Board27
4 Replies

2. Shell Programming and Scripting

Unzip all the files with subdirectories present and append a part of string from the main .zip files

Hi frnds, My requirement is I have a zip file with name say eg: test_ABC_UH_ccde2a_awdeaea_20150422.zip within that there are subdirectories on each directory we again have .zip files and in that we have files like mama20150422.gz and so on. Iam in need of a bash script so that it unzips... (0 Replies)
Discussion started by: Ravi Kishore
0 Replies

3. Shell Programming and Scripting

A simpler way to do this (save a list of files based on part of their name)

Hello, I have a script that checks every file with a specific extension in a specific directory. The file names contain some numerical output and I am recording the file names with the best n outcomes. The script finds all files in the directory with the extension .out.txt and uses awk to... (12 Replies)
Discussion started by: LMHmedchem
12 Replies

4. Shell Programming and Scripting

Validate if string part of a list

I have this requirement of validating input from user to be one of a list of strings. I validate it as below. case $1 in Jan) ;; Feb) ;; . . . Dec) ;; *) echo "Invalid input. Should be one of the following." echo "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov... (8 Replies)
Discussion started by: krishmaths
8 Replies

5. SuSE

Lord of the Rings and Counter Strike on OpenSUSE 11.1

is it possible to get Lord of the Rings Battle for Middle Earth 2 and Counter Strike working on OpenSUSE 11.1? i'm dual booting SUSE with XP but if i can get those games working then there is no need for Windows. Thanks (1 Reply)
Discussion started by: brendanb
1 Replies

6. Shell Programming and Scripting

read a part of filename from the list in the script

how can i read a part of filename from the list in the script? all file in directory...will start with "CDBACKUPFILE" then the name is stored in list.txt such as JOHN,MARRY,PETER. After this, is seq number. CDBACKUPFILEJOHN00001 CDBACKUPFILEMARRY00004 CDBACKUPFILEPETER00003 I will use:... (3 Replies)
Discussion started by: happyv
3 Replies

7. Shell Programming and Scripting

script on strike?

dear sages, please tell me what's wrong with the following script: #!/bin/sh #usage yd2ymd 1998213 # if there is no command line argument, assume one is being # piped in and read it if then read dt else dt=$1 fi #... (6 Replies)
Discussion started by: serguey
6 Replies
Login or Register to Ask a Question