sed basic

G

guptaankit.0010

Guest
bash4.3:$history | tail -8 | sed 's/.* //' or bash4.3:$history | tail -8 | sed 's/.*\t//'


this command is supposed to delete leading sequence numbers generated in history command...bit it's not working

pls help and bear with me as i m new to linux
 


School work assignment? Folks here don't want to do your homework for you... that would not help you to learn. I can tell you that I am NOT a coder myself. I'm really, really bad at it. But with what you provided as a start, and with the sed command guide I found below, I was able to find one solution to your problem. And I do know that with programming, there is always more than one solution. Think it through... you can do it!

http://www.computerhope.com/unix/used.htm

Good luck!
 
I think this is what the OP is after:
Code:
history | tail -8 | sed 's/^ [0-9]\+. //g'
 
I don't think so, but maybe I didn't get his criteria correct. I think he wants to suppress the line numbers from the history output. Your code still shows the line numbers. While googling around this morning I did find another solution, so that's at least two using sed. Come on @guptaankit.0010 -- you can do this! :)
 
The regex I used in the sed command should strip out the line numbers and whitespace at the start of the lines output by history|tail -8. Which is what the OP appears to be asking about.

It works on my machine at least. Not sure why you're seeing anything different! :/

Another alternative would be to use cut:
Code:
history | tail -8 | cut -c 8-
But personally, I think sed is the better option! Probably also possible using awk too.
 
Last edited:
Not sure why I see different either. I'm using bash 4.3.29 as the shell, and I get line numbers with your first code example. The cut command works, but I don't know if his school assignment is to find any method, or if he must use sed. Yes, I found at least one awk solution also. I'm not a programmer... but I can google like a mad man! :D
 
@guptaankit.0010 : Have you found the answer, or solved it yet? If it is still a struggle, you might "google like a mad man" like I do! ;) Hint: Refining your search terms can help a lot! Maybe try something like, "suppress line numbers in bash history" and see what comes up. Let us know what solution you find, and then I'll show you the 2nd one that I found too.

Good luck, and Happy New Year!
 

Members online


Latest posts

Top