Reputation
2,428
Top tag
Next privilege 2,500 Rep.
Create tag synonyms
Badges
7 23 60
Impact
~500k people reached

Aug
16
comment Prevent vendor change of packages
@Rinzwind: Is it a good idea to use zypper on Ubuntu?! I assume that they have packaged it for a reason, it just feels stange ...
Aug
16
comment Prevent vendor change of packages
@Oli: Yes, that is what I want to do :-).
May
25
comment 14.04 - Laptop Battery Indicator does not show the right battery percentage
Did my answer help you? Could you please either give me a comment on my answer or accept it?
May
5
comment Access individual lines of a text file and create separate text file with that name
Could you please choose one of the answers as your accepted one?
May
4
comment Access individual lines of a text file and create separate text file with that name
The read() reads all the lines into memory. What one could improve is to use for item, content in zip(open(…), open(…)): since that only reads the lines as needed. This will then work with arbitrary file sizes since it only uses generator expressions.
May
4
comment Access individual lines of a text file and create separate text file with that name
Although I fear that you will end up with \n at the end of the files. Now that I think about it, either your filenames have \n in them, or you files lack \n at the end of the lines. This is not a problem since there is only one line in it, but I prefer my files to end with \n, so that cat *.txt gives reasonable output.
May
4
comment Access individual lines of a text file and create separate text file with that name
Sure, it will be taken care of. It's not like the computer would break because it ran out of file descriptors. I just like it a habit to not write any code that leaks anything . Your last version looks pretty golfed, but you could make it even shorter by using open(…).readlines(), see this Stack Overflow question ;-).
May
4
comment Access individual lines of a text file and create separate text file with that name
That does work with spaces in the files :-).
May
4
comment Access individual lines of a text file and create separate text file with that name
You could just use done < "$1" in the last line, no need to introduce another variable. The all capital letter variables are usually only used for environment variables (like PATH or EDITOR). When you write a shell script, you can just use lowercase variable names, like you did with count.
May
4
comment Access individual lines of a text file and create separate text file with that name
Using null (\0) as a field delimiter be a bit less arbitrary, programs like find -print0 use this as a delimiter, and xargs -0 can read those. Since \0 is illegal for names in the filesystem, I think, this is fine for filenames. In a text file, you can have \0, I think, so that might break. Also you replace \n with your symbol. Couldn't you use that as a delimiter in awk directly? And if you have a file with DOS or Mac OS line breaks (\n\r and \r), it will not work. Your filenames are now just numbered instead of using the words in the second file. Did I miss something?
May
3
comment Access individual lines of a text file and create separate text file with that name
Given the way the question was posed, I do not see awareness for the troubles that spaces might bring when implementing this with shell scripts. Therefore I assume that the OP just gave a minimal example which does not contain spaces for simplicity, not because he/she is sure that there never will be spaces. I got burned with software not coping with spaces often enough myself, so I try my best to make new software cope with spaces well.
May
3
comment Access individual lines of a text file and create separate text file with that name
I think that will break with spaces in the words as well.
May
3
comment Access individual lines of a text file and create separate text file with that name
The problem with those kind of solutions is that somebody will come along with a file that does contain spaces and then it breaks in a non-obvious way. That is what I do not want to have in my software.
May
2
comment Access individual lines of a text file and create separate text file with that name
You can use for item, content in zip(lines(sys.argv[1]), lines(sys.argv[2])) and have this even shorter. It won't matter in a short script like this, but that would leak file descriptors when you use open () without a context manager (with open() as f:).
May
1
comment Access individual lines of a text file and create separate text file with that name
I see, so the $(…) is needed. But that adds another complication layer about the spaces in the file. I have no idea how to quote that properly. How come you have the most upvotes with a program that cannot cope with spaces‽
May
1
comment Access individual lines of a text file and create separate text file with that name
If you seriously want to use Bash, be sure to read the list of pitfalls. If you think there are to many pitfalls, don't use Bash ;-).
May
1
comment Access individual lines of a text file and create separate text file with that name
Indeed, it will put all the words of both files into the same line. It will then use the first word as the name of the file and the second word as the content. This means that if text1.txt contains two words or more, everything of text2.txt is ignored. – And why did you wrap it in $(…)?
May
1
comment Access individual lines of a text file and create separate text file with that name
You need $1 instead of #1 for the first command line argument. And text2 seems to be fixed, why not use $2.
May
1
comment Access individual lines of a text file and create separate text file with that name
You mean since paste used tab as a delimiter?
May
1
comment Access individual lines of a text file and create separate text file with that name
That will break once f contains a space. You need to use quotes there: "$f.txt".