Remove last space in a string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove last space in a string?
# 1  
Old 12-01-2010
Remove last space in a string?

I have a customer file now and would like to separate the names into two cells in a spreadsheet. Here is my data as an example:

SHAWN R KEEGAN
shawn r scroggin
Shawn Regan
Shawn Reilly

The first two have the middle initial so I'd like to include them in the "first name" field and the last name in the "last name" field. So what I need is a script to suck in the names in a text file and replace the last space in each line with a pipe so my data would look like this when converted:

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

Best option please? I've searched the forum here and not found a way to do this specific example.
# 2  
Old 12-01-2010
Easy, the bourne shell read builtin splits on IFS (which is by default whitespace) and trims. [edit] fixed to arrange middle initials properly.

Code:
#!/bin/sh

while read A B C
do
        if [ ! -z "${C} ] 
        then
                echo "${A} ${B}|${C}"
        else
                echo "${A}|${B}"
        fi
        C=""
done < input > output

# 3  
Old 12-01-2010
Sorry, I should have specified but I only really know Perl. Can you put it in layman's terms using Perl? Smilie
# 4  
Old 12-01-2010
Hi,

Using 'perl':

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

Regards,
Birei
# 5  
Old 12-01-2010
Quote:
Originally Posted by Grassy
Sorry, I should have specified but I only really know Perl.
Consider this your introduction to a scripting language that doesn't resemble line noise, then. Smilie
# 6  
Old 12-01-2010
For the record Smilie
Using sed:
Code:
tukuyomi@debian:~/unix.com$ cat name
SHAWN R KEEGAN
shawn r scroggin
Shawn Regan
Shawn Reilly
tukuyomi@debian:~/unix.com$ sed -r 's/(.*) (.*)$/\1|\2/' name
SHAWN R|KEEGAN
shawn r|scroggin
Shawn|Regan
Shawn|Reilly

# 7  
Old 12-01-2010
Code:
awk '$NF="|"$NF' infile

This User Gave Thanks to rdcwayx For This Post:
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