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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a string at a particular position and replace with blank based on position
# 8  
Old 09-09-2015
It is always a good idea to tell us what operating system and shell you're using when asking questions here. With a standards conforming version of sed, you weren't far off with your sed command:
Code:
sed '/(^.\{44\}\)02/s/\(^.\{358\}\).\{1\}\(.*\)/\1 \2/'

When I try running that on Mac OS X (which has a BSD version of sed), I get the diagnostic message:
Code:
sed: 1: "/(^.\{44\}\)02/s/\(^.\{ ...": RE error: parentheses not balanced

which can be fixed by adding a backslash before the 1st open parenthesis:
Code:
sed '/\(^.\{44\}\)02/s/\(^.\{358\}\).\{1\}\(.*\)/\1 \2/'

but I then get the diagnostic:
Code:
sed: 1: "/\(^.\{44\}\)02/s/\(^.\ ...": RE error: invalid repetition count(s)

The standards only require implementations of BRE parsing code to accept repetition counts up to 255, (and the 358 repetition count in the BRE in the substitute command exceeds that). If we change that to two repetition counts (each less than or equal to 255), it works:
Code:
sed '/\(^.\{44\}\)02/s/\(^.\{200\}.\{158\}\).\{1\}\(.*\)/\1 \2/'

but it can be simplified a little bit to just:
Code:
sed '/^.\{44\}02/s/^\(.\{200\}.\{158\}\)./\1 /'

If you want to try this on a Linux system (or another system using the GNU implementation of sed), you might need something like:
Code:
sed --posix '/^.\{44\}02/s/^\(.\{200\}.\{158\}\)./\1 /'

but the GNU version might be able to handle the higher repetition count (I don't have a GNU sed to test at this point).
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 09-16-2015
Hi Don,

Thank you so much. It worked.

I tried the below command using awk which also worked.

Code:
awk '{if(substr($0,45,2)=="02") printf (substr($0,1,358) " " substr($0,360,length($0)) "\n");else printf ($0 "\n");}' file1 > file2

Regards,
Pradhikshan

Last edited by Don Cragun; 09-16-2015 at 03:36 AM.. Reason: Add CODE and ICODE tags.
# 10  
Old 09-16-2015
Note that using:
Code:
printf (substr($0,1,358) " " substr($0,360,length($0)) "\n")

and:
Code:
printf ($0 "\n")

is very dangerous and can produce diagnostic messages or mangled output, and maybe also terminate your awk script instead of producing the output you want if there are any <percent-sign> or <backslash> characters in the input file you are processing. For your own safety, please change the above printf statements in your awk script to something more like:
Code:
printf("%s", substr($0,1,358) " " substr($0,360) "\n")
    or
printf("%s %s\n", substr($0,1,358),  substr($0,360))

and:
Code:
printf("%s", $0 "\n")
    or
printf("%s\n", $0)
    or much more simply
print $0
    or even just
print

respectively.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search for blank fields in a text file from a certain position?

Sample txt file : OK00001111112| OK00003443434|skjdaskldj OK32812983918|asidisoado OK00000000001| ZM02910291029|sldkjaslkjdasldjk what would be the shell script to figure out the blank space (if any) after the pipe sign? (4 Replies)
Discussion started by: chatwithsaurav
4 Replies

2. Shell Programming and Scripting

Fixed width file search based on position value

Hi, I am unable to find the right option to extract the data in the fixed width file. sample data abcd1234xgyhsyshijfkfk hujk9876 io xgla loki8787eljuwoejroiweo dkfj9098 dja Search based on position 8-9="xg" and print the entire row output ... (4 Replies)
Discussion started by: onesuri
4 Replies

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

4. Shell Programming and Scripting

Search and Replace by record position

Hi All, I have a file that I would like to search for data and replace other data by record position number: Example search.. search for "CLARK KENT" and replace Amt data "000025" with "000155"??? I'm able to search and replace unique data but, came to a stump when wanting to replace data... (11 Replies)
Discussion started by: macastor
11 Replies

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

6. UNIX for Dummies Questions & Answers

Replace based on an exact position

Trying to use sed - but having no luck. I have a text file - I want to replace whatever character is in position 106, 157 and 237 w/ the string "xxx". Want this change for all lines w/in that text file. I'm open to using awk or whatever command would be best for replacing characters based... (5 Replies)
Discussion started by: svn
5 Replies

7. Shell Programming and Scripting

Replace strings based on position and length?

Suppose i have a file which contains thousands of records. e.g adjgmptjadmwpgjmwmd i need to replace the string from 3rd to 8th position using awk script in entire file. And also the positions will be passed as parameter. (3 Replies)
Discussion started by: laknar
3 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

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