Advertisement

10.17.2007 at 06:27AM PDT, ID: 22898819
[x]
Attachment Details

Trying to replace characters in a text file by position on line (not pattern or delimiter) in unix shell scripting

Asked by Nikola_Novak in KornShell (ksh), Unix Systems Programming, Regular Expressions

Tags: replace, file, text, character, position

I am trying to develop a shell script on AIX 5.3.5 / ksh or ksh93, which will take a text file with fixed-length rows ('records'), and on each row replace certain characters, based on position, with spaces.
For example, I may want to replace characters 450 through 470 with 20 blank spaces.

Problem I've encountered is that most unix utilities are based on delimited fields, not position-defined fields. I've tried to cludge it in with regexes:
CTC_PS_BLANKCMD="sed s/^\(.\{$CTC_PS_BEG\}\)\(.\{$CTC_PS_LEN\}\)/\1$CTC_PS_SPACER/ $CTC_PS_SOURCE"

(where CTC_PS_BEG is the first character to be replaced, CTC_PS_LEN is the length, CTC_PS_SPACER is the string with spaces, and CTC_PS_SOURCE is the input file).
However, two problems: no matter how I play with quotes, interpreter/shell changes the number of spaces to single; more over, it only works for characters up to 255, because of AIX limit of RC_MAX_DUP which I cannot change - it only allows up to 255 matches of a regular expression.

What is the best way I can create a command that will replace characters based on position?Start Free Trial
[+][-]10.17.2007 at 06:35AM PDT, ID: 20093089

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]10.17.2007 at 06:41AM PDT, ID: 20093139

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.17.2007 at 06:45AM PDT, ID: 20093186

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.17.2007 at 06:47AM PDT, ID: 20093195

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.17.2007 at 06:53AM PDT, ID: 20093272

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.17.2007 at 06:57AM PDT, ID: 20093313

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.17.2007 at 07:03AM PDT, ID: 20093403

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]10.17.2007 at 07:03AM PDT, ID: 20093406

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.17.2007 at 07:07AM PDT, ID: 20093469

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.17.2007 at 07:22AM PDT, ID: 20093619

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.17.2007 at 07:28AM PDT, ID: 20093691

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.17.2007 at 09:50AM PDT, ID: 20095000

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: KornShell (ksh), Unix Systems Programming, Regular Expressions
Tags: replace, file, text, character, position
Sign Up Now!
Solution Provided By: ahoffmann
Participating Experts: 3
Solution Grade: A
 
 
[+][-]10.17.2007 at 10:38AM PDT, ID: 20095390

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.17.2007 at 11:34AM PDT, ID: 20095828

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]10.17.2007 at 11:37AM PDT, ID: 20095849

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_1_20070628