LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-06-2013, 05:34 AM   #1
tripialos
Member
 
Registered: Apr 2012
Posts: 169

Rep: Reputation: Disabled
how to delete charachters after a specific word


Greetings

I think the solution to this is something to do with sed in combination to regex.

Say i i have the above line

hellodude=12345
hellowolrd=12:34:56

and i want to erase everything after helloworld= . So in other words i want to transform the above text to:

hellodude=12345
hellowolrd=


Is there any command that could achieve that?


thanks
 
Old 08-06-2013, 05:45 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,789

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
there is no such command, but you can write a small script (probably one-liner) to do that. What language do you prefer?
 
Old 08-06-2013, 06:03 AM   #3
tripialos
Member
 
Registered: Apr 2012
Posts: 169

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
there is no such command, but you can write a small script (probably one-liner) to do that. What language do you prefer?
bash shell should be fine i guess. Could you help how to do that?

thanks
 
Old 08-06-2013, 06:14 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In bash if you have the string in a variable, you can try parameter substitution to remove everything after the sign =, i.e.
Code:
var='hellowolrd=12:34:56'
var=${var/=*/=}
this in practice substitutes the = sign and the rest of the string with an equal sign alone. On the other hand if you want to change the content of a file the solution is sed (as you guessed). Something like this should do the trick:
Code:
sed -i '/hellowolrd=/s/=.*/=/'
which is practically the same as above.
 
1 members found this post helpful.
Old 08-06-2013, 06:21 AM   #5
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
...or if you have the text in a file you could do

Code:
sed -e s/^helloworld=.*/helloworld=/g  /path/to/input.txt
It just takes each line that starts with "helloworld=" followed by any character sequence and replaces it with "helloworld="

Last edited by joe_2000; 08-06-2013 at 06:25 AM. Reason: Forgot the "beginning of line" symbol
 
2 members found this post helpful.
Old 08-06-2013, 06:55 AM   #6
tripialos
Member
 
Registered: Apr 2012
Posts: 169

Original Poster
Rep: Reputation: Disabled
niiice both worked form me like charm!!! Thanks guys
 
  


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
How to convert specific word into specific colors in console shell? arfal Linux - Server 3 05-10-2012 12:40 AM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 06:35 AM
How to delete all files with specific word in filename? moviecarpet Programming 13 05-26-2011 08:44 AM
[SOLVED] Delete range word to word with sed when all you have is one line subby80 Linux - Enterprise 16 09-02-2010 05:04 AM
How to delete files tat contains a specific word?? linuq Linux - Newbie 4 06-30-2008 12:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:19 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
Open Source Consulting | Domain Registration