View Full Version : Bash Script - Create Files
GiantCrab
December 3rd, 2009, 08:52 PM
So I have a text file in the format of:
>filename
contents of file
>filename2
contents, etc.
I'd like to create multiple files with the filenames listed in the text file, the contents of which are below it. How would I do this?
falconindy
December 3rd, 2009, 09:44 PM
This'll do:
#!/bin/bash
sourcefile=/path/to/source
while read line; do
[[ ${line:0:1} = ">" ]] && outFile=${line:1} || echo $line >> $outFile
done < $sourcefile
GiantCrab
December 3rd, 2009, 10:55 PM
Thanks for the help! I implemented the code, but it keeps spitting out:
$outfile: ambiguous redirect
What exactly is wrong?
falconindy
December 3rd, 2009, 11:39 PM
Post your source file. If I had to guess, I'd say that the first line isn't formatted as ">Filename".
zman121
December 3rd, 2009, 11:48 PM
Watch the case. The script used camelCase, your error message indicates all lower-case.
GiantCrab
December 4th, 2009, 12:50 AM
Whoops, I think it was the case. It works now. The only problem I have now is that the entries have spaces before and after the line that I'd like to retain, and this script gets rid of those spaces. Any way I could retain those spaces?
>testfile
there are spaces before and after this
>testfile 2
more spaces
falconindy
December 4th, 2009, 01:14 AM
Whoops, I think it was the case. It works now. The only problem I have now is that the entries have spaces before and after the line that I'd like to retain, and this script gets rid of those spaces. Any way I could retain those spaces?
>testfile
there are spaces before and after this
>testfile 2
more spaces
Leading and trailing whitespace, you say? Guess I could show off my noobish Python skills:
#!/usr/bin/env python
source = './sourceFile'
out = open(source) #filthy hack
for line in open(source).read().split('\n'):
if line.startswith('>'):
out.close()
outFile = line[1:]
out = open(outFile, 'w')
else:
out.write(line + '\n')
Change the source variable to point to your file and you're good to go.
GiantCrab
December 4th, 2009, 01:29 AM
Thanks alot falcon, works dandy. I'll have to spend some time looking at a good python book.
ghostdog74
December 4th, 2009, 11:11 AM
here's your bash script
while read -r line
do
case "$line" in
">"* )
filename=${line#*>};;
* )
echo "$line" >> $filename ;;
esac
done < "file"
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.