LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2011, 11:07 AM   #1
charu
Member
 
Registered: Nov 2010
Posts: 35

Rep: Reputation: 0
Linux Shell script - cat or tail syntax


I am trying to create one large file from several small ones.

I have

$FILE1
$FILE2
$FILE3

And want to check if each exists then if it does create and/or append to a file called
$BIGFILE

I am doing this
fnAppend()
{
#create $BIGFILE using $FILE1
cat $FILE1 > $BIGFILE

#if $FILE2 exists append it to $BIGFILE
if [ -e $FILE2 ];
then
cat $FILE2 >> $BIGFILE
fi

#if $FILE3 exists append it to $BIGFILE
if [ -e $FILE3 ];
then
cat $FILE3 >> $BIGFILE
fi

}


I am getting odd results, such as $FILE1 is being appended to, and $BIGFILE is bigger than it should be.

I have tried various syntaxes to do this, but thought I would ask see if someone could give me the right way to do it without me having to spend any more hours experimenting, as for some reason i seem to be stuck and going round in circles with this, which i think should probably be quite simple, but i guess i am just tired!
 
Old 03-29-2011, 11:29 AM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Can't you just
Code:
cat FILE? > bigfile.txt
 
1 members found this post helpful.
Old 03-29-2011, 11:36 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well apart from the fact that you never test if $FILE1 points to an actual file or that $BIGFILE does not already exist, my guess would be that in all the code you did not show us,
some of your variables may be set incorrectly.

Also, if you include [code][/code] tags around your code as pwc101 has, your indentation will be preserved and code easier to read.
 
1 members found this post helpful.
Old 03-29-2011, 12:34 PM   #4
charu
Member
 
Registered: Nov 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Ah sorry, yes the code tags.

i am actually not using variables, I just put them to save space,but it probably just confused what i was doing, so the actual code I am using is

Code:
fnSlHe()
{
## initially remove any current file as the function may be called more than once##
rm /path/to/file/12498.txt

cat /path/to/file/12498-1.txt > /path/to/file/12498.txt

if [ -e /path/to/file/12498-2.txt ];
then
cat /path/to/file/ >> /path/to/file/12498.txt
fi

if [ -e /path/to/file/12498-3.txt ];
then
cat /path/to/file/12498-3.txt >> /path/to/file/12498.txt
fi
}
If that looks correct then I guess it is something else in the script causing the problem rather than this function, if it does not look correct without the variables, then please let me know and I will search a bit deeper tomorrow after a good nights sleep.
 
Old 03-29-2011, 01:17 PM   #5
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Assuming your shell is bash:
Code:
fnSlHe(){
    # Get a list of the files we'll be iterating over
    files=(/path/to/files/12498-*.txt)

    # Use the first file's name to get a basename for the catted file
    prefix="${files[0]%-*}".txt

    # Check we don't already have a file of that name
    if [ -e "$prefix" ]; then
        printf "Warning: output file (%s) already exists. Aborting.\n" "$prefix"
        return 1
    else
        # OK, so now we need to iterate over all the files in $files, and
        # cat each file in $files out to $prefix.
        for ((i=0; i<${#files[@]}; i++)); do
            cat "${files[i]}" >> "$prefix"
        done
    fi
}
 
1 members found this post helpful.
Old 03-29-2011, 02:39 PM   #6
charu
Member
 
Registered: Nov 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Thumbs up append file to file

Thanks very much, that is great, the iterating through a list of files with that pattern is a much better way of getting the existing files than the way I was trying to do it, much neater and much appreciated.

Last edited by charu; 03-29-2011 at 02:40 PM.
 
Old 03-29-2011, 08:35 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Glad you got a solution, but in answer to why yours was not working and assuming there are no typos in post #4, the offending part is your second file test
and cat:
Code:
if [ -e /path/to/file/12498-2.txt ];
then
    cat /path/to/file/ >> /path/to/file/12498.txt # here you are catting the directory and not the second file
fi
 
1 members found this post helpful.
Old 03-31-2011, 06:59 AM   #8
charu
Member
 
Registered: Nov 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Yes it was a typo I was too tired to be posting on forums that day!

The condensed script using the iteration works perfectly and is great.

I realised today that the problem I was having with my long winded method of trying to do this, was actually to do with something else, but I am glad I asked anyway as I prefer the condensed method and will in future use that for similar functions.

I was going on file size to check if the files had joined successfully. The files were much bigger than they should have been hence I thought it was a problem in this cat function....the file size was though due to a sed command prior to the cat...although my code to cat was pretty bad considering the new code i have now been given.

I was doing this search/replace...
Code:
sed -i 's/\|HeadingOne\|/\|HeadingTwo\|/g' $SLHD3
I was searching for a string containing a pipe, thinking that escaping the pipe with the backslash would be ok, but that was causing some weirdness, making the files huge, leading me to think that the cat command was joining files multiple times.

Anyway that part of the problem has was fixed by using double quotes round the sed command and no escape character for the pipe.
Code:
sed -i "s/|HeadingOne|/|HeadingTwo|/g" $SLHD3
Thankyou

Last edited by charu; 03-31-2011 at 08:05 AM. Reason: yet another typo
 
  


Reply



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] place a shell script with tail -f in the back ground procfs Linux - Newbie 6 10-06-2010 10:29 PM
Shell script-How to call tail then exit when 'END timestamp' appears in log? Mountain Linux - Software 4 05-01-2009 02:29 PM
crontabbed shell script, trying to echo/cat something zaubara Programming 2 06-13-2004 07:18 PM
running root-tail from shell script not working Nuk Linux - Newbie 1 04-09-2004 08:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:20 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