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
 


OP
A

atanere

Guest
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!
 
OP
J

JasKinasis

Guest
I think this is what the OP is after:
Code:
history | tail -8 | sed 's/^ [0-9]\+. //g'
 
OP
A

atanere

Guest
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! :)
 
OP
J

JasKinasis

Guest
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:
OP
A

atanere

Guest
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
 
OP
A

atanere

Guest
@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!
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top