Replacing a position in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing a position in a file
# 1  
Old 06-12-2009
Replacing a position in a file

Hi
I'm trying to use awk in a file(test123.dat).
My requirement is to to check for the 65th position, if the 65th position is a space then replace the 65th position by the number 9.

This is the code that i used:
awk '{substr($0,65,1) ~ / / }{sub(substr($0,65,1),"9")}{print}' test123.dat

But this doesnt work Smilie
Plz Help...
# 2  
Old 06-12-2009
A little change...
Code:
awk '{if(substr($0,65,1)==" ")sub(substr($0,65,1),"9");print}'

-----Post Update-----

by the way
this was wrong
Code:
 (substr($0,5,1) ~ / / )

-----Post Update-----

by the way
this was wrong
Code:
 (substr($0,5,1) ~ / / )

# 3  
Old 06-12-2009
This is a sample data from the file test123.dat

0049DA06152009FPA06USD 000000000000D000000000009.9

If i use awk '{if(substr($0,65,1)==" ")sub(substr($0,65,1),"9");print}' test123.dat

this is the result that i get, the "9" gets appended after USD

0049DA06152009FPA06USD9 000000000000D000000000009.9

but i should get the result

0049DA06152009FPA06USD 000000000000D000000000009.99
# 4  
Old 06-12-2009
Code:
 
 
echo "0049DA06152009FPA06USD 000000000000D000000000009.9" | awk '{ print length($0) }'
50

r you trying to modifi 65th character ?..
# 5  
Old 06-12-2009
I had pasted only a part of the data in my previous post, sorry abt it

This is the actual data in the file

0049DA06152009FPA06USD 000000000000D000000000009.9 ORA100144

The total length is 149 and im trying to do the changes in the 65th position

When i post the actual data the spaces/tabs in the actual data does not get posted.
# 6  
Old 06-12-2009
try something like this
Code:
 
 echo "0049DA06152009FPA06USD 000000000000D000000000009.9 ORA100144" |
awk 'substr($0,51,1) ~/[^0-9A-Z]/ {print substr($0,1,50)"9"substr($0,52)}'

# 7  
Old 06-12-2009
Quote:
Originally Posted by angelarosh
I had pasted only a part of the data in my previous post, sorry abt it

This is the actual data in the file

0049DA06152009FPA06USD 000000000000D000000000009.9 ORA100144

The total length is 149 and im trying to do the changes in the 65th position

When i post the actual data the spaces/tabs in the actual data does not get posted.
First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position? I am asking this because the file I have doesn't always follow a pattern. For example the file I have is the result of a command to obtain windows ACLs: icacls C:\ /t... (5 Replies)
Discussion started by: nakaedu
5 Replies

2. Shell Programming and Scripting

Inserting value at a particular position without changing the position of other characters

Hi All, I wanted a sed/awk command to add a value/character on a particular position without disturbing the position of other characters. I have file a.txt OL 10031 Day Black Midi Good Value P01 P07 OL 10031 Day Black Short Good Value P01 P07 I want to get the output as... (2 Replies)
Discussion started by: rahulsk
2 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

Need command or script to print all lines from 2nd position to last but one position

hi guys, i want command or script to display the content of file from 2nd position to last but one position of a file abcdefghdasdasdsd 123,345,678,345,323 434,656,656,656,656 678,878,878,989,545 4565656667,65656 i want to display the same above file without first and... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

5. Shell Programming and Scripting

Reading a file and replacing char by position

Hi I'm looking for a way to read a text file that may contain 1000 records or more and each of these records has 460 characters. I need to read each record, and add a string of characters starting at position 256 for each record. Any suggestions using UNIX shell scripting. (4 Replies)
Discussion started by: macastor
4 Replies

6. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

7. Shell Programming and Scripting

Moving first position in a file to the last position

hi, I have a file which consists of some records: 2010_06_4010093_001_001|10|ABCDEFGH|9|4010093||0040400||31.12.2009|S|O|X||||20100602093851-31.12.2009|XXBBFC|EFG||||00001| 2010_06_4010162_001_001|11|ABCDEFGH|9|4010162||0040400||31.12.2009|S|O|X||||20100602093851-31.12.2009|XXBBFC|EFG||||00002|... (11 Replies)
Discussion started by: pparthiv
11 Replies

8. UNIX for Dummies Questions & Answers

Grep in a file for a particular pattern in a particular position witihn the file

Assume I have a file with a lot of data sets like 123 abc 01 456 def 02 789 ghi and I only want to grep all that datasets from my file having the pattern '02' at the postion 9-10 to get only 456 def 02 So I could group the datsets into three files according to the position 9-10, one... (9 Replies)
Discussion started by: ABE2202
9 Replies

9. UNIX for Dummies Questions & Answers

find if a position is between a given start and end position

Hi, I am a newbie in unix programming so maybe this is a simple question. I would like to know how can I make a script that outputs only the values that are not between any given start and end positions Example file1: 2 30 40 80 82 100 file2: ID1 1 ID2 35 ID3 80 ID4 81 ID6... (9 Replies)
Discussion started by: fadista
9 Replies

10. Shell Programming and Scripting

how to find a position and print some string in the next and same position

I need a script for... how to find a position of column data and print some string in the next line and same position position should find based on *HEADER8* in text for ex: ord123 abs 123 987HEADER89 test234 ord124 abc 124 987HEADER88 test235 ... (1 Reply)
Discussion started by: naveenkcl
1 Replies
Login or Register to Ask a Question