The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Korn: How to loop through a string character by character shew01 Shell Programming and Scripting 10 12-02-2008 07:58 AM
Sed-Special character replacement usshell Shell Programming and Scripting 3 05-22-2008 11:06 AM
KSH - Character Replacement mixxamike Shell Programming and Scripting 2 09-24-2007 04:43 PM
Character replacement piooooter Shell Programming and Scripting 2 09-06-2007 03:48 AM
Korn Shell Help Needed stevefox Shell Programming and Scripting 5 12-05-2005 10:17 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-29-2007
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
Question Help needed in character replacement in Korn Shell

How do I replace a space " " character at a particular position in a line?

e.g. I have a file below


Code:
$ cat i2
111 002 A a
33 0011 B c
2222 003 C a


I want all the 1st spaces to be replaced with forward slash "/" and the 3rd spaces to have 5 spaces to get the output below:



Code:
111/002 AAA     a
33/0011 BBB     c
2222/003 CCC     b

I created a shell which would achieve this but I'm looking for a easier way.


Code:
$ cat test.ksh
#!/bin/ksh

cut -d' ' -f1 < $1 > 1st_word
cut -d' ' -f3 < $1 > 3rd_word

while read line
do

sed -e "s/$line /$line\//g" $1 | grep $line

done < 1st_word > tmp_ouput


while read line
do

sed -e "s/$line /$line     /g" tmp_ouput1 | grep $line

done < 3rd_word


Any help will be appreciated

Steve
  #2 (permalink)  
Old 03-29-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398

Code:
$ cat file
111 002 A a
33 0011 B c
2222 003 C a
$ sed -e "s; ;/;1" -e "s/ /     /2" file
111/002 A     a
33/0011 B     c
2222/003 C     a

  #3 (permalink)  
Old 03-29-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 610
Try this..


Code:
sed -e 's/ /\//' -e 's/\(.* .*\)\( .*\)/\1    \2/' filename

Output:



Code:
cat temp
111 002 A a
33 0011 B c
2222 003 C a
sed -e 's/ /\//' -e 's/\(.* .*\)\( .*\)/\1    \2/' temp
111/002 A     a
33/0011 B     c
2222/003 C     a

  #4 (permalink)  
Old 03-29-2007
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Depending on your version of sed something like this may work for you:

Code:
$ sed -e 's! !/!1' -e 's/ /     /2' infile > outfile

If that doesn't work, the "longhand" version will....

Code:
$ sed -e 's/^\([^ ]*\) \([^ ]*\) \([^ ]*\) \(.*\)$/\1\/\2 \3     \4/' infile > outfile

Cheers
ZB
  #5 (permalink)  
Old 03-29-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,958

Code:
sed 's/ /\//;s/ \(.\)$/    \1/' file

  #6 (permalink)  
Old 03-29-2007
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
Smile

That's exactly what I wanted!

Thank you all for your quick response!!!

Cheers
Steve
  #7 (permalink)  
Old 03-29-2007
heru_90's Avatar
heru_90 heru_90 is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 65

Code:
sed 's/ /\//;s/ \(.\)$/    \1/' file

Hi matrixmadhan, can you explain what mean your code?

Thank you


regards,

heru
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0