LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-27-2008, 02:51 AM   #1
mksc
LQ Newbie
 
Registered: May 2008
Posts: 10

Rep: Reputation: 0
Exclamation reading path from a text file using bash script


hi all

I have a text file which consist file names with complete path.
I want a script which takeout path of file one by one and store path in a variable and file name in other variable.

i am giving you some part of this text file:

/root/Documents/ListOfnts.ods
/root/Documents/NewCmpnDet.xls
/root/Documents/ComTstChkLst.ods
/root/Documents/compstat.ods
/root/Documents/PDF BOOKS/handbook.pdf
/root/Documents/200804A0/04042008944.jpg
/root/Documents/printer/steps.txt
/root/Documents/AC97/bus.h


with the help of script I want output like this
PATH=/root/Documents/
FILE=ListOfnts.ods


Thanks
MKSC
 
Old 06-27-2008, 03:21 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You could use the basename and dirname commands to split directories from filenames. The first one (basename) will strip the dir part, the other one does the opposite:

Quote:
myPath="/root/Documents/ListOfnts.ods"

$ echo myPath
/root/Documents/ListOfnts.ods

$ basename $myPath
ListOfnts.ods

$ dirname $myPath
/root/Documents
A simple onliner to show it in action, using your example:
Code:
$ for THIS in `cat infile`
> do
> echo $THIS
> DIRNAME=$(basename $THIS)
> echo $DIRNAME
> FILENAME=$(dirname $THIS)
> echo $FILENAME
> done

/root/Documents/ListOfnts.ods
ListOfnts.ods
/root/Documents

/root/Documents/NewCmpnDet.xls
NewCmpnDet.xls
/root/Documents

/root/Documents/ComTstChkLst.ods
ComTstChkLst.ods
/root/Documents
Hope this helps.
 
Old 06-27-2008, 03:25 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can either use the dirname and basename commands into a loop which reads the file line by line, or use the following parameter substitution:
Code:
P=${line%/*}
F=${line##*/}
where $line is the current line read from the file.

An aside note: don't use PATH as name of a variable in your script, since it is a reserved word for the environment variable PATH. If you change its value inside your script, most likely you will get "Command not found" errors for subsequent statements.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
input into bash script(reading path) dalicros Linux - General 3 05-30-2007 06:54 PM
Reading a conf file from a BASH script dinolinux Programming 5 08-03-2005 04:18 AM
Need help reading text file in bash script scilec Programming 3 11-25-2004 06:44 PM
Bash script - reading from text file twantrd Programming 4 11-24-2004 12:38 AM
reading file, bash script marri Programming 3 11-15-2004 09:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration