Sponsored Content
Full Discussion: Replacing words in a file
Top Forums Programming Replacing words in a file Post 302070783 by matrixmadhan on Friday 7th of April 2006 11:27:07 AM
Old 04-07-2006
Welcome to the Forum !!! Smilie Smilie Smilie
Quote:
I'm trying to write a program that will open an existing file supplied by the command line argument and then replace words with "We" or "we" by "I" and "a" or "A" by "The". When I run the program it reads the file, changes the word but re writes it on a new line with only the replaced words not the whole sentence. Would anyone be able to help me? I also have to use open and read not fopen for example.
Yes it would rewrite on the newline and that too with only the replaced words not the whole sentence.
In your code where have you, instructed to write the whole sentence? Only the replacement is effected as follows
Quote:
write(in_fd, "I", strlen("I"));
try the following code,
not well tested, can be optimized, would server only as an example or starter;modify input/output file accordingly

Code:
# include<stdio.h>

int main()
{
 FILE *fpr, *fpw;
 int ch1, ch2, ch3;

 if( ((fpr=fopen("src", "r")) == NULL) || ((fpw=fopen("dest", "w")) == NULL ))
 {
    exit(1);
 }
  while( (ch1=fgetc(fpr)) != EOF )
  {
     if( (ch2=fgetc(fpr)) == EOF )
     {
        break;
     }
     if( (ch3=fgetc(fpr)) == EOF )
     {
        break;
     }
     if( (ch1=='W' && ch2=='e' && ch3==' ') || (ch1=='w' && ch2=='e' && ch3==' ') )
     {
        fprintf(fpw, "%s", "I ");
        continue;
     }
   else if( (ch1=='a' && ch2==' ') || (ch1=='A' && ch2==' ') )
     {
        fprintf(fpw, "%s", "The ");
        ungetc(ch3, fpr);
        continue;
     }
     else
     {
        fprintf(fpw, "%c",ch1);
        ungetc(ch3, fpr);
        ungetc(ch2, fpr);
     }
  }
  if(ch1 != EOF)
     fprintf(fpw, "%c",ch1);
  if(ch2 != EOF)
     fprintf(fpw, "%c",ch2);
  if(ch3 != EOF)
     fprintf(fpw, "%c",ch3);
  fclose(fpr);
  fclose(fpw);
}

>cat src
we source of power
We ultimate source of power
of all that a possible
neutral of all A neurons

>cat dest
I source of power
I ultimate source of power
of all that The possible
neutral of all The neurons
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed replacing words with abbreviations

I really hate to do this, but I am completely stumped. I have to create a sed script that will change the abbreviations in a file to the full word. I really just have no idea where to start. All I want is a starting point as well no actual complete answer. Thank you for your time in advance. ... (4 Replies)
Discussion started by: mauler123
4 Replies

2. Shell Programming and Scripting

Replacing words in a fast way

Hi, I have a file that looks like this: br0 br0 br1 br10 br11 br12 br13 br14 br15 br15 br2 br2 br3 br4 br5 br6 br7 (5 Replies)
Discussion started by: phil_heath
5 Replies

3. Shell Programming and Scripting

Replacing words

Hi, I have am using a file that contains names that I want to replace. Basically file 1 looks like this jack joe james john I have another file (file 2) that looks like this jack 2345 joe 6848 james 3342 john 3432 Basically I want to replace column1 from file1... (4 Replies)
Discussion started by: kylle345
4 Replies

4. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

5. Shell Programming and Scripting

Replacing words in file

Hello All Probably this is very simple for you but I cant figure it out I have to replace "No Header" with "Output Field Names" I/P file <ATTRIBUTE NAME ="Header Options" VALUE ="No Header"/> O/P needed <ATTRIBUTE NAME ="Header Options" VALUE = "Output Field Names"> (4 Replies)
Discussion started by: Pratik4891
4 Replies

6. Shell Programming and Scripting

Splitting concatenated words in input file with words from the same file

