LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-19-2012, 12:03 PM   #1
puntino
Member
 
Registered: Sep 2006
Location: Italy
Distribution: Suse 10.1
Posts: 71

Rep: Reputation: 15
shell script to concatenate files


Hi,
I have a folder with files that have the following names (imagine you typed ls in this folder)
open4233.kbehavior stat3758.kbehavior writev4139.kbehavior
open4235.kbehavior stat3760.kbehavior writev434.kbehavior
open4237.kbehavior stat3762.kbehavior writev912.kbehavior
open4239.kbehavior stat3778.kbehavior
open425.kbehavior stat3780.kbehavior

I would like to concatenate all the files whose name that include the word "open" in a unique file,
I know that I can type cat open*.kbehavior > open_merged.kbehavior
and in the same way I can work with the other files (write, stat).
But, since I have several files with the same stem (e.g. readXXX, closeYYYY, ... )
I would like to write a script that automatically merge them in one file (e.g all read in read_merged). I don't know a priori the number of digits following the stem.
Any hint?
Thank you in advance,
 
Old 11-19-2012, 12:10 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Hi,

concatenating is done with >>
Code:
cat open*.kbehavior >> open_merged.kbehavior
And all together
Code:
for i in open write stat; do cat $i*.kbehavior >> $i_merged.kbehavior; done
Markus
 
1 members found this post helpful.
Old 11-19-2012, 12:22 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
You could have used:
Code:
ls -la | grep "open*"
Or
ls -la | grep "stat*"
Or you can simple do:
Code:
ls | grep "open*" > /path/to/output_file
 
Old 11-20-2012, 01:33 AM   #4
puntino
Member
 
Registered: Sep 2006
Location: Italy
Distribution: Suse 10.1
Posts: 71

Original Poster
Rep: Reputation: 15
Ok Both are fine solutions. The problem is that I don't know the name of the all the stem in advance. It's very hard to know all them because in the folder there is a huge amount of files.
There are always files that I'm likely to miss (for instance some named sendfile1).I can't lidt all them and write a for loop like "for i open ...".
It would be nice that the script automatically seeks all the stems e.g. read, sendfile,open,write,etc and then concanate as we said.
 
Old 11-20-2012, 02:24 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Its hard to tell exactly what you want. Do you want to concatenate all files of the form <word><number>.behavior so that all filenames starting with the same word are concatenated.

You could take a listing of the files, use sed or awk to remove from the number on, thenI sort the list words, removing duplicates. Then in a loop concatenate the files beginning with the stem word.

Code:
ls [[:alpha:]]*[[:digit:]]*.behavior |
    sed  's/\([[:alpha:]]*\).*/\1/'   |
     sort -u >word.list
If one stem word is contained in a larger one, watch how you construct the wild card in your loop.
For example: cat ${word}[[:digit:]]* >${word}.behaviour_catted
Instead of ${word}*.
 
1 members found this post helpful.
Old 11-20-2012, 02:50 AM   #6
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,706

Rep: Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271
Hi,

you can get a list of the four char stems with:
Code:
(for f in  * ; do echo $f | cut -c1-4 ;done)|sort | uniq
Evo2.
 
1 members found this post helpful.
Old 11-20-2012, 02:56 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
The stem word may be any number of characters , from what I understand of the question. There isn't a need for the initial for loop to generate the file names.
 
Old 11-20-2012, 04:51 AM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
In case if you've lot of files, then you can search them as:
Code:
find /path/to/dir -name "stem*" -name "open*" -name "sendfile*" -name "write*" -exec ls -la {} \; | awk -F"" '{print $9}' >> /path/to/output_file
But I suggest you to once go through man page of find command, then try it and add it to the script.
 
1 members found this post helpful.
Old 11-20-2012, 06:11 AM   #9
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Code:
for file in *; do newfile=`sed 's/\([a-z]\+\)[^a-z]\+.kbehavior/\1_merged.kbehavior/p' $file`; cat $file >> $newfile; done
You don't need find. Use sed to generate the new filename.

Markus

Last edited by markush; 11-20-2012 at 06:21 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to concatenate strings in Shell kofucii Linux - Newbie 2 12-12-2009 05:07 AM
Script to concatenate several files docaia Linux - General 10 02-03-2008 01:59 PM
How to concatenate two strings into one string in B-shell? jimmyjiang Red Hat 5 01-08-2008 01:15 PM
Need shell script to concatenate a string and a variable into a directory name AwesomeMachine Linux - Newbie 2 05-07-2006 03:42 AM


All times are GMT -5. The time now is 12:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration