Skip to main content
added 44 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

We may use either of sed or awk to completely solve the problem.


With sed:

$ sed 's/^.\./0&/' file.txt 

When & occurs in the replacement part of the substitution command (s), it will be expanded to the part of the input line that matches the pattern part of the command.

The regular expression ^.\. means "match all lines that starts with (^) an arbitrary character (.) followed by a literal dot (\.)".

If the line is 1.02.2017 23:40:00, the pattern will match, and 1. would be replaced by 01. at the start of the line.


With awk:

Building on the partial awk code in the question...

This will, as stated, print the second character of each line of input:

$ awk '{ print substr($0, 2, 1) }' file.txt 

We can use the fact that substr($0, 2, 1) returns the second character and use that as the condition:

$ awk 'substr($0, 2, 1) == "." { ... }' file.txt 

What goes into { ... } is code that prepends $0, which is the contents of the current line, with a zero if the preceding condition is true:

$ awk 'substr($0, 2, 1) == "." { $0 = "0" $0 }' file.txt 

Then we just need to make sure that all lines are printed:

$ awk 'substr($0, 2, 1) == "." { $0 = "0" $0 } { print }' file.txt 

The condition substr($0, 2, 1) == "." may of course be changed into a regular expression too (we use exactly the same expression as we used in the sed solution):

$ awk '/^.\./ { $0 = "0" $0 } { print }' file.txt 

Some people who thinks "shorter is always better" would write that as

$ awk '/^.\./ { $0 = "0" $0 } 1' file.txt 

(and probably also remove most spaces: awk '/^.\./{$0="0"$0}1' file.txt)

We may use either of sed or awk to completely solve the problem.


With sed:

$ sed 's/^.\./0&/' file.txt 

When & occurs in the replacement part of the substitution command (s), it will be expanded to the part of the input line that matches the pattern part of the command.

The regular expression ^.\. means "match all lines that starts with (^) an arbitrary character (.) followed by a literal dot (\.)".

If the line is 1.02.2017 23:40:00, the pattern will match, and 1. would be replaced by 01. at the start of the line.


With awk:

Building on the partial awk code in the question...

This will, as stated, print the second character of each line of input:

$ awk '{ print substr($0, 2, 1) }' file.txt 

We can use the fact that substr($0, 2, 1) returns the second character and use that as the condition:

$ awk 'substr($0, 2, 1) == "." { ... }' file.txt 

What goes into { ... } is code that prepends $0 with a zero if the preceding condition is true:

$ awk 'substr($0, 2, 1) == "." { $0 = "0" $0 }' file.txt 

Then we just need to make sure that all lines are printed:

$ awk 'substr($0, 2, 1) == "." { $0 = "0" $0 } { print }' file.txt 

The condition substr($0, 2, 1) == "." may of course be changed into a regular expression too (we use exactly the same expression as we used in the sed solution):

$ awk '/^.\./ { $0 = "0" $0 } { print }' file.txt 

Some people who thinks "shorter is always better" would write that as

$ awk '/^.\./ { $0 = "0" $0 } 1' file.txt 

(and probably also remove most spaces: awk '/^.\./{$0="0"$0}1' file.txt)

We may use either of sed or awk to completely solve the problem.


With sed:

$ sed 's/^.\./0&/' file.txt 

When & occurs in the replacement part of the substitution command (s), it will be expanded to the part of the input line that matches the pattern part of the command.

The regular expression ^.\. means "match all lines that starts with (^) an arbitrary character (.) followed by a literal dot (\.)".

If the line is 1.02.2017 23:40:00, the pattern will match, and 1. would be replaced by 01. at the start of the line.


With awk:

Building on the partial awk code in the question...

This will, as stated, print the second character of each line of input:

$ awk '{ print substr($0, 2, 1) }' file.txt 

We can use the fact that substr($0, 2, 1) returns the second character and use that as the condition:

$ awk 'substr($0, 2, 1) == "." { ... }' file.txt 

What goes into { ... } is code that prepends $0, which is the contents of the current line, with a zero if the preceding condition is true:

$ awk 'substr($0, 2, 1) == "." { $0 = "0" $0 }' file.txt 

Then we just need to make sure that all lines are printed:

$ awk 'substr($0, 2, 1) == "." { $0 = "0" $0 } { print }' file.txt 

The condition substr($0, 2, 1) == "." may of course be changed into a regular expression too (we use exactly the same expression as we used in the sed solution):

$ awk '/^.\./ { $0 = "0" $0 } { print }' file.txt 

Some people who thinks "shorter is always better" would write that as

$ awk '/^.\./ { $0 = "0" $0 } 1' file.txt 

(and probably also remove most spaces: awk '/^.\./{$0="0"$0}1' file.txt)

added 194 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

We may use either of sed or awk to completely solve the problem.


With sed:

$ sed 's/^.\./0&/' file.txt 

When & occurs in the replacement part of the substitution command (s), it will be expanded to the part of the input line that matches the pattern part of the command.

The regular expression ^.\. means "match all lines that starts with (^) an arbitrary character (.) followed by a literal dot (\.)".

If the line is 1.02.2017 23:40:00, the pattern will match, and 1. would be replaced by 01. at the start of the line.


With awk:

Building on the partial awk code in the question (sed solution further down)...

The condition substr($0, 2, 1) == "." may of course be changed into a regular expression too (we use exactly the same expression as we used in the sed solution):

(and probably also remove most spaces: awk '/^.\./{$0="0"$0}1' file.txt)

Alternatively, to solve it with sed instead of awk:

$ sed 's/^.\./0&/' file.txt 

When & occurs in the replacement part of the substitution command (s), it will be expanded to the part of the input line that matches the pattern part of the command.

