0

I have file with different text on each line like:

blue  
red  
black
blue and red
red and black

How to create file for each line of text and the file names to be the same as the text? And also to add extension to the files? The files should look like this:

blue.txt  
red.txt  
black.txt
blue and red.txt
red and black.txt

Edit: i made a mistake because i have also some lines of text that contain multiple words with space between them. sorry.

user1800997
  • 183
  • 1
  • 1
  • 11
  • Possible solution you can found in the thread https://stackoverflow.com/questions/20796200/how-to-iterate-over-files-in-a-directory-with-bash – A.Newgate Oct 04 '18 at 08:24

3 Answers3

4

Read the input file line by line:

while read basename ; do
    touch "$basename".txt
done < list_of_names.txt
choroba
  • 231,213
  • 25
  • 204
  • 289
  • Are you in bash? If you put everything on one line, you need to insert a semicolon before `done`. – choroba Oct 04 '18 at 09:13
  • Yes, i am on bash and code is not on one line. I just created .sh file and copy the code in the file, then if i run the script like this ./script.sh i get the error. – user1800997 Oct 04 '18 at 11:06
  • Weird, it works for me. Try prepending `#! /bin/bash` as the first line of the script. – choroba Oct 04 '18 at 12:22
1

You could use GNU Parallel like this:

parallel touch {}.txt < filelist

If you want to see what it would do, without actually doing anything, use:

parallel --dry-run touch {}.txt < filelist
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
1

Using sed and xargs:

sed -e 's/$/.txt/' input.txt | xargs -d '\n' touch

where

  • sed substitutes end of the line ($) to .txt for each line read from input.txt file;
  • xargs builds a list of arguments from the result of the sed command and passes it to touch command;
  • -d '\n' option specifies newline character \n as a delimiter for the input items.
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • This works but adds strange symbol between the extension and the file name: `blue and red.txt` – user1800997 Oct 04 '18 at 08:34
  • @user1800997, could you share the output of `hexdump -C < input.txt` command (preferably, for an `input.txt` file containing only this particular string)? I suspect the source line contains unusual trailing characters – Ruslan Osmanov Oct 04 '18 at 09:48
  • I tested this only with one line of text and the sign do not appear, it appears only if there are more then one line. I guess this is a sign for the 'enter' key or line break. – user1800997 Oct 04 '18 at 10:58
  • @user1800997, I guess you have an issue with the clipboard. choroba's answer is correct and should work as well as mine, but you are still getting syntax errors. Could you try to enter the command manually? – Ruslan Osmanov Oct 04 '18 at 11:38
  • @user1800997 at least the solution works in Bash on Tutorialspoint: http://tpcg.io/vIk5Jr – Ruslan Osmanov Oct 04 '18 at 11:49
  • For me it's not working this way, but i still could use the command. I just have search and remove this symbol after the files are created. – user1800997 Oct 04 '18 at 12:17
  • The problem was that i was creating the input.txt file on Windows 7, my host os, and then copy it to Debian on virtualbox machine. I had to convert the file to unix format using dos2unix and now the command works without adding symbols in the end. – user1800997 Oct 04 '18 at 14:32