Visit The New, Modern Unix Linux Community


Extract filename from a given string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract filename from a given string
# 1  
Extract filename from a given string

I want to extract the filename from a string.

This is how I have the rawdata ina file


/home/sid/ftp/testing/abc.txt
/home/sid/ftp/tested/testing/def.txt
/home/sid/sftp/date/misc/hij.txt


i want a script which would provide me an output like this

Directory Filename
/home/sid/ftp/ abc.txt
/home/sid/ftp/tested/testing def.txt
/home/sid/sftp/date/misc hij.txt

All help appreciated.
# 2  
Quote:
Originally Posted by sidnow
I want to extract the filename from a string.

This is how I have the rawdata ina file


/home/sid/ftp/testing/abc.txt
/home/sid/ftp/tested/testing/def.txt
/home/sid/sftp/date/misc/hij.txt


i want a script which would provide me an output like this

Directory Filename
/home/sid/ftp/ abc.txt
/home/sid/ftp/tested/testing def.txt
/home/sid/sftp/date/misc hij.txt

All help appreciated.
Actually, you can help yourself, if you want. Look at this example:
Code:
file_path="/home/sid/ftp/tested/testing/def.txt"

echo ${file_path%/*} # this will display the directory path
/home/sid/ftp/tested/testing

echo ${file_path##*/} # this will display the file name without path
def.txt

Set up a loop with each absolute path and you have your script.
# 3  
When I put it in a loop it said bad substitution

This is the code

Code:
for i in `cat filename`
do
echo ${$i%/*} echo ${$i##*/}
done

It gave this error

Code:
bash: echo ${file_path%/*}:bad substitution

# 4  
Quote:
Originally Posted by sidnow
When I put it in a loop it said bad substitution

This is the code

Code:
for i in `cat filename`
do
echo ${$i%/*}; echo ${$i##*/}
done

It gave this error

Code:
bash: echo ${file_path%/*}:bad substitution

That was a good try, but it has a couple syntax errors.
Remove the red `$' and add the red `;'

However, that would display path and file in two different lines.
Modify as:

Code:
for i in `cat filename`
do
    printf "%s %s\n" ${i%/*} ${i##*/}
done


Last edited by Aia; 06-14-2015 at 08:22 PM..
This User Gave Thanks to Aia For This Post:
# 5  
Quote:
Originally Posted by Aia
That was a good try, but it has a couple syntax errors.
Remove the red `$' and add the red `;'

Brilliant Aia! It worked, thanks
# 6  
You can also use basename and the dirname to get the output
basename /tmp/abc.txt will give abc.txt
dirname /tmp/abc.txt will give /tmp
This User Gave Thanks to Khanaza For This Post:
# 7  
Quote:
Originally Posted by Aia
That was a good try, but it has a couple syntax errors.
Remove the red `$' and add the red `;'

However, that would display path and file in two different lines.
Modify as:

Code:
for i in `cat filename`
do
    printf "%s %s\n" ${i%/*} ${i##*/}
done


That is almost almost always the wrong way ro read the contents of a file. Not only is cat unnecssary but it will break your script if any lines contain whitespace or other pathological characters. It should be:

Code:
while read i
do
    printf "%s %s\n" "${i%/*}" "${i##*/}" ## note the quotes!
done < "$filename"

This User Gave Thanks to cfajohnson For This Post:

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #798
Difficulty: Medium
To convert from a base-10 integer to its base-2 (binary) equivalent, the number is divided by two and the remainder is the least-significant bit.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract string from filename

Hi I need to extract the string from file name filename: FILENAME_STRUT_01032013_XXXXXXX.TXT I want 01032013 from the above file name. number of characters may differ before the required string but underscores(-) are same number i.e. after second underscore. Please advise on this. ... (2 Replies)
Discussion started by: cnrj
2 Replies

2. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

3. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

4. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

5. Shell Programming and Scripting

extract every filename containing certain string in a directory and do some commend per file

Hi, Here is my question: suppose I have files like 1990_8xdaily_atmos.nc 1991_8xdaily_atmos.nc 1992_8xdaily_atmos.nc 1993_8xdaily_atmos.nc 1990_daily_atmos.nc 1991_daily_atmos.nc 1992_daily_atmos.nc 1993_daily_atmos.nc 1990_month_atmos.nc 1991_month_atmos.nc 1992_month_atmos.nc... (1 Reply)
Discussion started by: 1988PF
1 Replies

6. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

7. Shell Programming and Scripting

loop and extract matching filename

i am unable to exact matching filename in the loop. Filename.in file contains Customer_Product_ Information_Customer_Product_ sale_Product_ /home_dir contains files CUST_INFO_990090_1111.csv "1","Customer Product Detail","1000","salary" "1","Information Customer Product... (2 Replies)
Discussion started by: onesuri
2 Replies

8. Shell Programming and Scripting

How to extract timestamp from the filename?

Hi., My file name is of the format: name_abc_20100531_142528.txt where., my timestamp is of the format: yyyymmdd_hhmmss How to extract the date strring and time string into seperate variables in the shell script, after reading the file as the input? I want to get the variables... (9 Replies)
Discussion started by: av_vinay
9 Replies

9. Shell Programming and Scripting

extract string portion from filename using sed

Hi All, I posted something similar before but I now have a another problem. I have filenames as below TOP_TABIN240_20090323.200903231830 TOP_TABIN235_1_20090323.200903231830 i need to extract the dates as in bold. Using bash v 3.xx Im trying to using the print sed command but... (11 Replies)
Discussion started by: santam
11 Replies

10. UNIX for Dummies Questions & Answers

extract only filename

Hi, I want to display only the file names of the ls command with a delimiter of comma. For example:- ls /etc/dir/N* gives the following output /etc/dir/N1 /etc/dir/N2 /etc/dir/N3 I want the output as N1,N2,N3,-->Here i need the final comma also. Also, here /etc/dir is being... (2 Replies)
Discussion started by: dnat
2 Replies

Featured Tech Videos