position specific replace in file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

position specific replace in file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-21-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
position specific replace in file

How to replace the position specific values in the file..
i searched a lot the forums but i couldn't able to do...
i have file like below


Code:
576666666666666666666666666 7878 897987 121 0asdas Y12
5900fbb 777 09JJJ 78798347892374 234234234364 234232898
89HJHIHIGIUG989902743748327khjkhkjlh jggfsgf9898 90909090

1)In the first line i want to change from 18 position - 25 position to some other value- alpha numeric
2)if the line startd with 5900 then i should change 5 - 17 to some text

Simiar to other lines also ...with different positions

Need help on this
Sponsored Links
    #2  
Old 02-21-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 24 October 2014, 12:11 PM EDT
Location: India
Posts: 1,831
Thanks: 25
Thanked 525 Times in 504 Posts

Code:
sed '1s/\(^.\{17\}\).\{8\}\(.*\)/\1some123text\2/; /^5900/s/\(^.\{4\}\).\{13\}\(.*\)/\1some123text\2/' inputfile

The Following User Says Thank You to balajesuri For This Useful Post:
greenworld123 (02-22-2012)
Sponsored Links
    #3  
Old 02-22-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
It worked thx...
Can you explain me the command

---------- Post updated at 01:55 AM ---------- Previous update was at 01:46 AM ----------

I have one more query in second part of sed


Code:
sed '/^5900/s/\(^.\{4\}\).\{13\}\(.*\)/\1some123text\2/' inputfile

How do i can insert another condition into the above command
Example:
in the same line i have to check wther the 30th position is R or S or V
i mean below is the condition

1)The should starts with 5900
2)The 30 position is R or S or V .. then i have to change to some text
    #4  
Old 02-22-2012
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 17 March 2014, 1:43 AM EDT
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
If your concerned with the starting position then below would replace at 5th and 18th position in a line.

Code:
sed '/^5900/s/./TEXT/5;s/./TEXT2/18' inputfile

Sponsored Links
    #5  
Old 02-22-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 24 October 2014, 12:11 PM EDT
Location: India
Posts: 1,831
Thanks: 25
Thanked 525 Times in 504 Posts

Code:
1s/\(^.\{17\}\).\{8\}\(.*\)/\1some123text\2/

--> '1s' refers to substitution only in the first line. First 17 characters (matched by pattern in green) are stored in buffer, referenced by \1. The next 8 characters (i.e. from char 18 through to 25, pattern in blue) are matched. Rest of the line (pattern in red) is stored in buffer, reference by \2. This whole thing is substituted by "\1some123text\2".



Code:
 /^5900/s/\(^.\{4\}\).\{13\}\(.*\)/\1some123text\2/

--> Pattern in green matches line starting with 5900. In such lines, first 4 characters (pattern in blue) are stored in buffer, referenced by \1. The next 13 characters (i.e. from char 5 through to 17, pattern in red) are matched. Rest of the line (pattern in pink) is stored in buffer, referenced by \2. This whole thing is substituted by "\1some123text\2".
The Following User Says Thank You to balajesuri For This Useful Post:
greenworld123 (02-22-2012)
Sponsored Links
    #6  
Old 02-22-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
Thx for ur explanation...

can i insert another condition inthe same command...

the line should starts with 5900 and 30th position is R or S or V
Sponsored Links
    #7  
Old 02-22-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 24 October 2014, 12:11 PM EDT
Location: India
Posts: 1,831
Thanks: 25
Thanked 525 Times in 504 Posts
Quote:
Originally Posted by greenworld123 View Post
I have one more query in second part of sed


Code:
sed '/^5900/s/\(^.\{4\}\).\{13\}\(.*\)/\1some123text\2/' inputfile

How do i can insert another condition into the above command
Example:
in the same line i have to check wther the 30th position is R or S or V
i mean below is the condition

1)The should starts with 5900
2)The 30 position is R or S or V .. then i have to change to some text
The second part of sed can be extended to this:

Code:
/^5900/s/\(^.\{4\}\).\{13\}\(.\{12\}\)[RVS]\(.*\)/\1some123text\2X\3/

This will substitute characters from position 5-17 with "some123text" and substitute character 30 to "X", if it is R or V or S
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
Awk command to replace specific position characters. pinnacle Shell Programming and Scripting 8 02-02-2012 12:52 PM
Using sed to replace a string in a specific position tinman47 Shell Programming and Scripting 3 09-29-2011 05:35 PM
Sed position specific replace tiggyboo Shell Programming and Scripting 2 01-03-2011 01:47 PM
Using sed to replace specific character and specific position programmer22 Shell Programming and Scripting 5 10-11-2009 07:50 AM
read space filled file and replace text at specific position COD Shell Programming and Scripting 6 04-21-2008 05:40 AM



All times are GMT -4. The time now is 08:36 PM.