I'm stuck with this scenario where our system creates files every night and puts them in several folders according from whom it came from.
I have managed to create a script which will list the folders in one column and the files that are in them in another column, now my problem is when some of these folders have multiple files created for the day I have a double entry in the folders column. Below is an example of what I mean.
for dir in Folder*
do
printf "%s" "$dir"
(
cd "$dir"
set -- *
if [ -f "$1" ]
then
printf "\t"
## For bash 3.1 or later:
# printf -v list "%s, " "$@"
## Otherwise:
list=$( printf "%s, " "$@" )
printf "%s" "${list%, }"
fi
)
echo
done
Thank you matrixmadhan & cfajohnson, I wish I could create scripts as good as you guys but unfortunately I have started using the unix enviroment only recently, hence I cant make heads or tails out of your scripts.
I wonder if it would be to much to ask you guys to explain to me the scripts that you have provided.
And cfajohnson, yes, by folders I do mean directories.
for dir in Folder*
## The shell expands Folder* to a list
## of all files and directories beginning with "Folder"
##
## "for" assigns each member of the list in turn to the variable $dir
## and executes the code between "do" and "done" using that value
do
printf "%s" "$dir" ## print the name of the directory with no newline
(
cd "$dir" ## Change the working directory to the one in $dir
## That should have included a test that the cd was successful:
## cd "$dir" ||
## continue ## go to the next iteration of the loop if not successful
## Place all the [non-hidden] files into the positional parameters
set -- *
if [ -f "$1" ] ## check that there actually is a file
then
printf "\t" ## print a TAB character
## Store all the filenames, with commas, in $list
list=$( printf "%s, " "$@" )
## Print list of filenames without newlines
## and without the trailing comma
printf "%s" "${list%, }"
fi
)
echo ## print a newline
done
here is the explanation for the perl code I posted
open(FI, "<", $ARGV[0]) or die "Unable to open file : $ARGV[0] < $! > \n";
# open the file in read mode, first argument to the script is input file
# FI is the file handle
# If unable to open the file, just terminate from the script
my %data_hash = ();
# define a hash data structure
while (<FI> ) {
# while contents from file are available, iterate through the file through the file handle obtained above
chomp;
# remove new line character from the input read from file which is based on $/ as known as INPUT_RECORD_SEPERATOR
my ($folder, $file) = split(/ /);
# split the input record with space as delimiter
push( @{$data_hash{$folder}}, $file);
# key to the hash is folder
# value is an array reference containing list of files corresponding to a folder
# push the file value in an array against the corresponding folder name
}
close(FI) or die "Unable to close file : $ARGV[0] < $! > \n";
# close the file handle that is opened above
foreach my $k ( keys %data_hash ) {
# iterate through the above built hash data structure
print "$k " , @{$data_hash{$k}}, "\n";
# print the key and values
# key - folder name
# value - list of files for a folder
}