Replacing last spacebar


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing last spacebar
# 1  
Old 01-05-2013
Replacing last spacebar

I need to use bash to replace the last spacebar character for each line in a file so that e.g.:

Code:
f d e 3 5

becomes

Code:
f d e 3/5

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 01-05-2013 at 01:26 PM.. Reason: code tags, please!
# 2  
Old 01-05-2013
Code:
echo 'f d e 3 5' | sed 's#\(.*\) #\1/#'

# 3  
Old 01-05-2013
Code:
line="f d e 3 5"
echo ${line/% ?/\/}${line: -1}

Note: This will not work for variable length string. So using solution provided by vgersh99 is the best approach.
# 4  
Old 01-05-2013
With awk

Code:
echo "f d e 3 5" | awk '{$0=gensub(/ /,"/",(NF-1))};1'
f d e 3/5

# 5  
Old 01-05-2013
Quote:
Originally Posted by Jotne
With awk

Code:
echo "f d e 3 5" | awk '{$0=gensub(/ /,"/",(NF-1))};1'
f d e 3/5

only with gawk
# 6  
Old 01-05-2013
Hmm, I see that my awk points to gawk
So it will at least work in Ubuntu
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing using sed

hi Guys, I have a rar file which consists of 10 files. each file has a space in its file name. how can i replace all spaces with _ i can replace them using sed but the thing is i need to replace using a script and not command. can anyone help me out??:confused: (2 Replies)
Discussion started by: rajeshb6
2 Replies

2. UNIX for Dummies Questions & Answers

Replacing line with another

Hi all, The problem i need helped with is the following: I need to read the first line in a text file and use that line to replace the third line in another text file. after an hour of searching for different solutions i try here and hope for the best:) (2 Replies)
Discussion started by: khorne
2 Replies

3. Shell Programming and Scripting

Replacing a word

Dear Friends, I am facing some problem with some hundred files. Hence to use these file I have to make a small change in each file. But its not possible to open each file and make changes manually. Hence I need guidance frm you to automate it. Example File name is "Queue_11.txt" reload sec=F... (6 Replies)
Discussion started by: anushree.a
6 Replies

4. Shell Programming and Scripting

Replacing \n,\n with ,

Hi everyone, I've read lots of posts about tr and sed and such, I haven't been able to get out of this one. I have a file, like this: 1 , Jelle 2 , SandraThe only thing it needs to do is delete the enter between the number and the name. So, it needs to replace \n,\n with , ... (7 Replies)
Discussion started by: skerit
7 Replies

5. Shell Programming and Scripting

replacing / to -

Hi, i am running a shell script where i have assigned a variable to the value passed. so my variable is var1="investment/portfolio/run.job" now i want to assign another variable var2 as "investment-portfolio-run.out" how can i do it using awk or something else.... thanks (2 Replies)
Discussion started by: rudoraj
2 Replies

6. Solaris

Disk needs replacing?

A mirror Raid system with two disk (probabaly raid1: Sun solaris Netra 240) the following error occured when I look at it with metastat : d101: Submirror of d100 State: Needs maintenance Invoke: metareplace d100 c1t0d0s5 <new device> is there any advice that it indicates that... (15 Replies)
Discussion started by: xramm
15 Replies

7. UNIX for Dummies Questions & Answers

Replacing the last character

Hi , I need to change the last character of the line: ie: input file: (raj, muthu, mani, Output: (raj, muthu, mani); so i need to change the last comma by the closing braces so help me out in the issue. Thanks in advance. (2 Replies)
Discussion started by: ithirak17
2 Replies

8. UNIX for Dummies Questions & Answers

Replacing $ in variable

hi I have a variable like k=$DESTDIR/$PKG/$VERSION I want to replace each $ in string k with say "XXX". so that k becomes like this "XXXDESTDIR/XXXPKG/XXXVERSION" when I use echo $k | sed -e "s/\$/XXX" it actually passes expanded of variables $DESTDIR, $PKG and $VERSION to sed. ... (10 Replies)
Discussion started by: ashish_uiit
10 Replies

9. Shell Programming and Scripting

help in replacing ??

hi all i have input file like this abc.txt filename.out: <TAB>ABC<TAB>9 <TAB>AKC<TAB>1 filename1.out: <TAB>XYZ<TAB>1 <TAB>XYN<TAB>4 and i am trying to replace \n\t with \t so that output will be like this: filename.out:<TAB>ABC<TAB>9<TAB>AKC<TAB>1... (5 Replies)
Discussion started by: zedex
5 Replies
Login or Register to Ask a Question