Let's say I have a text file like:
john
george
james
stewert
with every name on a separate line.
I want to read the lines of this text file and create a text file for each name, like: john.txt
, george.txt
etc.
How can I do this in Bash?
bashcommand line
Let's say I have a text file like:
john
george
james
stewert
with every name on a separate line.
I want to read the lines of this text file and create a text file for each name, like: john.txt
, george.txt
etc.
How can I do this in Bash?
Best Answer
#1 Using Bash +
touch
:while read line; [...]; done <in
: This runsread
untilread
itself returns1
, which happens when when the end of the file is reached; the input forread
is read from a file namedin
in the current working directory instead of from the terminal due to the<in
redirection;touch "$line.txt"
: This runstouch
on the expanded value of$line.txt
, which is the content ofline
followed by.txt
;touch
will create the file if not existing and update its access time if existing;#2 Using
xargs
+touch
:-a in
: makesxargs
read its input from a file namedin
in the current working directory;-I name
: makesxargs
replace every occurence ofname
with the current line of input in the following command;touch name
: Runstouch
on the replaced value ofname
; it will create the file if not existing and update its access time if existing;