0

I have have a text file with many lines like this:

Smith: ''Handbook of Everything.'' Fantasy Press, 2000.
Wilson: ''Unix for Dummies.'' Wiley, 1993.

I want to write every line into a new file. The new file should contain the whole line and should be named same to the line it contains.

Smith: ''Handbook of Everything.'' Fantasy Press, 2000.txt
Wilson: ''Unix for Dummies.'' Wiley, 1993.txt

How do I accomplish this? Thanks.

αғsнιη
  • 41,407
lejonet
  • 103

3 Answers3

4

Using awk:

awk '{ fname=$0 "txt"; print > fname; close(fname) }' file
αғsнιη
  • 41,407
Freddy
  • 25,565
2

If your textfile is called textfile, the following should do the job

while read -r x
do
    echo "$x" > "${x}txt"
done < textfile
αғsнιη
  • 41,407
unxnut
  • 6,008
1

You can create a bash script as shown below. Replace /tmp/textFile.txt with the absolute path to your text file.

#!/bin/bash
file=/tmp/textFile.txt
while IFS= read -r line
do
        printf "%s\n" "$line" > /tmp/"$line"txt
done < "$file"
bit
  • 1,106