Replace nth position character of all the lines in file | Unix Linux Forums | Emergency UNIX and Linux Support

  Go Back    


Emergency UNIX and Linux Support Please post your urgent questions here for highest visibility. Posting a new thread to this forum requires Bits. We monitor this forum to help people with emergencies, but we do not guarantee response time or answers. This forum is "best effort" only. Members who reply to posts here receive a bonus of 1000 Bits per reply.

Replace nth position character of all the lines in file

Emergency UNIX and Linux Support


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-24-2012
greenworld123 greenworld123 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 30 May 2014, 6:42 AM EDT
Posts: 58
Thanks: 15
Thanked 0 Times in 0 Posts
Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk...
searched the forums but didn't find exact answer
Sponsored Links
    #2  
Old 07-24-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 16 July 2014, 11:41 PM EDT
Posts: 3,057
Thanks: 33
Thanked 657 Times in 636 Posts
replace the 150th character as X

Code:
awk '$0=substr($0,1,149)"X"substr($0,151,length($0))' input.txt > output.txt

---------- Post updated at 12:10 PM ---------- Previous update was at 12:07 PM ----------

if you have the lines with less than 150 characters, and dont want to append the X in the end, then use the below


Code:
 
awk 'length($0)>150{$0=substr($0,1,149)"X"substr($0,151,length($0))}1' input.txt

use nawk in solaris
Sponsored Links
    #3  
Old 07-24-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Gotham Knight
 
Join Date: Mar 2012
Last Activity: 16 July 2014, 3:22 PM EDT
Location: India
Posts: 1,412
Thanks: 100
Thanked 495 Times in 472 Posts
With sed:

Code:
sed 's/^\(.\{149\}\)./\1X/' inputfile

The Following User Says Thank You to elixir_sinari For This Useful Post:
greenworld123 (07-24-2012)
    #4  
Old 07-26-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 21 July 2014, 11:56 PM EDT
Location: India
Posts: 1,816
Thanks: 24
Thanked 519 Times in 498 Posts
Shorter:

Code:
sed 's/./X/149' input

Sponsored Links
    #5  
Old 07-26-2012
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 973 Times in 789 Posts
Quote:
Originally Posted by balajesuri View Post
Shorter:

Code:
sed 's/./X/149' input

To replace the 150th character, that should be 150, not 149.

Regards,
Alister
Sponsored Links
    #6  
Old 07-26-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 21 July 2014, 11:56 PM EDT
Location: India
Posts: 1,816
Thanks: 24
Thanked 519 Times in 498 Posts
Ah, my bad. You're right alister.
Sponsored Links
    #7  
Old 08-01-2012
pitagi pitagi is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 1 August 2012, 6:39 PM EDT
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
On the same lines how do I replace nth column in the file ?

for example


Code:
assgin  crclk  unit1 unit2
assign tap  signal_a

Want to put = after 2nd column


Code:
assign crclk = unit1 unit2
assgin tap = signal_a


Last edited by methyl; 08-01-2012 at 06:09 PM.. Reason: Please use code tags. Space characters are so important when editing.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace character in certain position in a string snipaa Shell Programming and Scripting 3 04-25-2011 08:17 PM
How to replace character on defined position necroman08 UNIX for Dummies Questions & Answers 2 07-17-2009 02:15 AM
Print lines with specific character at nth position in a file manaswinig Shell Programming and Scripting 1 06-04-2009 09:01 AM
Print lines with specific character at nth position in a file manaswinig Shell Programming and Scripting 2 06-04-2009 09:00 AM
Replace a perticular character of all lines of a file abovais Shell Programming and Scripting 1 12-11-2007 04:35 AM



All times are GMT -4. The time now is 04:29 PM.