LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-15-2009, 02:18 AM   #1
mxa055
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
Process text file containing file names and directories


Hello there,

I have a case where I need to return some data written by hand inside a textfile back from a bash script. And because an example will best explain what I want to say here goes.


I have a folder that has some files and some subfolders.

/file1
/file2
/folder1/
/file3
/folder2/
/file4

I write inside this folder a file that contains a list of what I want to be returned in sequence i.e.

#processme.lst#
#insequence
file1
folder1/
file2
file3
folder2/

I need some code that will output:

-file1 (this should only be outputed once no matter how many times the script processes the list file)
-folder1/file5
-folder1/file6
-folder1/file7 (all the files that are contained inside folder1
-file2
-file3
-folder2/file8
-folder2/file9 (same as folder1)

Note: folders contain a processme.lst file as well

The way I have implemented it so far finds the processme.lst file in the folder and reads the #insequence special entry so starts running the insequence script with arguments all the files listed below it.
so "sh insequence.sh file1 folder1 file2 file3 folder2"

if i put just files the script runs fine returning the files one by one in sequence by shifting the arguments. When a folder is found only one file from the folder is returned (this should return all the files). Also I haven't found a way of saying that do not return the first entry ever again.

I tried stopping to shift arguments when a folder is encountered but that got me stack inside the folder and never returned.

Any tips putting me in the right direction would be greatly appreciated
 
Old 10-15-2009, 04:35 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by mxa055 View Post
Hello there,

I have a case where I need to return some data written by hand inside a textfile back from a bash script. And because an example will best explain what I want to say here goes.


I have a folder that has some files and some subfolders.

/file1
/file2
/folder1/
/file3
/folder2/
/file4

I write inside this folder a file that contains a list of what I want to be returned in sequence i.e.

#processme.lst#
#insequence
file1
folder1/
file2
file3
folder2/

I need some code that will output:

-file1 (this should only be outputed once no matter how many times the script processes the list file)
-folder1/file5
-folder1/file6
-folder1/file7 (all the files that are contained inside folder1
-file2
-file3
-folder2/file8
-folder2/file9 (same as folder1)

Note: folders contain a processme.lst file as well

The way I have implemented it so far finds the processme.lst file in the folder and reads the #insequence special entry so starts running the insequence script with arguments all the files listed below it.
so "sh insequence.sh file1 folder1 file2 file3 folder2"

if i put just files the script runs fine returning the files one by one in sequence by shifting the arguments. When a folder is found only one file from the folder is returned (this should return all the files). Also I haven't found a way of saying that do not return the first entry ever again.

I tried stopping to shift arguments when a folder is encountered but that got me stack inside the folder and never returned.

Any tips putting me in the right direction would be greatly appreciated
Once you are able to clearly describe what you want, post again. No one is going to be able to turn your prose into code, and your computer certainly won't be able to either.

To solve this problem, think like a computer -- dumb and literal. List the actions, the steps to get to a solution. Leave nothing to the imagination. If you are specific and literal enough, you should be able to turn your list into a program.

Quote:
... by shifting the arguments
Don't do it that way -- use an array and/or a loop to scan the arguments. I say this because if you pass the arguments through the command line and try to shift them, after seeming to function correctly your program will suddenly complain that the list is too long. Avoid this problem at the outset by avoiding the method at the design stage.
 
Old 10-15-2009, 06:53 AM   #3
mxa055
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Original Poster
Rep: Reputation: 0
hmmm I guess I am not that good at explaining stuff so I will try posting some code.

Code:
if test "x$FILESDIR" != x -a -f "$FILESDIR/echoed_history" ; then
    dat="`date -u -d '12 minutes ago' '+%Y-%m-%d %T'`"

    while read -r d t f ; do
	test "x$d" = x -o "x$t" = x -o "x$f" = x && continue
	[[ "$d $t" < "$dat" ]] && continue
	n="$#"
		while test "$n" -gt 0 ; do
			file="$1"
			test "x${file#/}" = "x$file" && file="${PWD%/}/$file"
			test "x${f#$file}" = "x$f" && set -- "$@" "$1"
			test -f "$file" -o $n -gt 1 && shift
			let "n = n - 1"
		done
    done < "$FILESDIR/echoed_history"
fi
echo -E "$1"
exit 0
What this does is echo the arguments that have not been echoed before (which have been echoed is contained in the echoed_history) for 12 minutes or more. Every time the script is run the next argument in the sequence is being echoed. The problem arises when there is a folder in the list of arguments where I would like to echo that argument as many times as the number of files it contains.

Hope this provides a better explaination of what I am trying to do. Sorry if I fail to explain it better.
 
  


Reply

Tags
bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
cretaing multiple directories based on file names pkacker Programming 6 06-03-2009 05:07 AM
Trying to create directories based on file names JackieBrown Linux - General 3 05-15-2009 09:13 PM
Add text to the beginning of file names xebix Linux - Newbie 5 10-02-2008 12:43 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
copying multiple different files in different directories with identical file names nickleus Linux - Software 4 03-09-2006 01:25 PM


All times are GMT -5. The time now is 07:00 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration