Copy content of multiple sh's into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy content of multiple sh's into one
# 8  
Old 05-11-2013
The problem with these is: The sh files are put inside the new sh file without order...


Is there no way to have a newline between each of them ?
# 9  
Old 05-11-2013
Quote:
Originally Posted by pasc
The problem with these is: The sh files are put inside the new sh file without order...


Is there no way to have a newline between each of them ?
try this

Code:
cat *.sh | while read f
 do
echo "$f\n" >> final.sh
done

# 10  
Old 05-11-2013
Quote:
Originally Posted by pasc
The problem with these is: The sh files are put inside the new sh file without order...


Is there no way to have a newline between each of them ?
What do you mean "put inside the new sh file without order"? The input files are put into your output file in alphabetic order.

Unless your existing .sh files do not end with a newline character, there is also a newline between the input files in your output file. Do you mean that you want an empty line to be added after each input file as it is copied into the new file, an empty line to be added before each input file as it is copied into the new file, an empty line to be added between input files as they are copied into the new file, or something else?

Do yoo understand that a shell executing your new file will ignore empty lines in your input file (unless there are mismatched quotes or incomplete here-dociments inside one or more of the input files)? Adding empty lines means you will have to use something in addition to or instead of cat to produce your output file? Can you explain WHY you need to do this?
# 11  
Old 05-11-2013
The problem is that they are put in like this:

Code:
SH1
SH2 SH3
SH4 SH5 SH6...

Newlines are missing between several of them, in turn will stop my commands to work.

The commands are simple:
"mv XXXXX YYYYY" style. when the commands are lined up beside each other that will not work like it should.

Correct Form:
Code:
SH1
SH2
SH3
SH4...

@zozoo: That code you posted will actually ADD /n into my final script file... resulting in that not working.
# 12  
Old 05-11-2013
Quote:
Originally Posted by pasc
The problem is that they are put in like this:

Code:
SH1
SH2 SH3
SH4 SH5 SH6...

Newlines are missing between several of them, in turn will stop my commands to work.

The commands are simple:
"mv XXXXX YYYYY" style. when the commands are lined up beside each other that will not work like it should.

Correct Form:
Code:
SH1
SH2
SH3
SH4...

@zozoo: That code you posted will actually ADD /n into my final script file... resulting in that not working.
Hi pasc,
In your first post in this thread, you said:
Quote:
they all contain one line of text: (this is just an example, the content itself looks different).
but that was a lie if you're getting the output:
Code:
SH1
SH2 SH3
SH4 SH5 SH6...

To get that output the files that contained SH2, SH4, and SH5 did not contain a line of text. By definition, a line contains a terminating newline character which is not present in those three files. Assuming that each of your input files contains exactly one line or exactly one partial line (missing a newline), try the following:
First, save the following in a file named cat1line
Code:
#!/bin/ksh
if [ $# -lt 2 ]
then    printf "Usage: %s output_file input_file...\n" "${0##*/}" >&2
        exit 1
fi
of="$1"
shift
> "$of"
while [ $# -gt 0 ]
do      (cat $1;echo)| {
                IFS="" read -r line
                printf "%s\n" "$line" >> "$of"
        }
        shift
done

Second, make it executable:
Code:
chmod +x cat1line

Third, run it:
Code:
./cat1line output_pathname *.sh

# 13  
Old 05-12-2013
Actually scratch everything I said...

the problem was:

the lines contained a " " tab.
This messed up my programs parsing and made it "seem" like there were no newlines.... all in all viewing it on pc revealed that the first given cat approach definitly works fine:

Code:
cat *.sh > /var/mobile/backup.sh


Last edited by pasc; 05-12-2013 at 08:25 AM..
# 14  
Old 05-12-2013
Quote:
Originally Posted by pasc
by "a lie" you mean some don't contain text ?
If so: no, some just contain "less" text than others...
By a lie, I mean you said each file contained a line of text. By definition, a line is terminated by a <newline> character. The output you displayed shows that half of the first six input files did not contain a line.

The script I gave you should work even though some of the files didn't contain a line. It won't work if one or more of the input files contain any characters after a <newline> character that you want to appear in the output file.

---------- Post updated at 04:43 ---------- Previous update was at 04:34 ----------

Quote:
Originally Posted by pasc
Actually scratch everything I said...

the problem was:

the lines contained a " " tab.
This messed up my programs parsing and made it "seem" like there were no newlines.... all in all viewing it on pc revealed that the first given cat approach definitly works fine:

Code:
cat *.sh > /var/mobile/backup.sh

Containing a tab character doesn't account for the output you showed us in message #11 in this thread. Only the lack of a <newline> character accounts for the output you showed us in that message.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash selective copy folders and content to another location

I'm looking for a bash scrypt to copy some folders and some of the content to another location. I'm a teacher and very noobish with programming language anyway what I'm looking for , I have this director structure Main director "Students" with subfolders "john";"daisy";"work" etc .. and some of... (2 Replies)
Discussion started by: brickleul
2 Replies

2. Shell Programming and Scripting

How to replace multiple file content?

Hi Gurus, I need replace multiple files content. the file name pattern likes currentfile_code_* the content pattern in the file like text=value I need replace the content as text=abcde Thanks in advance (7 Replies)
Discussion started by: ken6503
7 Replies

3. UNIX Desktop Questions & Answers

copy content of file to another

I have a file called part1.pdb...contents are: ATOM 1 N SER 1 -10.295 -8.909 10.913 1.00 0.00 ATOM 2 H1 SER 1 -10.230 -8.214 10.183 1.00 0.00 ATOM 3 H2 SER 1 -9.558 -8.745 11.584 1.00 0.00 ATOM 4 H3 SER 1 -11.255 ... (1 Reply)
Discussion started by: kanikasharma
1 Replies

4. Shell Programming and Scripting

How to copy mail content in a file in Unix

Hi Guys I want to write a script which search mail with subject line and after that I want the mail content in a file please help guys. Thanks Atul Singh (3 Replies)
Discussion started by: atul9806
3 Replies

5. Shell Programming and Scripting

copy content of file to another files from certain position

Hello Guys I have a directory of xml files (almost 500 ) Now in each xml file 3 new attributes will be added which are in a different single file Is there any way I can copy the whole content of single file at 6th line of all the files in the directory . Thanks a lot!!! (7 Replies)
Discussion started by: Pratik4891
7 Replies

6. UNIX for Dummies Questions & Answers

Deleting Multiple Files With the Same Content

My website has been hacked and i need to remove a lot of unique files with same content. The files have very similar code: eval(base64_decodeHow do i find multiple/all the files with the above code and remove them? Thanks. (10 Replies)
Discussion started by: Boris_yo
10 Replies

7. Shell Programming and Scripting

Get multiple line content between two chars

Hello - I have a file that has the something like the following : REM CREATE TABLE lots of text REM table specifc creation text ; REM ALTER TABLE lots of text REM text specific to the the alter command REM could be more lines of text; What I need is to get all the lines for the ALTER... (2 Replies)
Discussion started by: Feliz
2 Replies
Login or Register to Ask a Question