Re: Insert character x at...
Tim Chase <[email protected]>
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <[email protected]> |
On 2013-05-14 21:17, blackmuerte wrote:
> Hi all, I'm needing some help creating a command line that's
> hopefully simple.
>
> I'd like to use a sed command in a batch file to do the following
> on a fixed format input file:
>
> For every line in input file that begins with '0530' (in first 4
> characters on line), insert 'x' at position/column 32 on same line,
> then output the whole file to a newfile.
>
> Optionally, if its possible to delete one character (a blank space)
> on same line at position 34, that would be helpful as well, to
> preserve formatting.
For the original request:
sed '/^0530/s/\(.\{31\}/\1x/' input.txt
For the followup, you can gather the rest of the characters and juggle
them accordingly:
sed '/^0530/s/\(.\{31\}\)\(.\{2\}\)./\1x\2/' input.txt
The 31 is your insertion-position-minus-one, and the "2" in "\{2\}" is
the number of characters you want to keep after the inserted
quantity. If you want to insert more than one character and delete
the same number of characters, you can do it with
sed '/^0530/s/\(.\{31\}\)\(.\{2\}\).\{3\}/\1foo\2/' input.txt
where the "3" is the length of the replacement "foo".
Adjust for any fenceposting errors you stumble across ;-)
-tim