Visit Our UNIX and Linux User Community

How can i add each line from a txt file to different files in the same directory?

Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How can i add each line from a txt file to different files in the same directory?
# 1  
Old 04-11-2019
How can i add each line from a txt file to different files in the same directory?

Hello, this is my first thread here Smilie
So i have a text file that contains words in each line like

and i have 4 txt files, i want to add each line to each file, like file 1 gets abcd at the end; file 2 gets efgh at the end ....
I tried with:

cat test | while read -r line; do
for i in *; do
        if [ "$i" == "test" ] ; then
        echo $line >> $i

but each time the whole txt is written to each file, i just want each line to each file, not the whole text.

Thanks in advance

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-11-2019 at 05:07 PM.. Reason: Added CODE tags.
# 2  
Old 04-11-2019
Welcome to the forum.

The behaviour you describe is easily understood: you read a line, then echo it to all files in the directory except "test", then read the next line, append to all again, and so forth.

Your request is not quite complete. Does it matter which line goes to which file? What if there's more lines than files / more files than lines?
# 3  
Old 04-11-2019
no it doesn't matter what line goes what file.
for example test has in it:

I have 4 files as well in the same directory.
All i want is: abcd goes to the end of file 1
efgh goes to the end of file 2
ijkl goes to the end of file 3
mnop goes to the end of file 4


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-11-2019 at 05:53 PM.. Reason: Added CODE tags.
# 4  
Old 04-11-2019
Try this slight modification to your attempt:

for i in file[1-4]
  do    read -r line
        if [ "$i" == "test" ] 
          then  continue
        echo $line >> $i
  done < test

This User Gave Thanks to RudiC For This Post:
# 5  
Old 04-11-2019
you're amazing man, it worked.
Thank you sooo much.
# 6  
Old 04-11-2019
If your shell offers "extended pattern matching" with the extglob shell option, you could avoid the if construct in your loop:
shopt -s extglob
ls -la !(test)

will list all files in your current working directory except test.
# 7  
Old 04-11-2019
the first correction you gave me solved my issue, i really appreciate brother. you're amazing.

Previous Thread | Next Thread
Test Your Knowledge in Computers #909
Difficulty: Easy
In computer science, a pointer is a programming language object that stores the memory address of another value located on a hard drive.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cpio all *.txt-files out of folders to just one directory

I need a hint for reading manpage (I did rtfm really) of cpio to do this task as in the headline described. I want to put all files of a certain type, lets say all *.txt files or any other format. Spread in more than hundreds of subdirectories in one directory I would like to select them and just... (3 Replies)
Discussion started by: 1in10
3 Replies

2. UNIX for Dummies Questions & Answers

Split Every Line In Txt Into Separate Txt File, Named Same As The Line

Hi All Is there a way to export every line into new txt file where by the title of each txt output are same as the line ? I have this txt files containing names: Kandra Vanhooser Rhona Menefee Reynaldo Hutt Houston Rafferty Charmaine Lord Albertine Poucher Juana Maes Mitch Lobel... (2 Replies)
Discussion started by: Nexeu
2 Replies

3. Shell Programming and Scripting

Save files in directory as txt

wget -x -i link.txt The above downloads and create unique entries for the 97 links in the text file. However, each new file is saved as CM080 with a FILE extention. Is there a way to convert each file in that directory to a .txt? The 97 files are in... (12 Replies)
Discussion started by: cmccabe
12 Replies

4. UNIX for Dummies Questions & Answers

Script to delete files line by line in .txt document

Friends, I am in need to delete files from a directory on a regular basis. I want to place all the files to be deleted in delete .txt file and require a script to delete files, line by line from this delete.txt file. Please help me with this script as am new to unix scripting. (3 Replies)
Discussion started by: fop4658
3 Replies

5. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

6. Shell Programming and Scripting

create txt file form data file and add some line on it

Hi Guys, I have file A.txt File A Data AK1521 AK2536 AK3164 I want create text file of all data above and write some data on each file. want Output on below folder /home/kka/out AK1521.txt Hi Welocme (3 Replies)
Discussion started by: asavaliya
3 Replies

7. Shell Programming and Scripting

moving the files in a.txt files to a different directory

HI All, I am coding a shell script which will pick all the .csv files in a particular directoryand write it in to a .txt file, this .txt file i will use as a source in datastage for processing. now after the processing is done I have to move and archive all the files in the .txt file to a... (5 Replies)
Discussion started by: subhasri_2020
5 Replies

8. Shell Programming and Scripting

Checking if the files in a directory have a txt extension

foreach file ($dir1/*) if ($file ~ *.txt) then echo "Skipping $file (is a txt file)" endif end that should work right guys? :confused: (15 Replies)
Discussion started by: pantelis
15 Replies

9. UNIX for Dummies Questions & Answers

List all files except *.txt in a directory

I have many types of files (Eg: *.log, *.rpt, *.txt, *.dat) in a directory. I want to display all file types except *.txt. What is the command to display all files except "*.txt" (9 Replies)
Discussion started by: apsprabhu
9 Replies

Featured Tech Videos