Discussion:
Insert character x at...
blackmuerte
2013-05-14 21:17:16 UTC
Permalink
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.

Any/all help is greatly appreciated.

Thanks
Tim Chase
2013-05-16 14:20:16 UTC
Permalink
Post by blackmuerte
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
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
Tim Chase
2013-05-16 15:10:53 UTC
Permalink
Post by Tim Chase
sed '/^0530/s/\(.\{31\}/\1x/' input.txt
Doh, I missed the closing "\)" in there. Should have been

sed '/^0530/s/\(.\{31\}\)/\1x/' input.txt

which could have been reduced in this particular case to

sed '/^0530/s/.\{31\}/&x/' input.txt
One alternative to insert a char after a column N is to use the
numeric flag in the s command.
$ echo 123456789 | sed 's/./&x/5'
12345x6789
But that's pretty slick. I don't often recall the /{number} flag, as
vi/vim doesn't offer it, so I don't have as much occasion to use it.

-tim
Aurelio Jargas
2013-05-16 14:59:39 UTC
Permalink
Post by Tim Chase
Post by blackmuerte
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.
sed '/^0530/s/\(.\{31\}/\1x/' input.txt
One alternative to insert a char after a column N is to use the numeric
flag in the s command.

$ echo 123456789 | sed 's/./&x/5'
12345x6789

Substitute any char (.) by itself (&) and an x. But only in the 5th
occurrence.
--
Aurelio | www.aurelio.net | @oreio


[Non-text portions of this message have been removed]
Loading...