1

I am trying to merge two files of different lengths using a script. I want the longer file to be appended to the end of the shorter file and when both files are of equal length they are suppose to merge in any order My script so far:

#!/bin/csh -f
echo ENTER a file name
set fl1 = $<
echo ENTER a second file name
set fl2 = $<

var1 = $( wc -c $fl1 )
var2 = $( wc -c $fl2 )

if ( ! -e $fl1 && ! -e $fl2 ) then
        echo File does not exist
        echo -n Please enter a vaild file name

else if { $var1 -lt $var2 } then
        $fl2 >> $fl1
else if { $var1 -gt $var2 } then
        $fl1 >> $fl2
else if { $var1 -eq $var2 } then
        $fl2 >> $fl1 || $fl1 >> $fl2
endif
  • 1
    This is not an answer, but you really might want to consider not using csh for scripting. Here's a nice explanations of why: Csh Programming Considered Harmful. – terdon Nov 27 '15 at 14:00
  • 1
    This script seems like a strange mix of csh, Bourne shell, and tcl... – Martin Tournoij Nov 27 '15 at 14:37
2

There are a number of problems with your script. The first and most important is that you're using csh. Don't. Among its many failings as a scripting language, csh doesn't support $(), you need backtics. You also need cat in order to concatenate files, you can't just execute the file as though it were a program. Not if you want to print its contents, anyway. You also have the if syntax wrong. Finally, you don't need else if. Since you only have two actions to perform, what's the point of checking for three different conditions? All conditions but one will do the same thing, so just use if ... else.

A working version of your script in csh would be:

#!/home/terdon/bsd-csh -f
echo ENTER a file name
set fl1 = $<
echo ENTER a second file name
set fl2 = $<

set var1 = `wc -c < "$fl1"`
set var2 = `wc -c < "$fl2"`


echo "V: $var1 $var2"
if ( ! -e "$fl1" && ! -e "$fl2" ) then
        echo File does not exist
        echo -n Please enter a vaild file name

else if ( "$var1" > "$var2" ) then
      cat "$fl2" >> "$fl1"
else
        cat "$fl1" >> "$fl2"
endif

Here's a script with the same functionality in bash, just to illustrate what you're missing:

#!/bin/bash
read -p "ENTER a file name:" fl1
read -p "ENTER a second file name:" fl2

[[ -e "$fl1" &&  -e "$fl2" ]] ||
    echo "File does not exist, please enter a vaild file name" && exit;

if [[ $(wc -c "$fl1") -lt $(wc -c "$fl2") ]]
then
    cat "$fl2" >> "$fl1"
else
    cat "$fl1" >> "$fl2"
fi
| improve this answer | |
  • I keep getting the error if: Badly formed number – NOOBCAKE Nov 27 '15 at 15:40
  • @NOOBCAKE using which script? With what input? – terdon Nov 28 '15 at 2:21
0

If nothing works you can always enclose a command that does what your trying to do (well almost) in your script, there are many ways to do it for example

to join file1.txt, file2.txt and file3.txt consecutively together to new file: file4.txt use:

copy/b file1.txt +file2.txt +file3.txt file4.txt

or:

copy/b *.txt newfile.txt 

you can also commands like pr and vim variants to achieve it. How to use this commands in your script( if nothings working) I will leave that you. But yes this is a bad practice. This should be your last resort.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.