Skip to main content
POSIXified.
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

One way:

sed '-e :a;sa -e 's/^.\{1,29\}$/&0/;ta;n';ta' -e n file

Match any character(.), and match 1 to 29 characters of the same type(1,29). If match successful, put a '0' behind the pattern matched(&). When the match fails which means when the count of characters excluding the first character exceeds 29, stop it and hence we get the string zero padded by 30.

n at the end simply reads the next line and prints it. This way the even numbered lines get printed untouched.

Executing the above script:

$ sed '-e :a;sa -e 's/^.\{1,29\}$/&0/;ta;n';ta' -e n file
FSDFDSFSD000000000000000000000
FDSFD
FHGDHFDHGHFGHGHGF0000000000000
HHGDF
GFDGDFGFDG00000000000000000000
GFDGFDGFDGDFGDGD

One way:

sed ':a;s/^.\{1,29\}$/&0/;ta;n' file

Match any character(.), and match 1 to 29 characters of the same type(1,29). If match successful, put a '0' behind the pattern matched(&). When the match fails which means when the count of characters excluding the first character exceeds 29, stop it and hence we get the string zero padded by 30.

n at the end simply reads the next line and prints it. This way the even numbered lines get printed untouched.

Executing the above script:

$ sed ':a;s/^.\{1,29\}$/&0/;ta;n' file
FSDFDSFSD000000000000000000000
FDSFD
FHGDHFDHGHFGHGHGF0000000000000
HHGDF
GFDGDFGFDG00000000000000000000
GFDGFDGFDGDFGDGD

One way:

sed -e :a -e 's/^.\{1,29\}$/&0/;ta' -e n file

Match any character(.), and match 1 to 29 characters of the same type(1,29). If match successful, put a '0' behind the pattern matched(&). When the match fails which means when the count of characters excluding the first character exceeds 29, stop it and hence we get the string zero padded by 30.

n at the end simply reads the next line and prints it. This way the even numbered lines get printed untouched.

Executing the above script:

$ sed -e :a -e 's/^.\{1,29\}$/&0/;ta' -e n file
FSDFDSFSD000000000000000000000
FDSFD
FHGDHFDHGHFGHGHGF0000000000000
HHGDF
GFDGDFGFDG00000000000000000000
GFDGFDGFDGDFGDGD
added 419 characters in body
Source Link
Guru
  • 6k
  • 1
  • 22
  • 20

One way:

sed ':a;s/^.\{1,29\}$/&0/;ta;n' file

Match any character(.), and match 1 to 29 characters of the same type(1,29). If match successful, put a '0' behind the pattern matched(&). When the match fails which means when the count of characters excluding the first character exceeds 29, stop it and hence we get the string zero padded by 30.

n at the end simply reads the next line and prints it. This way the even numbered lines get printed untouched.

Executing the above script:

$ sed ':a;s/^.\{1,29\}$/&0/;ta;n' file
FSDFDSFSD000000000000000000000
FDSFD
FHGDHFDHGHFGHGHGF0000000000000
HHGDF
GFDGDFGFDG00000000000000000000
GFDGFDGFDGDFGDGD

One way:

sed ':a;s/^.\{1,29\}$/&0/;ta;n' file

One way:

sed ':a;s/^.\{1,29\}$/&0/;ta;n' file

Match any character(.), and match 1 to 29 characters of the same type(1,29). If match successful, put a '0' behind the pattern matched(&). When the match fails which means when the count of characters excluding the first character exceeds 29, stop it and hence we get the string zero padded by 30.

n at the end simply reads the next line and prints it. This way the even numbered lines get printed untouched.

Executing the above script:

$ sed ':a;s/^.\{1,29\}$/&0/;ta;n' file
FSDFDSFSD000000000000000000000
FDSFD
FHGDHFDHGHFGHGHGF0000000000000
HHGDF
GFDGDFGFDG00000000000000000000
GFDGFDGFDGDFGDGD
Source Link
Guru
  • 6k
  • 1
  • 22
  • 20

One way:

sed ':a;s/^.\{1,29\}$/&0/;ta;n' file