Skip to main content
added explaining why sed only, removed '[~] #' to higlight the code
Source Link

Considering this file:

1 2 3 4 5 

I can extract the last 3 lines with this sed one-liner:

[~] #$ /bin/sed input.txt -e ':a;$q;N;4,$D;ba' 

But I would also like to add some characters (# ) in front of each line, so that the output will be like so:

# 3 # 4 # 5 

How can I modify the one-liner to do that? I mean, without adding another sedsed invocation (with | /bin/sed -e 's/^/# /').

I would prefer to use sed only for the same reasons of the original question, i.e. I have a system with limited binaries available.

Considering this file:

1 2 3 4 5 

I can extract the last 3 lines with this sed one-liner:

[~] # /bin/sed input.txt -e ':a;$q;N;4,$D;ba' 

But I would also like to add some characters (# ) in front of each line, so that the output will be like so:

# 3 # 4 # 5 

How can I modify the one-liner to do that? I mean, without adding another sed invocation (with | /bin/sed -e 's/^/# /').

Considering this file:

1 2 3 4 5 

I can extract the last 3 lines with this sed one-liner:

$ /bin/sed input.txt -e ':a;$q;N;4,$D;ba' 

But I would also like to add some characters (# ) in front of each line, so that the output will be like so:

# 3 # 4 # 5 

How can I modify the one-liner to do that? I mean, without adding another sed invocation (with | /bin/sed -e 's/^/# /').

I would prefer to use sed only for the same reasons of the original question, i.e. I have a system with limited binaries available.

Removed cat
Source Link
virtualdj
  • 177
  • 1
  • 8

Considering this file:

1 2 3 4 5 

I can extract the last 3 lines with this sed one-liner:

[~] # cat input.txt | /bin/sed input.txt -e ':a;$q;N;4,$D;ba' 

But I would also like to add some characters (# ) in front of each line, so that the output will be like so:

# 3 # 4 # 5 

How can I modify the one-liner to do that? I mean, without adding another sed invocation (with | /bin/sed -e 's/^/# /').

Considering this file:

1 2 3 4 5 

I can extract the last 3 lines with this sed one-liner:

[~] # cat input.txt | /bin/sed -e ':a;$q;N;4,$D;ba' 

But I would also like to add some characters (# ) in front of each line, so that the output will be like so:

# 3 # 4 # 5 

How can I modify the one-liner to do that? I mean, without adding another sed invocation (with | /bin/sed -e 's/^/# /').

Considering this file:

1 2 3 4 5 

I can extract the last 3 lines with this sed one-liner:

[~] # /bin/sed input.txt -e ':a;$q;N;4,$D;ba' 

But I would also like to add some characters (# ) in front of each line, so that the output will be like so:

# 3 # 4 # 5 

How can I modify the one-liner to do that? I mean, without adding another sed invocation (with | /bin/sed -e 's/^/# /').

Source Link
virtualdj
  • 177
  • 1
  • 8

Extract last n lines and prepend chars with sed one-liner

Considering this file:

1 2 3 4 5 

I can extract the last 3 lines with this sed one-liner:

[~] # cat input.txt | /bin/sed -e ':a;$q;N;4,$D;ba' 

But I would also like to add some characters (# ) in front of each line, so that the output will be like so:

# 3 # 4 # 5 

How can I modify the one-liner to do that? I mean, without adding another sed invocation (with | /bin/sed -e 's/^/# /').