The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Splitting input files into multiple files through AWK command arund_01 Shell Programming and Scripting 3 05-13-2008 06:17 AM
Find duplicates from multuple files with 2 diff types of files ricky007 Shell Programming and Scripting 2 03-04-2008 10:46 AM
unzip particular gzip files among the normal data files thepurple Shell Programming and Scripting 4 11-30-2007 08:17 AM
when I try to run rm on multiple files I have problem to delete files with space umen UNIX for Dummies Questions & Answers 1 09-20-2005 12:20 AM
text files, ASCII files, binary files and ftp transfers Perderabo Answers to Frequently Asked Questions 0 04-08-2004 01:25 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 02-14-2008
Registered User
 

Join Date: Feb 2008
Posts: 6
cat files

Hi,

I was a typical Windows guy. Like to do things just by clicking my mouse. 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
Reply With Quote
Forum Sponsor
  #2  
Old 02-14-2008
otheus's Avatar
Moderator ala Mode
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 600
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?
Reply With Quote
  #3  
Old 02-15-2008
Registered User
 

Join Date: Feb 2008
Posts: 6
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
Reply With Quote
  #4  
Old 02-15-2008
Registered User
 

Join Date: Jan 2007
Posts: 366
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 12:47 PM.
Reply With Quote
  #5  
Old 02-15-2008
Registered User
 

Join Date: Feb 2008
Posts: 6
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 View Post
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0