LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-17-2010, 09:33 PM   #1
puth
LQ Newbie
 
Registered: Feb 2010
Posts: 19

Rep: Reputation: 0
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.
 
Old 03-17-2010, 09:44 PM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
Code:
sed 's/_1//'

Last edited by troop; 03-17-2010 at 09:46 PM.
 
Old 03-17-2010, 09:46 PM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
try sed s/_1$/" "/

jlinkels
 
Old 03-17-2010, 09:59 PM   #4
primerib
Member
 
Registered: Mar 2010
Posts: 38

Rep: Reputation: 19
sed s/_1$//

No quotes.
 
Old 03-18-2010, 06:52 AM   #5
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
Quote:
Originally Posted by puth View Post
...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
 
Old 03-18-2010, 11:49 AM   #6
primerib
Member
 
Registered: Mar 2010
Posts: 38

Rep: Reputation: 19
Quote:
Originally Posted by jlinkels View Post
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..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can I run a command on another shell without changing the current shell? quanba Other *NIX 3 04-05-2010 01:19 PM
LXer: The Linux Command Shell For Beginners: Fear Not The Command Line! LXer Syndicated Linux News 0 12-22-2008 07:30 PM
How to run root privileged Linux command as normal user via shell shell tcegrid Linux - Newbie 1 06-23-2008 04:38 PM
truncate help munna_dude Programming 8 10-18-2007 10:24 PM
truncate command butthead Linux - General 1 05-14-2002 04:40 AM


All times are GMT -5. The time now is 12:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration