Skip to main content
Added perl because it's AIX and sed can't handle very long lines
Source Link
Chris Davies
  • 128.5k
  • 16
  • 179
  • 326

You can use a counted RE. For example, x{12} will match 12 x characters, and y{1,3} would match 1, 2, or 3 y characters. Here we're going to use .{30} to match 30 character wildcards (i.e. 30 of any character). The \1 in the result string matches the bracketed reference in the pattern match

sed -r 's#^(.{30})#\1213 #' file

In your updated question you now say there are 878 characters before the insert. So just amend the example's 30 to reality's 878 and insert XXX

sed -r 's#^(.{878})#\1XXX#' file

The same process can apply for any fixed-width modification.

You can use perl too, which doesn't have the line length limitations that plague some implementations of sed,

perl -pe 's#^(.{878})#$1XXX#' file

You can use a counted RE. For example, x{12} will match 12 x characters, and y{1,3} would match 1, 2, or 3 y characters. Here we're going to use .{30} to match 30 character wildcards (i.e. 30 of any character). The \1 in the result string matches the bracketed reference in the pattern match

sed -r 's#^(.{30})#\1213 #' file

In your updated question you now say there are 878 characters before the insert. So just amend the example's 30 to reality's 878 and insert XXX

sed -r 's#^(.{878})#\1XXX#' file

The same process can apply for any fixed-width modification.

You can use a counted RE. For example, x{12} will match 12 x characters, and y{1,3} would match 1, 2, or 3 y characters. Here we're going to use .{30} to match 30 character wildcards (i.e. 30 of any character). The \1 in the result string matches the bracketed reference in the pattern match

sed -r 's#^(.{30})#\1213 #' file

In your updated question you now say there are 878 characters before the insert. So just amend the example's 30 to reality's 878 and insert XXX

sed -r 's#^(.{878})#\1XXX#' file

The same process can apply for any fixed-width modification.

You can use perl too, which doesn't have the line length limitations that plague some implementations of sed,

perl -pe 's#^(.{878})#$1XXX#' file
added 17 characters in body
Source Link
Chris Davies
  • 128.5k
  • 16
  • 179
  • 326

You can use a counted RE. For example, x{12} will match 12 x characters, and y{1,3} would match 1, 2, or 3 y characters. Here we're going to use .{30} to match 30 character wildcards (i.e. 30 of any character). The \1 in the result string matches the bracketed reference in the pattern match

sed -r 's#^(.{30})#\1213 #' file

In your updated question you now say there are 878 characters before the insert. So just amend the example's 30 to reality's 878 and insert XXX

sed -r 's#^(.{878})#\1213 #'#\1XXX#' file

The same process can apply for any fixed-width modification.

You can use a counted RE. For example, x{12} will match 12 x characters, and y{1,3} would match 1, 2, or 3 y characters. Here we're going to use .{30} to match 30 character wildcards (i.e. 30 of any character). The \1 in the result string matches the bracketed reference in the pattern match

sed -r 's#^(.{30})#\1213 #' file

In your updated question you now say there are 878 characters before the insert. So just amend the example's 30 to reality's 878

sed -r 's#^(.{878})#\1213 #' file

The same process can apply for any fixed-width modification.

You can use a counted RE. For example, x{12} will match 12 x characters, and y{1,3} would match 1, 2, or 3 y characters. Here we're going to use .{30} to match 30 character wildcards (i.e. 30 of any character). The \1 in the result string matches the bracketed reference in the pattern match

sed -r 's#^(.{30})#\1213 #' file

In your updated question you now say there are 878 characters before the insert. So just amend the example's 30 to reality's 878 and insert XXX

sed -r 's#^(.{878})#\1XXX#' file

The same process can apply for any fixed-width modification.

Simplied per recommendation from comments
Source Link
Chris Davies
  • 128.5k
  • 16
  • 179
  • 326

You can use a counted RE. For example, x{12} will match 12 x characters, and y{1,3} would match 1, 2, or 3 y characters. Here we're going to use .{30} to match 30 character wildcards (i.e. 30 of any character). The \1 and \2 in the result string matchmatches the bracketed referencesreference in the pattern match

sed -r 's#^(.{30})#\1213 #' file

In your updated question you now say there are 878 characters before the insert. So just amend the example's 30 to reality's 878

sed -r 's#^(.*{878})#\1213 \2#'#' file

The same process can apply for any fixed-width modification.

You can use a counted RE. For example, x{12} will match 12 x characters, and y{1,3} would match 1, 2, or 3 y characters. Here we're going to use .{30} to match 30 character wildcards (i.e. 30 of any character). The \1 and \2 in the result string match the bracketed references in the pattern match

sed -r 's#^(.{30})(.*)#\1213 \2#' file

You can use a counted RE. For example, x{12} will match 12 x characters, and y{1,3} would match 1, 2, or 3 y characters. Here we're going to use .{30} to match 30 character wildcards (i.e. 30 of any character). The \1 in the result string matches the bracketed reference in the pattern match

sed -r 's#^(.{30})#\1213 #' file

In your updated question you now say there are 878 characters before the insert. So just amend the example's 30 to reality's 878

sed -r 's#^(.{878})#\1213 #' file

The same process can apply for any fixed-width modification.

Source Link
Chris Davies
  • 128.5k
  • 16
  • 179
  • 326
Loading