The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replace > with new line character borncrazy Shell Programming and Scripting 4 07-14-2008 10:34 PM
Replace certain character with a new a new line. djsal Shell Programming and Scripting 8 01-11-2008 09:59 AM
to replace a new line character chan Shell Programming and Scripting 2 01-11-2008 09:56 AM
Replace a character in last line Mohammed Shell Programming and Scripting 6 08-01-2007 11:46 AM
sed - Replace Line which contains the Pattern match with a new line kousikan Shell Programming and Scripting 2 03-24-2007 04:24 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 08-08-2008
Registered User
 

Join Date: Apr 2007
Posts: 44
match a character in a line and replace

Hi,

I have a file with large number of records. Sample below:

123456789QWERT2U 2 erter
987123678ZXCVB6Y 5 7689
934567123GHJKUI4O 7
-
--
--

I want the 16th character in each record to be replaced with the below as follows;so 2 will become K, 6 will become O and 4 will become M.
0 }
1 J
2 K
3 L
4 M
5 N
6 O
7 P
8 Q
9 R

So, the records should look like:


123456789QWERTKU
987123678ZXCVBOY
934567123GHJKUIMO

---

How can i do that sort of patterns matching and replacement.

Thanks.
Reply With Quote
Forum Sponsor
  #2  
Old 08-08-2008
Ikon's Avatar
Registered User
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 350
What have you tried so far?
Reply With Quote
  #3  
Old 08-10-2008
Registered User
 

Join Date: Aug 2008
Location: Portugal
Posts: 212
One of your examples is different of the others.

934567123GHJKUI4O 7 -> "4" is the 16th char.
987123678ZXCVB6Y 5 7689 -> "6" is the 15th char.

I made it replace the 15th char, but if you want to replace the 16th just change substr's param from 14 to 15.

Code:
redoubtable@Tsunami ~ $ echo -e "987123678ZXCVB6Y 5 7689\n934567123GHJKUI4O 7\n123456789QWERT2U 2 erter"|perl -ne '@a = ("}","J","K","L","M","N","O","P","Q","R"); substr($_, 14, 1, $a[substr($_, 14, 1)]); print'
987123678ZXCVBOY 5 7689
934567123GHJKU}4O 7
123456789QWERTKU 2 erter
redoubtable@Tsunami ~ $
Reply With Quote
  #4  
Old 08-10-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,046
Assuming it is the 16th character you want to replace, here is one way of doing it using awk i.e. awk -f awkscriptfile file

Code:
BEGIN { split("} J K L M N O P Q R", lookup) }
{ print substr($0,1,15) lookup[substr($0,16,1) + 1] substr($0,17) }
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0