C program to trim blank spaces

#include <stdio.h>

void copy(char *to, char *from);

int main(void)
{

char str[80];

copy (str,” this ia a test “);

printf(“without spaces:%s”,str);

return 0;

}

void copy(char *to, char *from)

{

const char *p; char *q;

for(p=from,q=to;*p != ‘ ‘; p++)

// space between quotes in the above statement
{

if (*p != ‘ ‘) { *q=*p; q++; }

}

*q = ”;

}

// Download here.

// trim.doc

Tips: This function can be modified to find and replace a char.

See this page for an example Find and Replace

See this page for string replace: string replace

Advertisements

Suppress Error Message in Shell Script

stderr

Redirect the output of stderr (descriptor 2) to /dev/null:

# save this file as testnull
# chmod 755 testnull
ls -garbage 2>/dev/null
# try ./testnull

Diagram below shows a demo

shell_error.JPG

Notice that

1) redirecting to /dev/null from command line has no effect

2) redirecting error from shell script works !!

stdout

[raja@AGRAJA ~] ls 1>file_list.txt
[raja@AGRAJA ~] ls > same_file_list.txt

This is the default option during redirecting output.

i.e “1>” and “>” mean the same.

stdin

[raja@AGRAJA ~] ls 0>empty_file
[raja@AGRAJA ~] ls

The above prints to standard terminal.

Note that “empty_file” will be emptied.

Redirect everything to file

Redirect all console output texts (stdout, stderr, stdlog …) to file

For sh shell 
at command line:
[raja@AGRAJA ~] make > & ! make.log
inside shell script:
illegal junk text as command > junk.spam 2>&1
[or]
illegal junk text as command > & ! junk.spam
For csh shell
Note: The below works on the command line, as well as inside a script
[raja@AGRAJA ~] make > & ! make.log
[raja@AGRAJA ~] ls > & ! files.lst
[raja@AGRAJA ~] illegal junk text as command > & ! junk.spam