position specific replace in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting position specific replace in file
# 1  
Old 02-21-2012
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
# 2  
Old 02-21-2012
Code:
sed '1s/\(^.\{17\}\).\{8\}\(.*\)/\1some123text\2/; /^5900/s/\(^.\{4\}\).\{13\}\(.*\)/\1some123text\2/' inputfile

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 02-22-2012
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
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

# 5  
Old 02-22-2012
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".
This User Gave Thanks to balajesuri For This Post:
# 6  
Old 02-22-2012
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
# 7  
Old 02-22-2012
Quote:
Originally Posted by greenworld123
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

3. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

4. Shell Programming and Scripting

Find and replace with 0 for characters in a specific position

Need command for position based replace: I need a command to replace with 0 for characters in the positions 11 to 20 to all the lines starts with 6 in a file. For example the file ABC.txt has: abcdefghijklmnopqrstuvwxyz 6abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz... (4 Replies)
Discussion started by: thangabalu
4 Replies

5. Shell Programming and Scripting

Awk command to replace specific position characters.

Hi, I have a fixed width file. The way this file works is say for example there are 30 columns in it each with different sizes say 10,5,2, etc... If data in a field is less than the field size the rest of it is loaded with spaces. I would like an awk command to that would replace I have... (8 Replies)
Discussion started by: pinnacle
8 Replies

6. Shell Programming and Scripting

Using sed to replace a string in a specific position

I asked this before, but my problem got more complicated. Heres what I am trying to do: I'm trying to replace a string at a certain location with another string. Heres the file I'm trying to change: \E I want to replace the escape code at the 3rd line, 2nd column with this escape code... (3 Replies)
Discussion started by: tinman47
3 Replies

7. Shell Programming and Scripting

Sed position specific replace

I'm drawing a blank on how to use sed to replace selectively based on position in the string (vs nth occurence): hello.|there.|how.|are.|you.| I want the period removed in the 3rd item (as defined by the pipe delimiter) if a period is present. So the result in this case would be: ... (2 Replies)
Discussion started by: tiggyboo
2 Replies

8. Shell Programming and Scripting

Add characters at specific position in file

Hello I want to add some value at the specific position. My file has data like Hello Welcome to UNIX Forums Need Assistance I want to add some value at the end but at same character position for all lines. I want my output file to have data like : Here '_' represents blanks.... (3 Replies)
Discussion started by: dashing201
3 Replies

9. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

10. Shell Programming and Scripting

read space filled file and replace text at specific position

Hi I have a spaced filled file having records like below: What I want is to read line having RT3 at position 17-19 then go to position 2651 check the 18 characters (might be space filled till 18 characters). This position should have a... (6 Replies)
Discussion started by: COD
6 Replies
Login or Register to Ask a Question