LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-11-2010, 09:57 AM   #1
puth
LQ Newbie
 
Registered: Feb 2010
Posts: 19

Rep: Reputation: 0
using cut


hello,

i have some thing like the below in a file.

linux example1
linux example10
linux example32
read error
linux example2

i need to pick all the words that are just after linux and place them in another file.I tried to use cut -d '' -f2 but at some places where the line is the not started with linux i get the output as below

example1
example10
example32
error
example2

i want the output as
example1
example10
example32
example2.

how to modify my cut statement?
 
Old 03-11-2010, 10:07 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I think you should use awk, that way you can test if the first word is 'linux', and if it is, then you print the second word.

See:
http://www.grymoire.com/Unix/Awk.html
 
Old 03-11-2010, 10:11 AM   #3
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
grep, sed , awk , perl , shell -- too many choices.....

since you already have the 'cut' part down, you could simply add a grep before it.
 
Old 03-11-2010, 01:30 PM   #4
puth
LQ Newbie
 
Registered: Feb 2010
Posts: 19

Original Poster
Rep: Reputation: 0
i used grep "linux"|awk '{print $2}' and got the desired output.
 
Old 03-11-2010, 02:55 PM   #5
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
But, that isn't quite the solution, because linux could be second or third and it would still grep that line.

Maybe

Code:
awk '{ if ($1 == "linux") print $2 }' input.txt
 
  


Reply



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] using cut puth Programming 6 02-19-2010 08:31 AM
help with cut command using find. Cut last 8 characters leaving the rest ncsuapex Programming 4 09-16-2009 08:55 PM
How to use Cut ZAMO Linux - General 3 09-25-2008 11:39 AM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
Cut Help metallica1973 Programming 67 12-20-2007 04:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:48 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