LinuxQuestions.org
Review your favorite Linux distribution.
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 07-01-2012, 10:47 AM   #16
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191

hmm ... usual story is that it might be nice to see a before and after of the data, how about:
Code:
awk 'ORS=length?" ":"\n"' infile > outfile
 
Old 07-01-2012, 12:12 PM   #17
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
I am beginning to see the complexities involved in the solution to this problem. So, and begging your pardon to all of you, dear LQers, I think I'll write a nice little program in C, language which, as this thread has shown me, is simpler than the bash script language. And yet,its manuals can fill libraries. My regards to all who have collaborated in this thread.
 
Old 07-02-2012, 01:33 AM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well as the OP it is of course always your choice on the direction you take, but I am curious as to what you have found
complex about any of the solutions? If none are meeting your requirements we are happy to see more examples to try and
assist
 
Old 07-02-2012, 03:35 AM   #19
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by stf92 View Post
... I think I'll write a nice little program in C, language which, as this thread has shown me, is simpler than the bash script language. And yet,its manuals can fill libraries.
The C language itself is elegantly small and simple. It is the libraries developed for it that are huge and complex so need libraries of documentation.
 
Old 07-02-2012, 04:53 AM   #20
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
The truth is I want to have a program I understand and can therefore modify at will. In this respect, maybe some of you shall want to see this thread. Once again, I express my admiration for your labor in this forum.
 
Old 07-04-2012, 01:30 AM   #21
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
This is a possible solution to the problem. The program is self-documenting.

Code:







/* FILE NAME:  strip
 * PROG APPL:  postprocessor for when I post to LQ and write in the
 *   console.
 *
 * Compile with 'gcc -Wall -o strip strip.c'
 *
 * 
 * 1.  Removi la verificacion de EOF en la fputc y sigue andando (Restaurar).
 * 2.  
 *
 *
 * 
 *                    ANDANDO
 *
 *
 */

/*
 * Based on u2d.c, from Pacific.
 *
 */

#include        <stdio.h>
#include        <stdlib.h>

/*
 *      Unix To DOS text file conversion utility. 
 *
 *      Converts the Unix style end-of-line sequence 0AH
 *      to MS-DOS style end-of-line sequence 0DH 0AH
 */

#define TMPNAME "val"
#define LF 0x0a
#define CR 0x0d
#define BL 0x20

/*
 *      int     fixfile(char * filename)
 *
 *      "Fix" the text file specified by "filename".
 *      Returns 0 on error, non zero otherwise.
 */

int fixfile(char * filename) {
        FILE *          infile;
        FILE *          outfile;
        char            ch;
        unsigned long   in, out;

        printf("u2d: %s: ", filename);
        fflush(stdout);
        infile = fopen(filename, "r");  /* try to open input file */
        if (!infile) {
                printf("not found\n");
                return 0;
        }
        outfile = fopen(TMPNAME, "w");  /* try to open scratch file */
        if (!outfile) {
                fclose(infile);
                printf("unable to open temp file %s\n", TMPNAME);
                return 1;
        }
        in = out = 0;
        while ((ch = fgetc(infile)) != EOF) {   /* while not end of file */
        if (ch == 'LF') {
            /*******************/
            ch= fgetc(infile);
            if(ch == LF){
                fputc(LF, outfile);
                fputc(LF, outfile);
            } else {
                fputc(BL, outfile);
                fputc(ch, outfile);
            }        
                
        
            /*******************/
        }
        else {
            fputc(ch, outfile);
        }   
        }
        fclose(infile);
        fclose(outfile);
    
        printf("converted: %lu bytes read, %lu bytes written\n", in, out);
        return 0;
}

main(int argc, char ** argv)
{
        int     i;

        for (i = 1; i != argc; i++) {
                if (fixfile(argv[i])) {
                        printf("u2d: aborted!\n");
                        exit(1);
                }
        }
}
semoi@darkstar:~/script/el_mio/prog$ cat strip.c
/* FILE NAME:  strip   
 * PROG APPL:  postprocessor for when I post to LQ and write in the
 *   console.
 *
 * Compile with 'gcc -Wall -o strip strip.c'
 *
 * 
 * 1.  Removi la verificacion de EOF en la fputc y sigue andando (Restaurar).
 * 2.  
 *
 * WATCH OUT: there is a strip command already in the OS.
*
 * 
 *                    ANDANDO
 *
 *
 */

/*
 * Based on u2d.c, from Pacific.
 *
 */

#include        <stdio.h>
#include        <stdlib.h>

/*
 *      Unix To DOS text file conversion utility. 
 *
 *      Converts the Unix style end-of-line sequence 0AH
 *      to MS-DOS style end-of-line sequence 0DH 0AH
 */

#define TMPNAME "val"
#define LF 0x0a
#define CR 0x0d
#define BL 0x20

/*
 *      int     fixfile(char * filename)
 *
 *      "Fix" the text file specified by "filename".
 *      Returns 0 on error, non zero otherwise.
 */

int fixfile(char * filename) {
        FILE *          infile;
        FILE *          outfile;
        char            ch;
        unsigned long   in, out;

        printf("u2d: %s: ", filename);
        fflush(stdout);
        infile = fopen(filename, "r");  /* try to open input file */
        if (!infile) {
                printf("not found\n");
                return 0;
        }
        outfile = fopen(TMPNAME, "w");  /* try to open scratch file */
        if (!outfile) {
                fclose(infile);
                printf("unable to open temp file %s\n", TMPNAME);
                return 1;
        }
        in = out = 0;
        while ((ch = fgetc(infile)) != EOF) {   /* while not end of file */
        if (ch == LF) {
            ch= fgetc(infile);
            if(ch == LF){
                fputc(LF, outfile);
                fputc(LF, outfile);
            } else {
                fputc(BL, outfile);
                fputc(ch, outfile);
            }        
        }
        else {
            fputc(ch, outfile);
        }   
        }
        fclose(infile);
        fclose(outfile);
    
        printf("converted: %lu bytes read, %lu bytes written\n", in, out);
        return 0;
}

main(int argc, char ** argv)
{
        int     i;

        for (i = 1; i != argc; i++) {
                if (fixfile(argv[i])) {
                        printf("u2d: aborted!\n");
                        exit(1);
                }
        }
}
semoi@darkstar:~/script/el_mio/prog$
 
  


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] Substituting zeros with dots in 2nd and 3rd field of a text file. iconig Linux - Newbie 9 05-31-2012 09:19 AM
Substituting text 'Edit1' by lines n1 to n2 Nick Edwards Linux - Newbie 1 03-24-2009 12:30 PM
substituting or deleting a few characters in text i.you Linux - Software 3 12-19-2007 02:39 AM
Java: Reading characters from a text file chief_officer Programming 5 03-26-2007 07:04 PM
Convert special characters in text file nyk Linux - Software 1 01-05-2005 03:20 PM

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

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