![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 08:59 AM |
| to replace a new line character | chan | Shell Programming and Scripting | 2 | 01-11-2008 08: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 03:24 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
|||
|
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 ~ $
|
|
|||
|
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) }
|
|||
| Google The UNIX and Linux Forums |