2

I am a scripting beginner.
What i am trying to do.

I have a text file that looks like this (example):

hello
hi there
apple

I am trying to make a script that saves every line as a separate new file with the line content as file name.
The name of the files should be as follows (without the .txt extension to the output files):

hello 
hi there
apple

How can i use grep or awk to save every line from a text file as a new file with the line content as file name (without the .txt extension).
Thankyou for any help.

3 Answers 3

1

Forget using grep or awk and do it in your shell.

    while IFS= read -r line
    do echo "$line" > "$line"
    done < FILE
1
  • This script did the job. It's possible that the other script also does the job but this one was first. Thanks a bunch :D
    – linux-dude
    Jul 7, 2015 at 12:40
1

Are you determined to use Grep/Awk?

There is a much easier way to do this:

while read f; do echo "$f" > "$f"; done<file.txt

Make sure to use quotes, especially for the file name.

0

You don't need a loop if you use awkprint (on its own) is equivalent to print $0, where $0 is the full input line, without the trailing \n. The following is enough:

awk '{print>$0}' file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .