Concatenating words without spaces.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenating words without spaces.
# 1  
Old 07-14-2012
Concatenating words without spaces.

Hi All,

I have written a C program to solve this problem but I am eager to know whether the same output can be obtained using sed or awk?

This is the input:
Code:
star
ferry
computer
symbol
prime
time

This is the output:
Code:
starferry
ferrycomputer
computersymbol
symbolprime
primetime


This is my code written in C.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<inttypes.h>

char *chomp ( char * );

int32_t main ( int32_t argc , char ** argv )
{
    FILE *input_file = NULL;
    input_file = fopen ( argv [ 1 ] ,"r" );
    if ( input_file == NULL )
    {
        fprintf ( stderr , "input file open error\n" );
    }

    FILE *output_file = NULL;
    output_file = fopen ( "concatenated_words.dat" , "w" );
    if ( output_file == NULL )
    {
        fprintf ( stderr , "file write error\n" );
    }

    unsigned long int status_flag = 0;

    char *word1 = NULL;
    word1 = ( char * ) malloc ( 20 * sizeof ( char ) );
    if ( word1 == NULL )
    {
        fprintf ( stderr , "malloc() memory allocation failure\n" );
    }

    char *word2 = NULL;
    word2 = ( char * ) malloc ( 20 * sizeof ( char ) );
    if ( word2 == NULL )
    {
        fprintf ( stderr , "malloc() memory allocation failure\n" );
    }

    while ( !feof ( input_file ) )
    {
        if ( status_flag == 0 )
        {
            fscanf ( input_file , "%s\n" , word1 );
        }
        memset ( word2 , 0 , strlen ( word2 ) );
        fscanf ( input_file , "%s\n" , word2 );

        word1 = chomp ( word1 );
        word2 = chomp ( word2 );

        fprintf ( output_file , "%s%s\n" , word1 , word2 );
        status_flag ++;
        memset ( word1 , 0 , strlen ( word1 ) );
        strcpy ( word1 , word2 );
    }

    free ( word1 );
    free ( word2 );
    fclose ( input_file );
    fclose ( output_file );
}
              
char *chomp ( char *word )
{
    int32_t word_length = 0;
    word_length = strlen ( word );
    if ( word [ word_length ] == '\n' )
    {
        word [ word_length ] = '\0';
    }
    return ( word );
}

I am using gcc on Linux.
# 2  
Old 07-14-2012
With sed:

Code:
sed 'N;h;s/\n//;P;g;D' inputfile


Last edited by elixir_sinari; 07-14-2012 at 07:39 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 07-14-2012
Try:
Code:
awk 'p{print p $0}{p=$0}' infile

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 07-14-2012
The following should mimic the intent of that C code (including printing out the only line if there is only a single line in the input).
Code:
sed '1n;$q;p' | paste -d\\0 - -

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 07-14-2012
See how sed and awk can be terse compared to c? Smilie
# 6  
Old 07-14-2012
Code:
awk 'p{print p $0}{p=$0}' infile

please tell me what does "p" in the start refers to ?
# 7  
Old 07-14-2012
Only print if variable p contains something, which is the case on every line except the first line...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

3. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

4. Shell Programming and Scripting

Remove spaces from between words that are in a field

Hi all, Is there a sed/awk cmd that will remove blank space from between words in a particular field, replacing with a single space? Field containing 'E's in the example below: Example input file: AAAAA AA|BBBB|CCCCCCC|DDDDDD |EEEE EEEEEE| FFF FFFFF| ... (6 Replies)
Discussion started by: dendright
6 Replies

5. Shell Programming and Scripting

Grep words with spaces and save the output

I have a file that contains the schedule for a tournament with 41 teams. The team names have spaces in them. I would like to search for each teams schedule and then save that to that teams file For example Team name: "Team Two" I would like to search for all the games for "Team Two" and... (8 Replies)
Discussion started by: knijjar
8 Replies

6. Shell Programming and Scripting

Insert varying length spaces between words

Hey all, Fist post, so be kind... I have written an expect script which logs into a terminal and gathers several screens of information. Unfortunately the log file gives me all the special escape and control characters from the terminal. I am hoping to use a combination of shell scripting, sed,... (1 Reply)
Discussion started by: mpacer
1 Replies

7. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

8. Programming

Counting characters, words, spaces, punctuations, etc.

I am very new to C programming. How could I write a C program that could count the characters, words, spaces, and punctuations in a text file? Any help will be really appreciated. I am doing this as part of my C learning exercise. Thanks, Ajay (4 Replies)
Discussion started by: ajay41aj
4 Replies

9. Programming

Concatenating array of strings into one string separated by spaces

Hi, Well as the title says, I have an array of strings (delimited by null). The length of the array is variable and length of each string is variable as well. What I need is one huge string with the original strings in the array separated by spaces. For example is an array is such that array... (12 Replies)
Discussion started by: newhere
12 Replies

10. Shell Programming and Scripting

Retaining spaces between words

Retaining Spaces within a word -------------------------------------------------------------------------------- Hi Experts, I have a 2 GB flat file which have unicode field, some of them are blanks and its size is 4000 character. In the existing system SED command removes the spaces.... (7 Replies)
Discussion started by: RcR
7 Replies
Login or Register to Ask a Question