The regular expression is the same as in the last awk variation and means "match all lines that starts with (^) an arbitrary character (.) followed by a literal dot (\.)".

Building on the partial awk code in the question (sed solution further down)...

The condition substr($0, 2, 1) == "." may of course be changed into a regular expression too:

(and probably also remove most spaces: awk '/^.\./{$0="0"$0}1' file.txt)

Alternatively, to solve it with sed instead of awk:

$ sed 's/^.\./0&/' file.txt 

When & occurs in the replacement part of the substitution command (s), it will be expanded to the part of the input line that matches the pattern part of the command.

The regular expression is the same as in the last awk variation and means "match all lines that starts with (^) an arbitrary character (.) followed by a literal dot (\.)".

We may use either of sed or awk to completely solve the problem.


With sed:

$ sed 's/^.\./0&/' file.txt 

When & occurs in the replacement part of the substitution command (s), it will be expanded to the part of the input line that matches the pattern part of the command.

The regular expression ^.\. means "match all lines that starts with (^) an arbitrary character (.) followed by a literal dot (\.)".

If the line is 1.02.2017 23:40:00, the pattern will match, and 1. would be replaced by 01. at the start of the line.


With awk:

Building on the partial awk code in the question...

The condition substr($0, 2, 1) == "." may of course be changed into a regular expression too (we use exactly the same expression as we used in the sed solution):

(and probably also remove most spaces: awk '/^.\./{$0="0"$0}1' file.txt)

added 15 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

Building on the partial awk code in the question (sed solution further down)...

This will, as stated, print the second character of each line of input:

$ awk '{ print substr($0, 2, 1) }' file.txt 

We can use the fact that substr($0, 2, 1) returns the second character and use that as the condition:

$ awk 'substr($0, 2, 1) == "." { ... }' file.txt 

What goes into { ... } is code that prepends $0 with a zero if the preceding condition is true:

$ awk 'substr($0, 2, 1) == "." { $0 = "0" $0 }' file.txt 

Then we just need to make sure that all lines are printed:

$ awk 'substr($0, 2, 1) == "." { $0 = "0" $0 } { print }' file.txt 

The condition substr($0, 2, 1) == "." may of course be changed into a regular expression too:

$ awk '/^.\./ { $0 = "0" $0 } { print }' file.txt 

Some people who thinks "shorter is always better" would write that as

$ awk '/^.\./ { $0 = "0" $0 } 1' file.txt 

(butand probably also remove most spaces: awk '/^.\./{$0="0"$0}1' file.txt)

Alternatively, to solve it with sed instead of awk:

$ sed 's/^.\./0&/' file.txt 

When & occurs in the replacement part of the substitution command (s), it will be expanded to the part of the input line that matches the pattern part of the command.

The regular expression is the same as in the last awk variation and means "match all lines that starts with (^) an arbitrary character (.) followed by a literal dot (\.)".

Building on the partial awk code in the question (sed solution further down)...

This will, as stated, print the second character of each line of input:

$ awk '{ print substr($0, 2, 1) }' file.txt 

We can use the fact that substr($0, 2, 1) returns the second character and use that as the condition:

$ awk 'substr($0, 2, 1) == "." { ... }' file.txt 

What goes into { ... } is code that prepends $0 with a zero if the preceding condition is true:

$ awk 'substr($0, 2, 1) == "." { $0 = "0" $0 }' file.txt 

Then we just need to make sure that all lines are printed:

$ awk 'substr($0, 2, 1) == "." { $0 = "0" $0 } { print }' file.txt 

The condition substr($0, 2, 1) == "." may of course be changed into a regular expression too:

$ awk '/^.\./ { $0 = "0" $0 } { print }' file.txt 

Some people who thinks "shorter is always better" would write that as

$ awk '/^.\./ { $0 = "0" $0 } 1' file.txt 

(but probably remove most spaces)

Alternatively, to solve it with sed instead of awk:

$ sed 's/^.\./0&/' file.txt 

When & occurs in the replacement part of the substitution command (s), it will be expanded to the part of the input line that matches the pattern part of the command.

The regular expression is the same as in the last awk variation and means "match all lines that starts with (^) an arbitrary character (.) followed by a literal dot (\.)".

Building on the partial awk code in the question (sed solution further down)...

This will, as stated, print the second character of each line of input:

$ awk '{ print substr($0, 2, 1) }' file.txt 

We can use the fact that substr($0, 2, 1) returns the second character and use that as the condition:

$ awk 'substr($0, 2, 1) == "." { ... }' file.txt 

What goes into { ... } is code that prepends $0 with a zero if the preceding condition is true:

$ awk 'substr($0, 2, 1) == "." { $0 = "0" $0 }' file.txt 

Then we just need to make sure that all lines are printed:

$ awk 'substr($0, 2, 1) == "." { $0 = "0" $0 } { print }' file.txt 

The condition substr($0, 2, 1) == "." may of course be changed into a regular expression too:

$ awk '/^.\./ { $0 = "0" $0 } { print }' file.txt 

Some people who thinks "shorter is always better" would write that as

$ awk '/^.\./ { $0 = "0" $0 } 1' file.txt 

(and probably also remove most spaces: awk '/^.\./{$0="0"$0}1' file.txt)

Alternatively, to solve it with sed instead of awk:

$ sed 's/^.\./0&/' file.txt 

When & occurs in the replacement part of the substitution command (s), it will be expanded to the part of the input line that matches the pattern part of the command.

The regular expression is the same as in the last awk variation and means "match all lines that starts with (^) an arbitrary character (.) followed by a literal dot (\.)".

added 15 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k
Loading
added 185 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k
Loading
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k
Loading