Reputation: 383
I need to prepare a csv as a means to load data. I have a directory which holds n number of html files. The code of each of html file needs to be copy and pasted into a csv field. Ideally this is what I would like the csv to look like
FileName Code
filename1.html contents of filename1.html here
filename2.html contents of filename2.html here
I know there is way to pipe the ls of the directory into a csv, however I am not sure on how to achieve the following result. I am thinking that I probably need create a loop and have the file appended each time with delimiter added at each interval. But I am not quite sure where to start. Writing this as a shell/bash script would be best
Any help would be appreciated
Upvotes: 0
Views: 1355
Reputation: 1732
Not bash, but simple ruby script to make a CSV file with filename and file content as columns:
require 'csv'
@path = ARGV[0]
files = []
files << ['filename', 'contents']
Dir.foreach(@path) do |filename|
next if filename == '.' or filename == '..'
files << [filename, File.read("#{@path}/#{filename}")]
end
File.write("#{@path}/result.csv", files.map(&:to_csv).join)
Usage: ruby script.rb <path>
Upvotes: 0
Reputation: 121
a
and b
that I have mentioned aboveshell.sh
with the contents of the script inside this folderRun the following command to make your shell script executable
chmod +x shell.sh
Before running the script, change the line from:
files=$(ls)
to
files=$(ls | grep -v shell | grep -v csv)
Then run the following command to see the output of the script
./shell.sh
Now redirect it to your csv file:
./shell > file.csv
To see the content of the file file.csv
run:
cat file.csv
Upvotes: 1
Reputation: 121
Say you have two files:
file a
contains:
abc
123
file b
contains:
def
456
so:
#!/bin/bash
echo "Filename,Code"
files=$(ls)
for file in ${files[@]}; do
echo -n $file,
tr -d '\n' < $file
echo ""
done
The above code will output:
Filename,Code
a,abc123
b,def456
Upvotes: 0