LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-02-2008, 08:55 AM   #1
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Rep: Reputation: 0
Script to concatenate several files


I need a script to concatenate several files in one step, I have 3 header files say file.S, file.X and file.R, I need to concatenate these 3 header files to data files, say file1.S, file1.R, file1.X so that the header file "file.S" will be concatenated to all data files with .S extentions and so on for "file.X" and file.R with the .X and .R data files..
I tried using wild cards, like:

cat file.S file[1-50].S > file[1-50].SP

but I got a message saying that file[1-50].SP does not match
so can this be done through a script? this will save a LOT of time for me, so if someone can help me it will be much appreciated
 
Old 02-02-2008, 10:36 AM   #2
juanctes
Member
 
Registered: Dec 2004
Location: Argentina, corrientes (far from buenos aires, to the north)
Distribution: Ubuntu :(
Posts: 74

Rep: Reputation: 15
mm

Didn't understood very well what you need, a better explanation perhaps with an example of what you want and not what you need would be much better.
Code:
if [ ! -e headers.S ] ; then
   touch headers.S
fi
for i in `seq 1 50` ; do
   cat file$i.S headers.S >> headers.S.tmp
done
cat headers.S.tmp > headers.S
rm headers.S.tmp
here every file[1-50].S is concatenated to headers.S
I think here you have all you need to make your own version.
 
Old 02-02-2008, 10:52 AM   #3
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Something like this, perhaps?

for((i=1;$i<51;i++)); do cat headers.S file$i.S > file$i.S; done

If the headers should be at the beginning of the resulting file.
 
Old 02-02-2008, 11:04 AM   #4
juanctes
Member
 
Registered: Dec 2004
Location: Argentina, corrientes (far from buenos aires, to the north)
Distribution: Ubuntu :(
Posts: 74

Rep: Reputation: 15
Quote:
Originally Posted by Uncle_Theodore View Post
Something like this, perhaps?

for((i=1;$i<51;i++)); do cat headers.S file$i.S > file$i.S; done

If the headers should be at the beginning of the resulting file.
I think that "cat" throws an error when you use the same file at both sides of redirection...
that why I used a headers.S.tmp.
 
Old 02-02-2008, 11:21 AM   #5
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Oops! It does... Sorry. So, it should be something like this

for((i=1;$i<51;i++)); do cat headers.S file$i.S > tempfile; mv tempfile file$i.S; done

But, anyway, my command does a different thing than yours. I'm trying to add headers.S at the beginning of each file$1.S file. Yours gets all files into one big files... Perhaps, the OP should explain the problem in more detail, since we apparently understood it differently.
 
Old 02-02-2008, 03:00 PM   #6
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Original Poster
Rep: Reputation: 0
it works!

I want to do what Uncle_Theodore's script does exactly
thanks a lot for both of you, I am a beginner in UNIX, so can you please tell me what is the shell for these 2 scripts?
 
Old 02-02-2008, 03:06 PM   #7
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
They're in BASH. juanctes' script should probably work in SH too.
 
Old 02-02-2008, 07:03 PM   #8
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Original Poster
Rep: Reputation: 0
Modification required

Sorry, 1 last question, the script works very fine to files ending with the numbers i.e file1.S, file2.S etc...
I tried applying the same script to files having the numbers between the letters of the name like DSQ_SW01_Blk, DSQ_SW02_Blk, etc... but it didn't work
I used the same code but just changed the following:
cat header.S DSQ_SW0$i_Blk > temp
but it didn't work, so how can I implement that correctly?
 
Old 02-02-2008, 08:53 PM   #9
juanctes
Member
 
Registered: Dec 2004
Location: Argentina, corrientes (far from buenos aires, to the north)
Distribution: Ubuntu :(
Posts: 74

Rep: Reputation: 15
mm

Quote:
Originally Posted by docaia View Post
Sorry, 1 last question, the script works very fine to files ending with the numbers i.e file1.S, file2.S etc...
I tried applying the same script to files having the numbers between the letters of the name like DSQ_SW01_Blk, DSQ_SW02_Blk, etc... but it didn't work
I used the same code but just changed the following:
cat header.S DSQ_SW0$i_Blk > temp
but it didn't work, so how can I implement that correctly?
try this:
Code:
cat header.S DSQ_SW0${i}_Blk > temp
bash is interpreting that your variable is $i_Blk and not just $i
example:
bash-3.1# cat prueba.sh1
#!/bin/bash
for i in `seq 1 10`; do
echo header.S DSQ_SW0${i}_Blk
done

bash-3.1# source prueba.sh1
header.S DSQ_SW01_Blk
header.S DSQ_SW02_Blk
header.S DSQ_SW03_Blk
header.S DSQ_SW04_Blk
header.S DSQ_SW05_Blk
header.S DSQ_SW06_Blk
header.S DSQ_SW07_Blk
header.S DSQ_SW08_Blk
header.S DSQ_SW09_Blk
header.S DSQ_SW010_Blk

Last edited by juanctes; 02-02-2008 at 08:56 PM.
 
Old 02-03-2008, 09:30 AM   #10
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Original Poster
Rep: Reputation: 0
User specified files

everything is working perfect, is there a way to ask the user to enter the first and last file numbers so that the script will work on this range of numbers only instead of editing the script every time?
I used the following code:
i= $1
j= $2
for((i; i<=j))
....and so on
it worked if I enter the script name and 1st and last file numbers, I just want to modify it so that it will ask the user to enter the first file with a message, then it will ask for the last file with another message then works

Last edited by docaia; 02-03-2008 at 09:38 AM.
 
Old 02-03-2008, 01:59 PM   #11
juanctes
Member
 
Registered: Dec 2004
Location: Argentina, corrientes (far from buenos aires, to the north)
Distribution: Ubuntu :(
Posts: 74

Rep: Reputation: 15
root@home:~# ./prueba.sh1
ingrese init
1
ingrese final
2
header.S DSQ_SW01_Blk
header.S DSQ_SW02_Blk
root@home:~# cat prueba.sh1
#!/bin/bash
echo "ingrese init"
read init
echo "ingrese final"
read final
for i in `seq $init $final`; do
echo header.S DSQ_SW0${i}_Blk

---------
advise: Now you could start reading `man bash`

Last edited by juanctes; 02-03-2008 at 02:00 PM. Reason: advise:
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenate PDF files? mykrob Linux - Software 5 11-07-2006 05:25 AM
Need shell script to concatenate a string and a variable into a directory name AwesomeMachine Linux - Newbie 2 05-07-2006 03:42 AM
perl: concatenate arrays jrtayloriv Programming 2 01-23-2005 07:13 AM
how to concatenate variable name in Java ? black Programming 1 11-16-2004 04:08 AM
concatenate binary files???? justin19fl Linux - Newbie 6 05-14-2001 02:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:04 AM.

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
Open Source Consulting | Domain Registration