Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How do I replace a string in file that is in a certain position with spaces? Post 302969871 by Scrutinizer on Tuesday 29th of March 2016 12:40:55 PM
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..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 11:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy