Cat files situation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cat files situation
# 1  
Old 11-16-2013
Cat files situation

Hello,
I am PhD student (Biomedical sciences) and very new to Linux. I need some help with the following task :

I have files in the following format for their names :


Code:
An_A1_nnn_R1.txt;  An_A1_nnm_R1.txt;  An_A1_nnoo_R1.txt
An_A2_nnn_R1.txt;  An_A2_nnm_R1.txt;  An_A2_nno_R1.txt

..................
An_A900_nnn_R1.txt;  An_A900_nnm_R1.txt;  An_A900_nno_R1.txt

Between An_A(1..900) and R1 the string can be anything.

I need to do this:

Code:
cat An_A1_nnn_R1.txt  An_A1_nnmm_R1.txt   An_A1_nnoo_R1.txt > An_A1.R1.txt



obviously I can do 900 times a cat command but I am sure there should be and easy way using a for loop or something like that.

any ideas?

Thanks in advance

Julio

Moderator's Comments:
Mod Comment Moving thread to proper forum

Last edited by Scrutinizer; 11-16-2013 at 01:01 PM.. Reason: code tags
# 2  
Old 11-16-2013
If you have them all in one directory, you can easily do something like this:

Code:
cat *.txt >> your_big_file.text

This User Gave Thanks to Neo For This Post:
# 3  
Old 11-16-2013
thanks Neo, maybe I did not explain it correctly..
what you suggest will cat all files in the directory...but I need to cat them depending on the number after "An_A" . So, since the name of the file go from An_A1* to An_A900* I finally will have 900 cat results..

---------- Post updated at 05:18 PM ---------- Previous update was at 04:56 PM ----------

I thought on doing something like this:

-----------------------------------
Code:
for i in {1..900}
       do 
                 cat *A$i*R1.txt > A$i.R1.txt
      done

--------------------------------------------

but it does not work...

Last edited by Scrutinizer; 11-16-2013 at 12:22 PM.. Reason: code tags
# 4  
Old 11-16-2013
You should append instead:
Code:
cat *A${i}*R1.txt >> A${i}.R1.txt

This User Gave Thanks to Yoda For This Post:
# 5  
Old 11-16-2013
*A$i*R1.txt would include A$i.R1.txt so A$i.R1.txt would be both read from and written to..

So instead try:
Code:
for i in {1..900}
do 
  cat *_A$i_*_R1.txt >> A$i.R1.txt
done

or
Code:
for i in {1..900}
do 
  cat *_A$i_*_R1.txt > A$i.R1.txt
done

if you want to empty the target files first..

Last edited by Scrutinizer; 11-16-2013 at 12:49 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 11-16-2013
actually , using "trial and error" I just found that all these 3 cat codes do the job


----------------------

Code:
#!/bin/bash

for i in {1..900}
 do 

    #cat *A"$i"*R1* > A"$i".R1.txt
    #cat *A"$i"*R1* >> A"$i".R1.txt
    cat *A${i}*R1.txt >> A${i}.R1.txt

done

---------------------


thanks again Yoda and Neo

Last edited by Scrutinizer; 11-16-2013 at 12:54 PM.. Reason: code tags
# 7  
Old 11-16-2013
Hi Julio, I still see this as problematic. If you repeat the command then the second time the target files will exist and match the pattern, so they will be cat onto themselves.. I did a short test and I ended up with a very big file...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cat N files in a folder

I am new to Linux and I am trying to cat only N files in a folder. N is dynamically given number at runtime. If I give N as 2 then cat only 2 files in the folder and If I give N as 5 then cat only 5 files in the folder. Is there any way to do that? (6 Replies)
Discussion started by: KMusunuru
6 Replies

2. Shell Programming and Scripting

CAT 3 files with the same name in 3 different folders

I am trying to cat 3 files. They all have the same name MFExtract.txt. But, they are in 3 seperate file names. Any idea how I could cat the 3 together. It's throwing me off figuring out a way because the names are the same. I was thinking just doing a 3 cat commands with the full path names and... (2 Replies)
Discussion started by: risarose87
2 Replies

3. UNIX for Dummies Questions & Answers

Printing into two files under difference situation

I want to printing into two files under difference situation. For example, file 1 name.txt >gma-miR172a Glyma02g28845 >gma-miR1513a-3p Glyma02g15840 >gma-miR166a-5p Glyma02g15840 >gma-miR1530 Glyma02g15130 >gma-miR1507a Glyma02g01841 File 2 a.gff Glyma01g07930 ... (4 Replies)
Discussion started by: grace_shen
4 Replies

4. UNIX for Dummies Questions & Answers

Using cat on specific files only

I am concatenating txt-files using cat: cat *.txt > file.dat However, the same directory has the installation instructions included, which is also a txt file: install.txt I currently have the install.txt file renamed to install._txt, but I prefer a solution using regular expressions. Is there... (5 Replies)
Discussion started by: figaro
5 Replies

5. UNIX for Dummies Questions & Answers

Edit files with cat

Hi, sometimes one wants to edit files while still seeing output of earlier commands in terminal. I've found out that cat test && cat - >> test does the trick for displaying file content and adding lines but I believe I saw a much cooler command that was also able to erase lines from files. I cannot... (6 Replies)
Discussion started by: scarleo
6 Replies

6. Shell Programming and Scripting

cat certain files in directories to files named after the dir?

Hi all, I have a directory with many subdirectories each named like so: KOG0001, KOG0002, ...KOG9999. Each of these subdirectories contain a variable number two kinds of files (nuc and prot) named like so: Capitella_sp_nuc_hits.fasta (nuc) and Capitella_sp_prot_hits.fasta (prot). The... (2 Replies)
Discussion started by: kmkocot
2 Replies

7. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

8. Shell Programming and Scripting

cat files in different directories

i have data files in different directories like /jadat /cadat etcc... i have list of this directories in a file files. content of files: ja ca directories: /jadat /cadat file in each of these directories natt_trans_like.dat i need to loop though each directory for a... (1 Reply)
Discussion started by: dsravan
1 Replies

9. UNIX for Dummies Questions & Answers

cat files

Hi, I was a typical Windows guy. Like to do things just by clicking my mouse:cool:. I got a new job now...where they are big on unix. I am trying to wet my fingures now with unix. Haven't taken the dive yet. I am trying to find a solution for this problem. Please help me with some... (4 Replies)
Discussion started by: sandeep78
4 Replies

10. Shell Programming and Scripting

KSH CAT Two files into one

Quick question, File A has one line (10 charachters), File B has one line (10 charachters). how do a concat these two files together to produce a file (File C) that has the contents of Files A + B together onto one line of 20 Charachters if I use: cat FileA FileB > FileC I get the... (5 Replies)
Discussion started by: kshelluser
5 Replies
Login or Register to Ask a Question