LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   truncate shell command (http://www.linuxquestions.org/questions/programming-9/truncate-shell-command-796165/)

puth 03-17-2010 08:33 PM

truncate shell command
 
In a file i have to grep for a particular word and cut 8 characters of that word and replace the last characters with space if it is _1

Eg: HP4350_1

i did grep|cut -c 2-9|
but didn't know how to truncate the last two characters if its '_1'.
i used tr '[_1] '[ ]'.but it replaced all the characters where there is a 'underscore' and 1 instead of'_1' together.

troop 03-17-2010 08:44 PM

Code:

sed 's/_1//'

jlinkels 03-17-2010 08:46 PM

try sed s/_1$/" "/

jlinkels

primerib 03-17-2010 08:59 PM

sed s/_1$//

No quotes.

jlinkels 03-18-2010 05:52 AM

Quote:

Originally Posted by puth (Post 3902574)
...and replace the last characters with space if it is _1

The OP said he wanted a space, so there must be quotes or the space must be escaped.

jlinkels

primerib 03-18-2010 10:49 AM

Quote:

Originally Posted by jlinkels (Post 3903001)
The OP said he wanted a space, so there must be quotes or the space must be escaped.

jlinkels

That he did, I missed that somehow..


All times are GMT -5. The time now is 12:25 AM.