LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-16-2014, 07:16 AM   #1
thurst1
LQ Newbie
 
Registered: Jul 2014
Posts: 5

Rep: Reputation: Disabled
Need to replace some text after ":" in c


I have a file with contents like below, need to replace the last set "zzzz" in every line with 0.

I tried using strtok, strstr for this this purpose but not getting desired results.

<file contents>
xxxx:yyyy:zzzz
....:....:....
....:....:....
....:....:....
 
Old 07-16-2014, 07:36 AM   #2
metcommradio
LQ Newbie
 
Registered: Feb 2007
Location: Spain
Distribution: Fedora 22; Archlinux
Posts: 16

Rep: Reputation: 1
Not sure what you mean really but keeping things simple, you might try to read the file (or paste the text content) into your favourite text editor and use the 'Search & Replace' function ... ? I spent ages once trying to achieve a similar issue in a spreadsheet only to later realise that this simple solution was easier.

Sorry if I'm on the wrong track ....
 
Old 07-16-2014, 07:53 AM   #3
turtleli
Member
 
Registered: Aug 2012
Location: UK
Posts: 206

Rep: Reputation: Disabled
Did you try using strrchr?
 
3 members found this post helpful.
Old 07-16-2014, 08:06 AM   #4
thurst1
LQ Newbie
 
Registered: Jul 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by metcommradio View Post
Not sure what you mean really but keeping things simple, you might try to read the file (or paste the text content) into your favourite text editor and use the 'Search & Replace' function ... ? I spent ages once trying to achieve a similar issue in a spreadsheet only to later realise that this simple solution was easier.

Sorry if I'm on the wrong track ....
I am trying this c program.
 
Old 07-16-2014, 08:36 AM   #5
thurst1
LQ Newbie
 
Registered: Jul 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by turtleli View Post
Did you try using strrchr?
No, Can you help on this?
 
1 members found this post helpful.
Old 07-16-2014, 08:39 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
maybe reverse the string (strrev()) then read up to the first: then truncate and replace ?

strrchr() implicitly reverses then searches in one function:
http://linux.die.net/man/3/strrchr

Last edited by schneidz; 07-16-2014 at 08:43 AM.
 
Old 07-16-2014, 09:12 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> No, Can you help on this?

It should be quite easy:

Code:
    ...
    while (fgets (line_buffer, sizeof (line_buffer), input_file)) {
        char *lastcolon= strrchr (line_buffer, ':');
        if (lastcolon) {
            /* do some transformation */
        }
        fputs (line_buffer, output_file);
    }
 
Old 07-17-2014, 08:44 AM   #8
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
When using fgets don't forget to check whether you've read the whole line. Also be sure to not go over the buffer in “do some transformation”.
 
1 members found this post helpful.
Old 07-17-2014, 01:59 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
If every line in your file follows a consistent format, you can perform sscanf() on the input line.

Code:
int ret;

if((ret = sscanf(input_buffer, "%s:%s:%s:")) != 3) {
    printf("Error: did not find correct format, ret=%d on line %s\n", ret, input_buffer);
}
Plus, make a test program, turn off optimization, enable GDB debugging when you compile, save returns and information as much as possible in variables, and use GDB to debug the process line by line.
 
Old 07-18-2014, 09:17 AM   #10
thurst1
LQ Newbie
 
Registered: Jul 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
If every line in your file follows a consistent format, you can perform sscanf() on the input line.

Code:
int ret;

if((ret = sscanf(input_buffer, "%s:%s:%s:")) != 3) {
    printf("Error: did not find correct format, ret=%d on line %s\n", ret, input_buffer);
}
Plus, make a test program, turn off optimization, enable GDB debugging when you compile, save returns and information as much as possible in variables, and use GDB to debug the process line by line.


Hi,
My file has only one line so i am thinking of using fscanf to parse the lines.
i am using fscanf(fp,"%s %d", str,&num). But if my file has something like "abcd 1234". It is not working , how to configure format specifier for this scenario?
 
Old 07-18-2014, 09:25 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> My file has only one line so i am thinking of using fscanf to parse the lines.

One line? Did you count carefully enough?
 
Old 07-18-2014, 09:32 AM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
This example works fine:
Code:
#include <stdio.h>

void main(void)
{
    FILE *fptr;
    int ret;
    char input[32];
    int value;

    if((fptr = fopen("data.txt", "r")) == NULL) {
        printf("Unable to open data.txt file, exiting.\n");
        return;
    }

    if((ret = fscanf(fptr, "%s %d", input, &value)) != 2) {
        printf("fscanf returned %d\n", ret);
    }
    else {
        printf("Scanned string: %s\n", input);
        printf("Scanned number: %d\n", value);
    }
}
Contents of data.txt:
Code:
abcde 012345
Sample output:
Code:
Scanned string: abcde
Scanned number: 12345

Last edited by rtmistler; 07-18-2014 at 09:34 AM.
 
1 members found this post helpful.
Old 07-18-2014, 09:40 AM   #13
thurst1
LQ Newbie
 
Registered: Jul 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
This example works fine:
Code:
#include <stdio.h>

void main(void)
{
    FILE *fptr;
    int ret;
    char input[32];
    int value;

    if((fptr = fopen("data.txt", "r")) == NULL) {
        printf("Unable to open data.txt file, exiting.\n");
        return;
    }

    if((ret = fscanf(fptr, "%s %d", input, &value)) != 2) {
        printf("fscanf returned %d\n", ret);
    }
    else {
        printf("Scanned string: %s\n", input);
        printf("Scanned number: %d\n", value);
    }
}
Contents of data.txt:
Code:
abcde 012345
Sample output:
Code:
Scanned string: abcde
Scanned number: 12345



Can we replace the the number with "0" and save in the same file?
 
Old 07-18-2014, 10:00 AM   #14
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Not sure what you're asking. I realize that I did overlook the fact that scanning "012345" as an integer would result in 12345. This does work if the line of data is "abcd 1234" and gives you "abcd" as a string and 1234 as a number.

Copy that code, compile it, and test it for yourself.

If you're saying that you'd like to write that data out to a file. I'd recommend you write it to a different file, for starters. Because opening a file in append mode versus create-read/write mode have different behaviors insofar as what does or doesn't happen to the existing file contents as well as where the read and write pointers start out at.

But if you just fopen() a different file with "w" then it will always create a new file and start writing at the top of that new file. It will also always overwrite any same named file.

I suggest that you should develop an understanding about reading and writing separately first before you play with modifying in place.
 
Old 07-25-2014, 01:38 PM   #15
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
You might make use of mmap() to map a file to memory which makes editing in place quite easy
 
  


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
unable to replace "ö" to "p" in shell scripting with "sed" meninmech Programming 5 06-22-2012 02:58 PM
Perl: Find variable text between keywords & replace w/ "term_<variable_text>_term" dsayars Programming 6 07-14-2010 11:05 PM
can I replace text with the result of "wc" using sed? BrianK Linux - General 1 04-21-2004 01:15 PM
1. shell script "find and replace" on text 2. java GUI application randomx Programming 4 03-05-2004 01:01 PM

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

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