Operating System - HP-UX
1832858 Members
2567 Online
110048 Solutions
New Discussion

Re: Shell script use to combine several files into one files

 
wfalai
Occasional Contributor

Shell script use to combine several files into one files

Dear professionals,

I want to combine serveral text files into one textfiles. e.g.

Severals Files in a directory:
AAA00001
AAA00002
AAA00003
AAA00004
BDD00001
BDD00002
BDD00003
BDD00004
BDD00005
.....
...
..


These files will be combined into:
AAA00000
BDD00000
....
..
.


Where the content of AAA00000 equal to the combination of AAA00001, AAA00002, AAA00003, AAA00004.


Pls advise

Thanks a lot.
17 REPLIES 17
Systeemingenieurs Infoc
Valued Contributor

Re: Shell script use to combine several files into one files

ls|cut -c-3|
while read fname
do
cat ${fname}* >${fname}00000
done

something like that
A Life ? Cool ! Where can I download one of those from ?
Systeemingenieurs Infoc
Valued Contributor

Re: Shell script use to combine several files into one files

should've been : ls |cut -c-3|sort -u|...
A Life ? Cool ! Where can I download one of those from ?
wfalai
Occasional Contributor

Re: Shell script use to combine several files into one files

Hi,

If I need to count back from the last position, say, 00001 and do a cut there.

The remain text become AAA .

How can I do .?

Thanks a lot
Justo Exposito
Esteemed Contributor

Re: Shell script use to combine several files into one files

Hi,
Try this:
for i in $(ls | cut -c1-3)
do
cat ${i}* >> ${i}00000
done

Regards,

Justo.
Help is a Beatiful word
Justo Exposito
Esteemed Contributor

Re: Shell script use to combine several files into one files

Hi,
Sorry one correction:
for i in $(ls | cut -c1-3 | sort -u)
do
cat ${i}* >> ${i}00000
done

Regards,

Justo.
Help is a Beatiful word
wfalai
Occasional Contributor

Re: Shell script use to combine several files into one files

Thanks all..

I should say that:

"AAA" is the prefix of the filename. However, it is a randomly prefix pattern. So, say, it may be Dedss00001, Dedss00002.....etc., KKKAA00001, KKKAA00002...etc...

Because many many files with different length there and I may not be possible to use " cut -c1-3 " apply to all of the files. So, how can I do so that the script can find how long of the prefix ?

that is, cut -cx-y , where y is the last position of the filename and x is the 6th counting from the last position y.

Thanks ...
Carlos Fernandez Riera
Honored Contributor

Re: Shell script use to combine several files into one files

something like ?

ls -1 *[0-9][0-9][0-9][0-9][0-9][0-9] | awk ' { print " cat" ,substr($1,1,length($1-6)) "[0-9][0-9][0-9][0-9][0-9][0-9] > " substr($1,1,length($1-6)) "0" }'

unsupported
Justo Exposito
Esteemed Contributor

Re: Shell script use to combine several files into one files

Hi,

This is the solution if the last part is always six characters:

for i in $(ls | sed 's/......$//' | sort -u)
do
cat ${i}* >> ${i}00000
done

Regards,

Justo.
Help is a Beatiful word
Carlos Fernandez Riera
Honored Contributor

Re: Shell script use to combine several files into one files


ls -1 *[0-9][0-9][0-9][0-9][0-9][0-9] | awk ' { print " cat" ,substr($1,1,length($1-6)) "[0-9][0-9][0-9][0-9][0-9][0-9] > " substr($1,1,length($1-6)) "0" }'
| sort -u > file_command

sh file_command
unsupported
James R. Ferguson
Acclaimed Contributor

Re: Shell script use to combine several files into one files

Hi:

Given a filename you can use the shell's parameter substitution to get the basename and the length of that token.

Given:

# NAME=/var/tmp/filename

# BN=${F##*/}
# echo $BN
#...returns "filename" or the 'basename'

# L=${#BN}
# echo $L
#...returns the length or here, eight (8)...

Regards!

...JRF...

L=${#F} will
Jean-Louis Phelix
Honored Contributor

Re: Shell script use to combine several files into one files

Hi,

Fix your OLD suffix (one of the current files ie 000001) and NEW suffix (ie 000000) and try this one :

#!/usr/bin/sh
OLDSUF=001
NEWSUF=000
for var in *$OLDSUF
do
PREF=$(echo $var | sed 's/'$OLDSUF'$//')
cat ${PREF}* > ${PREF}${NEWSUF}
done

Regards
It works for me (© Bill McNAMARA ...)
H.Merijn Brand (procura
Honored Contributor

Re: Shell script use to combine several files into one files

# perl -e'local$/;for(<* >){/^(\w+)\d+$/ or next;open$x,"<$_";open$y,">>${1}00000";print$y <$x>}'

unchecked, untested. If taking this to production, add tests to the open calls.

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Shell script use to combine several files into one files

# perl -e'local$/;for(<* >){/^([A-Z]+)\d+$/i or next;open$x,"<$_";open$y,">>${1}00000";print$y <$x>}'

unchecked, untested. If taking this to production, add tests to the open calls.

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Shell script use to combine several files into one files

Ignore the first. An Opera-7 glitch. sorry. N/A
Enjoy, Have FUN! H.Merijn
Rodney Hills
Honored Contributor

Re: Shell script use to combine several files into one files

Here is a perl script that takes in account to NOT process the XXX00000 file as part of the input. It also has the benefit of not having to call external processes to copy each file.

HTH

-- Rod Hills

open(LIS,"ls -1|");
$prv=""
while() {
chomp;
$fil=$_;
next if substr($fil,3,5) eq "00000" ;# ignore result file
$cur=substr($fil,0,3);
if ($prv ne $cur) {
close OUT if $prv;
open(OUT,">${cur}00000");
$prv=$cur;
}
open(INP,"<$fil");
while() { print OUT $_; }
close INP;
}
There be dragons...
H.Merijn Brand (procura
Honored Contributor

Re: Shell script use to combine several files into one files

Good catch Rodney!

# perl -e'local$/;for(<* >){/^([A-Z]+)(\d+)$/i or next;$2>0 or next;open$x,"<$_";open$y,">>${1}00000";print$y <$x>}'
Enjoy, Have FUN! H.Merijn
wfalai
Occasional Contributor

Re: Shell script use to combine several files into one files

Really thanks a lot.

I did the script by reference your examples. Here:

for i in (`ls -1 *[0-9][0-9][0-9][0-9][0-9][0-9]|sort -u)
do
cat ${i}* >> ${i}00000
done

Happy scripting!