Replace trailing whitespaces with pipe symbol using perl | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Replace trailing whitespaces with pipe symbol using perl

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-18-2012
kar_333 kar_333 is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 27 December 2013, 5:23 AM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Replace trailing whitespaces with pipe symbol using perl

I want to replace the whitespace with Pipe symbol to do a multiple pattern matching for the whole text "mysqld failed start" and same as for other text messages Below are the messages stored in a file seperate by whitespace


Code:
mysqld failed start 
nfsd mount failed 
rpcbind failed to start

for each text i need to add the pipe symbol so that it will be easier to do mutliple search pattern similar to egrep.

expected o/p


Code:
mysqld failed start|nfsd mount failed|rpcbind failed to start


Last edited by Scrutinizer; 11-18-2012 at 02:06 PM.. Reason: Less information; (mod) code tags
Sponsored Links
    #2  
Old 11-18-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 April 2014, 2:39 AM EDT
Location: San Jose, CA, USA
Posts: 3,484
Thanks: 141
Thanked 1,207 Times in 1,022 Posts
PLEASE USE CODE TAGS! The only trailing space that is visible in your 1st message is a space after the the last start and you haven't replaced it with a | character in the expected output.
Sponsored Links
    #3  
Old 11-18-2012
kar_333 kar_333 is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 27 December 2013, 5:23 AM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry for the confusion i don't want the pipe symbol at the end
    #4  
Old 11-18-2012
msabhi's Avatar
msabhi msabhi is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 2 November 2013, 1:26 AM EDT
Location: In books/UNIX.com
Posts: 177
Thanks: 16
Thanked 61 Times in 60 Posts
@Kar_333
Your request incomprehensible...Do you understand "trailing whitespaces"? Please use code tags and place your request again
Sponsored Links
    #5  
Old 11-18-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 April 2014, 2:39 AM EDT
Location: San Jose, CA, USA
Posts: 3,484
Thanks: 141
Thanked 1,207 Times in 1,022 Posts
What you have shown us is still a single line of input and a single line of output with the 3rd and 6th spaces (which do not appear to be trailing spaces) changed to vertical bars. I still don't understand how to translate your example into a meaningful script.

Please edit your original post and insert appropriate CODE tags so we can see what you're trying to do.
Sponsored Links
    #6  
Old 11-19-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Gotham Knight
 
Join Date: Mar 2012
Last Activity: 18 April 2014, 12:09 AM EDT
Location: India
Posts: 1,412
Thanks: 100
Thanked 495 Times in 472 Posts
Like this?
with bash/ksh93:

Code:
paste -sd'|' <(sed 's/[ \t]*$//' infile)

or with other shells not supporting process substitution:

Code:
sed 's/[ \t]*$//' infile|paste -sd'|' -

And with awk (with slight changes to RudiC's solution):

Code:
awk '{sub(/[ \t]*$/,"");printf("%s%s",(NR>1?"|":""),$0)}END{printf "\n"}' infile


Last edited by elixir_sinari; 11-19-2012 at 04:21 AM..
Sponsored Links
    #7  
Old 11-19-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 23 April 2014, 4:36 AM EDT
Location: Aachen, Germany
Posts: 3,534
Thanks: 51
Thanked 823 Times in 785 Posts
With scrutinizer's edits of the original request and translating/guessing "whitespace" being <newline> instead of "space|tab|newline", I'd propose:
Code:
$ awk '{printf "%s%s", (NR>1)?"|":"", $0}' file
mysqld failed start |nfsd mount failed |rpcbind failed to start

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace pipe with Broken Pipe saj Shell Programming and Scripting 1 04-08-2011 09:18 AM
PERL - Compare 2 strings, excluding whitespaces bojomojo Shell Programming and Scripting 2 03-29-2011 03:57 AM
replace nulls with whitespaces in a file meeragiri Shell Programming and Scripting 2 07-14-2009 03:15 AM
Problem while concating PIPE symbol with a file pssandeep Shell Programming and Scripting 0 03-09-2009 08:33 PM
remove verticalbar or pipe symbol bogu0001 Shell Programming and Scripting 9 01-30-2009 08:24 AM



All times are GMT -4. The time now is 08:08 AM.