Define word by word reading

You may not realize it, but we all read very often in our daily life. We always want to go through all the documents faster at work; we just want to find out the main point of all the long letters and notes from the government or any kinds of organizations quickly; even when we read for leisure, we’d probably be traveling on the bus and just want to finish the current chapter as soon as possible. Yes I get it, you want to read faster without missing the gems.

But is speeding up your word-by-word reading what you should do? The answer is definitely no.

Reading word by word slows you down from processing the idea.

When we read, our eyes normally stop on each word. We call this fixation. It is a bad idea to stop at every word in the text because it slows down the reading speed and may even affect our ability to understand the text.[1]

Language would not have worked without a context. It is true that every word has its own literal meaning but what makes it alive is the context of the text. With the same word but in different contexts, it expresses different contextual meanings, revealing different meanings behind the word. Instead of reading every single word, understanding the context is more important. By having the context in mind, you know what kinds of words you should pay attention to more.

Try to read phrase by phrase instead.

English readers can read roughly two or three words at a time, so instead of stopping at every word, you can stop at every three words. Ideas are not made up of a single word. Being able to read a text phrase by phrase instead of word by word even helps you to understand the idea better.

Skim for the keywords only.

Words play different roles in a sentence. Some are more meaningful while some are less. When our eyes do not stop on each word anymore, we can try skimming to absorb the more meaningful words and ignore those which are less meaningful. What makes a sentence complete is a subject and a verb while all the other elements are only complementing the sentence. For most of the time, you will not have any difficulties in understanding the text despite absorbing the keywords only.

Remember, ideas are bigger than words.

Ideas are made up of words. When you stop reading word by word and focus more on the idea you’re trying to understand, you will read faster. While speeding up reading can increase your productivity at work, it allows you to enjoy reading more!

⌄ Scroll down to continue reading article ⌄

⌄ Scroll down to continue reading article ⌄

My problem is to read a text file word by word and recovers every word in a variable. I tried to do:

while read ligne; 
do {
for ( i=1; i<= length ($ligne); i++); do
 { var=$(awk '{print $i}' test.txt)}

}done < test.txt

But it doesn’t work and I have this error:

Couldn't parse this for loop

Anthon's user avatar

Anthon

77.5k42 gold badges164 silver badges221 bronze badges

asked Apr 9, 2015 at 8:53

Aomine Daiki's user avatar

1

It depends on how you define words.

If words are separated by one or more spaces, you can do:

tr -s '[:blank:]' '[n*]' < file |
  while IFS= read -r word; do
    : echo "$word" here
  done

If words are sequences of characters contains A-Z, a-z and _:

tr -cs 'A-Za-z_' '[n*]' < file | ...

On historical System V systems, you need to use square brackets [A-Za-z_].

answered Apr 9, 2015 at 9:20

cuonglm's user avatar

cuonglmcuonglm

149k38 gold badges321 silver badges401 bronze badges

4

Just

while read -ra line; 
do
    for word in "${line[@]}";
    do
        echo "$word";
    done;
done < test.txt

will split up the file word by word. Change the echo to whatever you want to do with the words.

semicolons are added so this can be put into a one-liner.

Stéphane Chazelas's user avatar

answered Apr 9, 2015 at 9:20

Lambert's user avatar

LambertLambert

12.3k2 gold badges25 silver badges34 bronze badges

6

You will receive list of «words» which was been separated by spaces but with punctuation marks enclosed:

while read -a tmp_var; 
do
    for i in "${tmp_var[@]}"
    do
        var[${#var[*]}=$i
    done
done < test.txt

But, as usual, test.txt has been transformed by tr or sed or etc. in «1 word in line» list by tr or sed or etc. and read line by line.

answered Apr 9, 2015 at 9:01

Costas's user avatar

CostasCostas

14.7k20 silver badges35 bronze badges

4

A simpler way, using whitespace as separator and all else as a word,

set -o noglob
words=($(cat text_file)) # use split+glob operator with glob disabled
                         # above. Splits on space tab and newline with
                         # the default value of $IFS.

If that words contain punctuactions and punctuations are words don’t cause much trouble you could try this way.

Stéphane Chazelas's user avatar

answered Apr 9, 2015 at 17:50

xae's user avatar

xaexae

1,94115 silver badges9 bronze badges

read -a WORDS -d "" < file.txt
for word in "${WORDS[@]}"
do
        echo $word
done

Option -a stores the words in an array.

Option -d specifies the delim string. This tells read where to stop processing the file. I’ve specified an empty string which causes read to continue until it gets an EOF. That is, the whole file is processed regardless of line endings.

answered Apr 6, 2017 at 4:45

mos fetish's user avatar

Considering that you used awk in your question, here’s another alternative to tr and fmt:

awk '{ for ( i = 1; i < NF; ++i ) print $(i); }' test.txt |
while IFS= read -r var 
do 
    echo processing: "$var" 
done

Note that, as with fmt and unlike tr, awk accepts input filenames as arguments.

answered Apr 9, 2015 at 10:52

JdeBP's user avatar

JdeBPJdeBP

65.3k12 gold badges158 silver badges335 bronze badges

I have written the following sample script which reads file /etc/passwd word by word.

#!/bin/bash

COUNT=1

FCOUNT=`cat /etc/passwd|wc -l`

while [ $COUNT -le $FCOUNT ]
do
    FTCOUNT=`awk -F ":" '{print NF,$0}' /etc/passwd|awk '{print $1}'|head -$COUNT|tail -1`
    TCOUNT=1
    while [ $TCOUNT -le $FTCOUNT ]
    do
            if [ $TCOUNT -gt $FTCOUNT ]
            then
                    FTCOUNT=""
                    break
            else
                    OUTPUT=`head -$COUNT /etc/passwd|cut -d ":" -f $TCOUNT|tail -1`
                    echo -n "${OUTPUT} "
                    sleep 2
                    TCOUNT=$(( TCOUNT + 1 ))
            fi
    done
    echo ""
    COUNT=$(( COUNT + 1 ))
done

Anthon's user avatar

Anthon

77.5k42 gold badges164 silver badges221 bronze badges

answered Mar 7, 2016 at 17:09

Santosh Ware's user avatar

In this article, we are going to learn How to Read text File word by word in C. We will read each word from the text file in each iteration.

C fscanf Function


The fscanf() function is available in the C library. This function is used to read formatted input from a stream. The syntax of the fscanf function is:

Syntax

int fscanf(FILE *stream, const char *format, ...)

Parameters :

  • stream − This is the pointer to a FILE object that identifies the stream.
  • format − This is the C string that contains one or more of the following items − Whitespace character, Non-whitespace character, and Format specifiers.
  • A format specifier will be as [=%[*][width][modifiers]type=].

1. Read File Word by Word in C using fscanf Function


Here we are making use of the fscanf function to read the text file. The first thing we are going to do is open the file in reading mode. So using fopen() function and “r” read mode we opened the file. The next step is to find the file stats like what is the size of the data this file contains. so we can allocate exact memory for the buffer that is going to hold the content of this file. We are using the stat() function to find the file size.

  • Once we have the size and buffer allocated for this size, we start reading the file by using the fscanf() function.
  • We keep reading the file word by word until we reach the end of file.In fscanf function, we are passing “%39[^-n] as the argument so we can read the text until we find the next word.
  • The code will look like this:
fscanf(in_file, "%39[^-n]", file_contents)

C Program to Read text File word by word


To run this program, we need one text file with the name Readme.txt in the same folder where we have our code.The content of the file is:

Hello My name is 
John 
danny
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

const char* filename = "Readme.txt";

int main(int argc, char *argv[])
{
    FILE *in_file = fopen(filename, "r");
    if (!in_file) 
	{
        perror("fopen");
        return 0;
    }

    struct stat sb;
    if (stat(filename, &sb) == -1) 
	{
        perror("stat");
        return 0;
    }

    char *file_contents = malloc(sb.st_size);

    while (fscanf(in_file, "%[^-n ] ", file_contents) != EOF) {
      printf("> %sn", file_contents);
	  }
    
	

    fclose(in_file);
    return 0;
}

Output

Hello
My
name
is
John
danny

You’re never assigning the return value of malloc() to quote[i] so they end up staying NULL (if you’re lucky):

  char **quote = malloc(sizeof(char*) * (size_t)SIZE_QUOTE);
  long i;
  for(i = 0; i < SIZE_QUOTE; i++){
    if(!(malloc(sizeof(char) * WORD_LENGTH)))

It should be something like this instead:

  char **quote = malloc(sizeof(char*) * (size_t)SIZE_QUOTE);
  for(int i = 0; i < SIZE_QUOTE; i++){
    quote[i] = malloc(sizeof(char) * WORD_LENGTH);
    if(!quote[i])

Also you could avoid malloc() entirely and statically initialize that array if all the sizes are known:

char quote[SIZE_QUOTE][WORD_LENGTH] = {{''}};

Also, you should be free()-ing the individual quote[i] at the end too:

for(int i = 0; i < SIZE_QUOTE; ++i) free(quote[i]);
free(quote);

There’s other mistakes that have been pointed out through the comments already, so I won’t elaborate further.

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    Like Article

    Prerequisites: File Handling in Python Given a text file and the task is to read the information from file word by word in Python. Examples:

    Input: I am R2J! Output: I am R2J! Input: Geeks 4 Geeks And in that dream, we were flying. Output: Geeks 4 Geeks And in that dream, we were flying.

    Approach:

    1. Open a file in read mode which contains a string.
    2. Use for loop to read each line from the text file.
    3. Again use for loop to read each word from the line splitted by ‘ ‘.
    4. Display each word from each line in the text file.

    Example 1: Let’s suppose the text file looks like this – Text File: read-word-by-word-python 

    Python3

    with open('GFG.txt','r') as file:

        for line in file:

            for word in line.split():

                print(word)

    Output:

    Geeks
    4
    geeks

    Time Complexity: O(n), where n is the total number of words in the file.
    Auxiliary Space: O(1), as the program is reading and displaying each word one by one without storing any additional data.

    Example 2: Let’s suppose the text file contains more than one line. Text file: python-read-word-by-word 

    Python3

    with open('GFG.txt','r') as file:

        for line in file:

            for word in line.split():

                print(word)

    Output:

    Geeks
    4
    Geeks
    And
    in
    that
    dream,
    we
    were
    flying.

    Time complexity: O(n), where n is the total number of words in the file.
    Auxiliary space: O(1), as only a constant amount of extra space is used to store each word temporarily while printing it.

    Like Article

    Save Article

    Понравилась статья? Поделить с друзьями:
  • Define word and sentence
  • Define what word processing is
  • Define what part of speech the underlined word is
  • Define what a root word is
  • Define the word worse