How do I replace a string in file that is in a certain position with spaces?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How do I replace a string in file that is in a certain position with spaces?
# 1  
Old 03-29-2016
How do I replace a string in file that is in a certain position with spaces?

I am trying to replace the string in position 26 through 35 of the data file with 10 spaces and I want the remaining file to stay as is, the record length is over 900 characters? I am trying to use the AWK and substr but I am not getting it formatted correctly.

Before
Code:
512256734618F198451213732347011262015402      315138010001103001

After
Code:
512256734618F1984512137323                5402      315138010001103001


Last edited by Scrutinizer; 03-29-2016 at 12:15 PM.. Reason: code tags
# 2  
Old 03-29-2016
Hello fnwine1500,

Welcome to forums, please use code tags as per forum rules for commands/codes/Inputs which you are using into your posts. Could you please try following and let me know if this helps you.
Code:
echo "512256734618F198451213732347011262015402 315138010001103001" | awk '{sub(substr($0,26,10)," ",$0);print}'

Output will be as follows.
Code:
512256734618F198451213732 15402 315138010001103001

Thanks,
R. Singh
# 3  
Old 03-29-2016
That worked but this is how I coded it to make sure every line was changed:

Code:
awk '{sub(substr($0,26,10)," ",$0);print}' ORIGINAL_FILE.dat > NEWFILE.dat


Thank you very much!

Last edited by Scrutinizer; 03-29-2016 at 01:08 PM.. Reason: code tags
# 4  
Old 03-29-2016
Didn't you want to insert 10 spaces? Above just puts in a single space...
And, above is safe only if the 10 chars are unique. You need to be sure there's no false positives in the cahrs 1 - 25!
If your awk version allows for empts field separators, try
Code:
awk  '{for (i=26; i<=35; i++) $i = " "} 1' FS="" OFS="" file
512256734618F198451213732          15402      315138010001103001

# 5  
Old 03-29-2016
With gawk 4:
Code:
awk '{gsub(/./," ",$2)}1' FIELDWIDTHS="25 10 1000" OFS="" file

# 6  
Old 03-29-2016
I couldn't get either suggestion to work. But this seems to work

Code:
awk '{sub(substr($0,26,10),"          ",$0);print}' ORIGINAL_FILE.dat > NEWFILE.d

at

The " " actually has 10 spaces in between the quotes

Last edited by Scrutinizer; 03-29-2016 at 01:39 PM.. Reason: code tags
# 7  
Old 03-29-2016
Quote:
Originally Posted by RavinderSingh13
[..]Could you please try following and let me know if this helps you.
Code:
echo "512256734618F198451213732347011262015402 315138010001103001" | awk '{sub(substr($0,26,10)," ",$0);print}'

[..]
Quote:
Originally Posted by fnwine1500
I couldn't get either suggestion to work. But this seems to work

Code:
awk '{sub(substr($0,26,10),"          ",$0);print}' ORIGINAL_FILE.dat > NEWFILE.dat

Note that this will fail if the same sequence of 10 characters happens to occur somewhere before position 16.
To illustrate:
Code:
$ echo "5123470112620F198451213732347011262015402      315138010001103001" | awk '{sub(substr($0,26,10),"          ",$0);print}'
51          0F198451213732347011262015402      315138010001103001

So this is not a reliable solution.

Quote:
The " " actually has 10 spaces in between the quotes
And those 10 spaces will be shown, if you use code tags, like I inserted into your post..


----

Try:
Code:
awk '{s=substr($0,from,len); gsub(/./," ",s); print(substr($0,1,from-1) s substr($0,from+len))}' from=26 len=10 ORIGINAL_FILE.dat > NEWFILE.da


Last edited by Scrutinizer; 03-29-2016 at 03:23 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

5. Shell Programming and Scripting

Replace character in certain position in a string

Hello everyone this is my first post of many to come :) I am writing a script and in this script at one point i need to replace a character in a particular position in a string for example: in the string "mystery" i would need to replace the 3rd position to an "r" so the string becomes... (3 Replies)
Discussion started by: snipaa
3 Replies

6. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

7. Shell Programming and Scripting

To trim Certain field in a line of a file and replace the new string in that position

To trim 3rd field in for all the lines of a file and replace the modified string in that particular field. For example i have a file called Temp.txt having content Temp.txt ----------------- 100,234,M1234 400,234,K1734 300,345,T3456 ---------------- So the modified file output should... (4 Replies)
Discussion started by: rpadhi
4 Replies

8. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

9. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies

10. Shell Programming and Scripting

Find the position of a string and replace with another string

Hi, I have a file named "Test_2008_01_21" The file contains a string "manual" that occurs many times in the file How can i find the positions of the string "manual" in the file Ex: if the string " manual " occurs three times in the file. i want to replace the second occurance of string... (6 Replies)
Discussion started by: bab123
6 Replies
Login or Register to Ask a Question