Search and replace string - PERL

These tools are for Replacing or removing characters or strings from a text.

Using SED
Remove something from the end of the line ("%")

sed 's/%$//' file > newfile

OR

Remove newline

sed ':a;N;$!ba;s/\n//g' x.txt
cat x.txt | sed ':a;N;$!ba;s/\n//g' 

Replace a character (\n)

sed ':a;N;$!ba;s/\n/ /g' x.txt
cat x.txt | sed ':a;N;$!ba;s/\n/ /g' 

Using TR
Remove newline

tr -d '\n' < x.txt
cat x.txt | tr -d '\n'

Replace a character (\n)

tr '\n' ' ' < x.txt
cat x.txt | tr '\n' ' '

Using AWK
Remove newline character

awk '{ printf "%s", $0 }' x.txt
cat x.txt | awk '{ printf "%s", $0 }'

Replace a character (newline)

awk '{ printf "%s ", $0 }' x.txt
cat x.txt | awk '{ printf "%s ", $0 }' 

Using Perl
Remove newline

perl -e 'while (<>) { chomp; print; }; exit;' x.txt
cat x.txt | perl -e 'while (<>) { chomp; print; }; exit;' 

Replace a character (\n)

perl -e 'while (<>) { chomp; print; print " "; }; exit;' x.txt
cat x.txt | perl -e 'while (<>) { chomp; print; print " "; }; exit;' 

perl -p -e 's/\s+$/ /g' x.txt
cat x.txt | perl -p -e 's/\s+$/ /g'

Using C, C++
Remove newline

#include <stdio.h>
 
int main(int argc, char *argv[]) {
    char k;
    while((k = getchar()) != EOF)
        if(k != 0x0D && k != 0x0A) putchar(k);
//        else putchar(' '); // replace newlines with spaces
    putchar(0x0A);
    return 0;
}

Using XARGS
Remove newline

xargs echo < x.txt
cat x.txt | xargs echo
cat x.txt | xargs echo -n

Source: http://linux.dsplabs.com.au/rmnl-remove-new-line-characters-tr-awk-perl-sed-c-cpp-bash-python-xargs-ghc-ghci-haskell-sam-ssam-p65/