![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 09:46 AM |
| unzip particular gzip files among the normal data files | thepurple | Shell Programming and Scripting | 4 | 11-30-2007 07: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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
cat files
Hi,
I was a typical Windows guy. Like to do things just by clicking my mouse 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 |
| Forum Sponsor | ||
|
|
|
|||
|
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 |
|
|||
|
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 11:47 AM. |
|
|||
|
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:
|
|||
| Google The UNIX and Linux Forums |