cat files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers cat files
# 1  
Old 02-14-2008
cat files

Hi,

I was a typical Windows guy. Like to do things just by clicking my mouseSmilie. 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 ideas.

Now the problem:
For ex: I have 6 files.
input_1.txt
input_2.txt
input_3.txt
input_4.txt
input_5.txt
input_6.txt

I want to write a script to concat the files on the parameter I pass to the script. Say If I pass parameter 2 to the script.
It should do this....
cat input_1.txt input_2.txt input_3.txt > input_1.txt
cat input_4.txt input_5.txt input_6.txt > input_2.txt

So, If I pass 3 to the script.
It should do this....
cat input_1.txt input_2.txt > input_1.txt
cat input_3.txt input_4.txt > input_2.txt
cat input_5.txt input_6.txt > input_3.txt

And the initial files I have can be dynamic....one day...I may have 20 or 30 files. Could be 25 (odd) too. The script should be dynamic enough.
I hope...my problem is clear enough for you.

Thanks for all your time.
-Sandeep
# 2  
Old 02-14-2008
How does the script know how many files you have to deal with? Will that be an additional parameter?

Let's say you have n input files and want j output files. Do you want each outputfile to comprise n/j input files? Do you always want the output file to be named the same as the input file? In the same directory?
# 3  
Old 02-15-2008
Hi,
Please find my answers below.

How does the script know how many files you have to deal with? Will that be an additional parameter?
Yes. Sorry forgot to mention that part.
The script should take 2 parameters.
#1 the no.of input jobs
#2 the no.of output jobs


Let's say you have n input files and want j output files. Do you want each outputfile to comprise n/j input files?
Yes that is what I expect. n/j input file.

Do you always want the output file to be named the same as the input file? In the same directory?
Yes.

Once again, thanks a lot for your time

-Sandeep
# 4  
Old 02-15-2008
Very basic script, no error checking etc etc ect.

Script takes 1 parameter, number of output files needed,

Script assumes all files which should be concatenated start with input_

Script assumes all files are in the current directory.


Should be easy to change the script into something more luxerary, it just presents a concept.

Code:
#!/usr/bin/bash
NUMOUT=${1}

NUMFILES=`ls input* | wc -l`

((IN_PER_OUT=${NUMFILES} / ${NUMOUT} + 1))

CURRENT=1
OUT=1
ls input* | while read FILE
do
  if [ ${CURRENT} -eq 1 ]
  then
    cat ${FILE} > output_${OUT}.txt
  else
    cat ${FILE} >> output_${OUT}.txt
  fi

  ((CURRENT=${CURRENT}+1))
  if [ ${CURRENT} -gt ${IN_PER_OUT} ]
  then
    CURRENT=1
    ((OUT=${OUT}+1))
  fi
done


Last edited by sb008; 02-15-2008 at 03:47 PM..
# 5  
Old 02-15-2008
Thank you, I tried your script.
It is working close to what I need.
I tweaked it a bit to meet my exact requirements.

Thanks again
-Sandeep


Quote:
Originally Posted by sb008
Very basic script, no error checking etc etc ect.

Script takes 1 parameter, number of output files needed,

Script assumes all files which should be concatenated start with input_

Script assumes all files are in the current directory.


Should be easy to change the script into something more luxerary, it just presents a concept.

Code:
#!/usr/bin/bash
NUMOUT=${1}

NUMFILES=`ls input* | wc -l`

((IN_PER_OUT=${NUMFILES} / ${NUMOUT} + 1))

CURRENT=1
OUT=1
ls input* | while read FILE
do
  if [ ${CURRENT} -eq 1 ]
  then
    cat ${FILE} > output_${OUT}.txt
  else
    cat ${FILE} >> output_${OUT}.txt
  fi

  ((CURRENT=${CURRENT}+1))
  if [ ${CURRENT} -gt ${IN_PER_OUT} ]
  then
    CURRENT=1
    ((OUT=${OUT}+1))
  fi
done

 
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. Shell Programming and Scripting

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 : 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 ... (8 Replies)
Discussion started by: Julio Finalet
8 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. Shell Programming and Scripting

cat /proc/ files

Hi, I need to write a shell script that should lists only the files that starts with alphabet from the /proc dir and then I have to cat those files and redirect to a file. But my below script is not working. It is reading the first file properly but not the subsequent files. Please throw a... (2 Replies)
Discussion started by: royalibrahim
2 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