Computing.Net > Forums > Unix > Sed, search/replace w/in range

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Sed, search/replace w/in range

Reply to Message Icon

Name: hack1234
Date: February 21, 2007 at 11:09:29 Pacific
OS: AIX 5.3
CPU/Ram: na
Product: IBM
Comment:

I have a text file containing 100's of thousands of rows. For each line in the file, I want to remove the pipe | character if it exists between position 22 and position 57 on each line. All other pipe characters should remain in tact, just want them removed from positions 22 - 57. I know how to use sed to remove all pipes from the file but cannot figure out how to limit my search/replace to a range of characters. Can anyone help? Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: February 21, 2007 at 14:26:04 Pacific
Reply:

One way is to cut each line into 3 sections, change only the second section, and glue the 3 lines back together ... No error checking:

#!/bin/ksh

while read line
do
len=$(expr "$line" : '.*') # length of line
f1="$(echo "$line" | cut -c1-21)"
f2="$(echo "$line" | cut -c22-57)"
f3="$(echo "$line" | cut -c58-$len)"
f2=$(echo "$f2"|sed 's/|//g')
printf "%s%s%s\n" "$f1" "$f2" "$f3"
done < data.file > newdata.file


0
Reply to Message Icon

Related Posts

See More


Remove Pipe from file getting a specific line f...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Sed, search/replace w/in range

sed character replace over a range www.computing.net/answers/unix/sed-character-replace-over-a-range/6649.html

replace data in a file www.computing.net/answers/unix/replace-data-in-a-file/6189.html

hw 2 search fr *.txt in current dir www.computing.net/answers/unix/hw-2-search-fr-txt-in-current-dir/8222.html