Remove last space in a string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove last space in a string?
# 8  
Old 12-02-2010
Code:
perl -pe 's/ (?=[^ ]+$)/|/' file

Code:
sed 's/ \([^ ]*\)$/|\1/' file

Code:
awk '{s=$NF;NF--;$0=$0"|"s}1' file


Last edited by Scrutinizer; 12-02-2010 at 02:11 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 12-02-2010
alternate with sed..
Code:
sed 's/ [^ ]*$/ |&/'

# 10  
Old 12-02-2010
@michael

That will introduce spaces, no?
# 11  
Old 12-02-2010
Quote:
Originally Posted by Scrutinizer
That will introduce spaces, no?
yes Smilie nice catch. Im trying to modify the command..
# 12  
Old 12-08-2010
I tried this and it simply added the pipe at the end of each customer record as follows:

customer.txt file includes:

SHAWN R KEEGAN
shawn r scroggin
Shawn Regan
Shawn Reilly

Script:

PHP Code:
#!/usr/bin/perl

$file '/home/www/misc/customers.txt';

open(FH_TMP,"<","$file");
open(OUT,">>",tmpfile);
while (
my $line = <FH_TMP>)
{
# chomp ($line);
$line =~ s/^(.*)\s+(.*)$/\1|\2/;
print 
OUT $line;
}
close(FH_TMP);
close(OUT); 
Output to tmpfile is:

PHP Code:
SHAWN R KEEGAN|shawn r scroggin|Shawn Regan|Shawn Reilly
Desired output is:

SHAWN R|KEEGAN
shawn r|scroggin
Shawn|Regan
Shawn|Reilly

Quote:
Originally Posted by birei
Hi,

Using 'perl':

Code:
$ perl -lpe 's/^(.*)\s+(.*)$/\1|\2/' infile

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove space with sed

Hello Folks , myfile contains 1000000 records as follows: logver=56 idseq=63256 itime=1111 devid=TG-40 devname=PUI-C2 vd=USER date=2019_01_10 time=18:39:49 logid="000013" type="traffic" subtype="forward" level="notice" eventtime=134 srcip=1.1.1.1 srcport=1 srcintf="XYX-CORE.01"... (3 Replies)
Discussion started by: arm
3 Replies

2. Shell Programming and Scripting

Remove all matches containing a string until its next space

I want to delete all matches in a text following this pattern: Each match starts with linux- Each match ends with a space (Remove linux-* until immediate next space) EXAMPLE: From this text: ibudev1 libudev1 libweather-ion7 libxatracker2 linux-generic linux-headers-generic... (4 Replies)
Discussion started by: Tribe
4 Replies

3. Shell Programming and Scripting

Remove not only the duplicate string but also the keyword of the string in Perl

Hi Perl users, I have another problem with text processing in Perl. I have a file below: Linux Unix Linux Windows SUN MACOS SUN SUN HP-AUX I want the result below: Unix Windows SUN MACOS HP-AUX so the duplicate string will be removed and also the keyword of the string on... (2 Replies)
Discussion started by: askari
2 Replies

4. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

5. Shell Programming and Scripting

remove space

File A.txt A005 -119.5 -119.5 -100.5 A006 -120.5 -119.5 -119.3 A008 0 0 0 Output A005 -119.5 -119.5 -100.5 A006 -120.5 ... (1 Reply)
Discussion started by: asavaliya
1 Replies

6. Shell Programming and Scripting

Remove space before a character

Hi guys, I am new to shell scripting and I have a small problem...If someone can solve this..that would be great I am trying to form a XML by reading a flat file using shell scripting This is my shell script LINE_FILE1=`cat FLEX_FILE1.TXT | head -1 | tail -1` echo... (1 Reply)
Discussion started by: gowrishankar05
1 Replies

7. Shell Programming and Scripting

remove characters from string based on occurrence of a string

Hello Folks.. I need your help .. here the example of my problem..i know its easy..i don't all the commands in unix to do this especiallly sed...here my string.. dwc2_dfg_ajja_dfhhj_vw_dec2_dfgh_dwq desired output is.. dwc2_dfg_ajja_dfhhj it's a simple task with tail... (5 Replies)
Discussion started by: victor369
5 Replies

8. Shell Programming and Scripting

Remove space

DATE=6/Jul/2010 6/Jul/2010 var="sed -n '/\ ---------- Post updated at 11:49 AM ---------- Previous update was at 11:36 AM ---------- #!/bin/bash DATE=`./get_date.pl 3` DATE1=`./get_date.pl 2` var1=$( echo "$DATE" | sed "s/ //g" ) var2=$( echo "$DATE1" | sed "s/ //g" ) var="sed -n... (1 Reply)
Discussion started by: sandy1028
1 Replies

9. UNIX for Dummies Questions & Answers

Space in file how to remove

Hello I have a file with data something like this in it : texttexttext "text .lst" TEXT=" text " texttexttext "moretext .lst" TEXT=" text " Question is how do I get rid of space so that the files looks like this : texttexttext "text.lst" TEXT="text" texttexttext... (8 Replies)
Discussion started by: davebw
8 Replies

10. UNIX for Dummies Questions & Answers

Remove unregconized space from a string

Hi, I need to unload some data from Informix database into text file. But what happen is when i open the text file, some record split into 2 line, become uncomplete. I had checked is due to some unregconized space in particular fields, i used many way to detect this unregconized space, but... (3 Replies)
Discussion started by: Ricole
3 Replies
Login or Register to Ask a Question