Dear all, I am working with names and I have a large file of names in which some words are written together (upto 4 or 5) and their corresponding single forms are also present in the word-list. An example would make this clear annamarie mariechristine johnsmith johnjoseph smith john smith... (8 Replies)
Discussion started by: gimley
8 Replies

7. UNIX for Dummies Questions & Answers

Replacing word and Capitalize words after

I have an assignment and I am not sure what to do. In Unix, I use PuTTY change the semicolon (;) to a period, and capitalize the first letter of the word immediately after it. I know change command is M-% and "." so only one semicolon is changed but I am not sure how to... (1 Reply)
Discussion started by: kathrut43
1 Replies

8. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

9. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

10. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies
spell(1)						      General Commands Manual							  spell(1)

NAME
spell, hashmake, spellin, hashcheck - find spelling errors SYNOPSIS
local_file] [files] n spelling_list DESCRIPTION
The command collects words from the named files and looks them up in a spelling list. Words that neither occur among nor are derivable (by applying certain inflections, prefixes, and/or suffixes) from words in the spelling list are printed on the standard output. If no files are named, words are collected from the standard input. The command ignores most and eqn constructions. Options The command recognizes the following options: All words not literally in the spelling list are printed, and plausible derivations from the words in the spelling list are indicated. British spelling is checked. Besides preferring etc., this option insists upon in certain words, such as in Every plausible stem is printed with for each word. By default, follows chains of included files much like (see deroff(1)) which recognizes the intrinsics and the names of such included files begin with If the option is used, follows the chains of included files. With the option, ignores all chains of included files. If the option is used, words found in local_file are removed from output. local_file is the name of a user-provided file containing a sorted list of words, one per line. With this option, the user can specify a set of words that are correct spellings (in addition to spell's own spelling list) for each job. The spelling list is based on many sources, and while more haphazard than an ordinary dictionary, is also more effective with respect to proper names and popular technical words. Coverage of the specialized vocabularies of biology, medicine, and chemistry is light. Pertinent auxiliary files can be specified by name arguments, indicated below with their default settings (see FILES and VARIABLES). Copies of all output are accumulated in the history file. The stop list filters out misspellings (such as that would otherwise pass. Three routines help maintain and check the hash lists used by Reads a list of words from the standard input and writes the corresponding nine-digit hash code on the standard output. This program only accepts words that are up to 30 characters long. When words exceeding 30 characters are encountered, a diagnostic message is displayed on stan- dard error. Reads n hash codes from the standard input and writes a compressed spelling list on the standard output. Information about the hash coding is printed on standard error. Reads a compressed spelling_list and recreates the nine-digit hash codes for all the words in it; it writes these codes on the standard output. EXTERNAL INFLUENCES
Environment Variables Your hashed spelling list (default is Spelling history (default is Your hashed stop list (default is Directory for temporary files; overrides the default EXAMPLES
To check spelling of a single word: If word is spelled correctly, a prompt is returned. If it is spelled incorrectly, word is printed before the prompt is returned. To check spelling of multiple words, they can also be typed as a group on the same command line: To create a personal spelling list that incorporates the words already present in the default American spelling list file To modify the default British spelling list file replace all occurrences of with in the above example. To add words to the default spelling list, change login to change the current working directory to and execute the commands listed in the above example. WARNINGS
The spelling list's coverage is uneven. When undertaking the use of as a new tool, it may be advisable to monitor the output for several months to gather local additions. Typically, these are kept in a separate local file that is added to the hashed spelling_list via as shown above. The British spelling feature was developed by an American. Start-up versions of files and are available in directory If these files or a suitable equivalent are not present in directory complains: The command is likely to be withdrawn from X/Open standards. Applications using this command might not be portable to other vendors' sys- tems. FILES
Hashed spelling lists, American and British. Hashed stop list. History file. Executable program file. SEE ALSO
deroff(1), sed(1), sort(1), tbl(1), tee(1). STANDARDS CONFORMANCE
spell(1)
All times are GMT -4. The time now is 08:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy