sed replace range of characters in each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replace range of characters in each line
# 1  
Old 11-21-2013
sed replace range of characters in each line

Hi,

I'm trying to replace a range of characters by their position in each line by spaces.

I need to replace characters 95 to 145 by spaces in each line.

i tried below but it doesn't work

Code:
sed -r "s/^(.{94})(.{51})/\ /" inputfile.txt > outputfile.txt

can someone please help me understand where I'm going wrong..
# 2  
Old 11-21-2013
Quote:
Originally Posted by Kevin Tivoli
Code:
sed -r "s/^(.{94})(.{51})/\ /" inputfile.txt > outputfile.txt

can someone please help me understand where I'm going wrong..
Yes: This contains syntactical errors. Would it be syntactically correct it would take the first 145 characters (in two groups, 94 and 51 chars) and replaces these by a single space.

Do the following:

Code:
sed 's/^\(.\{94\}\)\(.\{51\}\)/\1<b><b>.....51 blanks...<b>/' infile > outfile

You put the first 145 characters into two groups: "\(....\)" is a group each, the first consists of ".\{94\}" (any character, 94 times), the second similarly of 51 chars. This is replaced by: the first group ("\1"), so this is effectively unchanged, then 51 blanks. The rest of the line is left untouched. Notice, that i used "<b>" to denote a blank, otherwise it would be hard to read in the forum.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 11-21-2013
Many thanks bakunin :-)

Appreciate the detailed explanation, which is what i was hoping for than just an answer. your explanation helped me a lot to get my head around some basics of sed which I'm still learning.
# 4  
Old 11-21-2013
With awk:
Code:
awk '{printf "%s%*s%s\n",substr($0,1,m-1),n-m+1," ",substr($0,n+1)}' m=95 n=145 inputfile.txt > outputfile.txt

With perl:
Code:
perl -pe 'BEGIN {$m=95; $n=145; $d=$n-$m+1} substr($_,$m-1,$d) = " " x $d' inputfile.txt > outputfile.txt


Last edited by MadeInGermany; 11-21-2013 at 06:10 PM.. Reason: bug fix and added perl
This User Gave Thanks to MadeInGermany For This Post:
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 replace all but the first 3 characters with sed?

This seems like it should be an easy problem, but for some reason I am struggling with the solution. I simply want to replace all characters after the first 3 characters with another character, preferably with sed. Thanks in advance. Like this, but producing the proper number of *'s: sed... (30 Replies)
Discussion started by: leolson
30 Replies

2. UNIX for Dummies Questions & Answers

Using sed to replace a range of number

Trying to use SED to replace numbers that fall into a range but can't seem to get the logic to work and am wondering if SED will do this. I have a file with the following numbers 3 26 20 5. For the numbers that are greater than zero and less than 25, SED would add the word range after the... (7 Replies)
Discussion started by: jimmyf
7 Replies

3. Shell Programming and Scripting

Replace characters infile with sed

I have several files in a directory that look like this: jacket-n r potential-n - outcome-n f reputation-n b I want to replace the characters in the second column with certain numbers. For instance, I want the letters 'f', 'r' and 'b' in the second column to replaced with 0 and I want the... (1 Reply)
Discussion started by: owwow14
1 Replies

4. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

5. UNIX for Dummies Questions & Answers

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet... (3 Replies)
Discussion started by: Chella15
3 Replies

6. Shell Programming and Scripting

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet able... (0 Replies)
Discussion started by: Chella15
0 Replies

7. Shell Programming and Scripting

sed replace characters in next line with input from a file

Hi, I have a set of strings in filea. I want to search string xyz in fileb and replace next line in file b with the content from filea. #cat filea abc def ghi #cat fileb asdkjdslka sajljskdjoi xyzjjjjkko aaaaaaaa bbbbbbbb cccccccc xyzsdsajd dddddddd eeeeeeee (2 Replies)
Discussion started by: anilvk
2 Replies

8. Shell Programming and Scripting

How to replace a range of text with sed or awk?

Howdy! I'm trying to automate editing of a configuration file (custom.conf for GDM). I need to find every line between a line that starts with "" and the next line that starts with "", I want to preserve that line, but then delete all the lines in that configuration section and then insert... (3 Replies)
Discussion started by: TXTad
3 Replies

9. Shell Programming and Scripting

Replace string in a file within a range of line

Hi, I want to replace the srting '; with ABCD'; in a file from line 1 to line 65. Is there any single command to do it without using awk Thanks for quick reply https://www.unix.com/images/misc/progress.gif (3 Replies)
Discussion started by: tosattam
3 Replies

10. Shell Programming and Scripting

how to replace control characters using sed?

How can I use sed to replace a ctrl character such as 'new line' (\0a) to something else? Or any other good command can do this job? Thanks, Hillxy (5 Replies)
Discussion started by: hillxy
5 Replies
Login or Register to Ask a Question