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.

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 '15 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.