LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 04-10-2009, 10:50 AM   #1
ArOnaXx
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Rep: Reputation: 0
Question Grep or Sed ? How can I use it ?


Hello.
First, my english is not very good, sorry, but try understand me please.

So...

I have one question, and I can't resolve it. I used google but I had no response for my question.

I created one file: "file.txt", and put it there:

status:active
color:blue
mode:normal

and now I want to get everything after the character ":"

example:

cat file.txt | grep -everything_after ":"

but I don't know how I do, if I use grep or if I use sed.

Please help me, thanks.
 
Old 04-10-2009, 11:10 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
sed 's/[^:]*:/:/1' filename

This won't do you any good unless you understand how it works.....Take a look here:
http://www.grymoire.com/Unix/Sed.html

And got to http://tldp.org and get the Bash Guide for Beginners
 
Old 04-10-2009, 11:14 AM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
grep would not be the easiest tool to use for this - you should look at cut or awk instead:
Code:
cut -d: -f2 file.txt # output field number 2, fields separated by colon
awk -F: '{print $2}' file.txt
Here, I would choose cut (awk is more flexible, but more complex). You could do it in grep or sed, of course:
Code:
grep -o '[^:]*$' file.txt
sed 's/\(.*\)\(:\)\(.*\)/\3/'
... but these are not quite so readable
 
Old 04-10-2009, 11:40 AM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Some other solutions:
  1. gawk 'BEGIN {FS=":";} (print $2;}' file.txt
  2. cut -d: -f2 file.txt
But grep only prints matched lines, not parts of them, so it's not appropriate for your stated problem.

However, if you wanted to print the string after the colon only when the line contains a colon, something like this could work:
grep ":" file.txt | cut -d: -f2

Note that all of these solutions assume that there is no more than one ":" in any line.
 
Old 04-10-2009, 11:55 AM   #5
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Quote:
Originally Posted by PTrenholme View Post
But grep only prints matched lines, not parts of them, so it's not appropriate for your stated problem.
Depends on the version of grep - GNU grep (at least in recent versions) has an "-o" flag that only prints the part of the line matched by the expression, a feature lacking in the version on a Solaris machine that I work with.
 
Old 04-10-2009, 12:06 PM   #6
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by ArOnaXx View Post
Hello.
First, my english is not very good, sorry, but try understand me please.

So...

I have one question, and I can't resolve it. I used google but I had no response for my question.

I created one file: "file.txt", and put it there:

status:active
color:blue
mode:normal

and now I want to get everything after the character ":"

example:

cat file.txt | grep -everything_after ":"

but I don't know how I do, if I use grep or if I use sed.

Please help me, thanks.
If you only have those entries you can do...

Code:
awk -F':' '{print $2}'
 
Old 04-12-2009, 02:08 PM   #7
ArOnaXx
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Original Poster
Rep: Reputation: 0
GOT IT!
Thanks for all
I really appreciate
 
  


Reply

Tags
character, grep, sed



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
sed/grep new lines in text hk20 Linux - Newbie 2 09-13-2008 06:47 PM
grep -B using sed w3bd3vil Programming 3 08-11-2008 06:37 PM
how to use grep/sed tnik Linux - General 3 11-06-2007 07:51 PM
I need some help with SED and/or GREP... TheEngineer Linux - Newbie 1 02-02-2007 08:17 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

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

All times are GMT -5. The time now is 07:56 AM.

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