LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-06-2011, 11:14 PM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
Cutting fields from lines with multiple spaces


Please see the following code, between "status" and "OK" exists many spaces, I want to get
Code:
status                        OK
. how to ignore multi spaces? If tab exists in the spaces, how to ignore it ?
Is there other commands can replace cut?
Code:
[river@localhost CLI008003]$ echo 'drv status                        OK !!'| cut -d " " -f 2-3
status 
[river@localhost CLI008003]$ echo 'drv status                        OK !!'| cut -d " " -f 2-
status                        OK !!
[river@localhost CLI008003]$ echo 'drv status                        OK !!'| cut -d " " -f 3
 
Old 09-06-2011, 11:56 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,765

Rep: Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050
I presume the idea is to retain the whitespace between these two words? How about:
Code:
echo 'drv status                        OK !!'| sed -rn 's/.*(status.*OK).*/\1/p'
 
Old 09-07-2011, 09:38 AM   #3
tbrand
Member
 
Registered: Jul 2006
Location: Toronto, Canada
Distribution: gentoo
Posts: 33

Rep: Reputation: 17
A very simple solution to your problem is awk:

Code:
echo 'drv status                        OK !!'| awk '{print $2 "         " $3}'
 
Old 09-07-2011, 10:22 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,765

Rep: Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050
Actually tbrand, your solution is guessing how much white space is required.
 
Old 09-07-2011, 10:31 AM   #5
tbrand
Member
 
Registered: Jul 2006
Location: Toronto, Canada
Distribution: gentoo
Posts: 33

Rep: Reputation: 17
Very true, grail.

It's hard to tell from the question if it is important that the number of white spaces be preserved; I assumed that it was not important and that it was more important to duplicate the actual status. The sed solution will ignore lines that report some other status than ``OK''.
 
Old 09-07-2011, 10:39 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
What about
Code:
echo 'drv status                        OK !!' | awk '{print substr($0,index($0,"status"))}'
This assumes there are no other fields after OK that have to be left out. Otherwise you can refine the sed solution including any number of white spaces before and after the 2nd field.
 
Old 09-07-2011, 10:51 AM   #7
tbrand
Member
 
Registered: Jul 2006
Location: Toronto, Canada
Distribution: gentoo
Posts: 33

Rep: Reputation: 17
That's a good one.

One may also add a pattern to ignore any non-status lines because presumably this is meant to filter some log file. Like this:

Code:
echo 'drv status                        OK !!' | awk '/ status /{print substr($0,index($0,"status"))}'
 
  


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
AWK - why fields go to seperate lines? korhan Linux - Newbie 2 03-01-2007 04:21 PM
fscanf with optional fields, spaces? phyx Programming 1 01-26-2007 06:16 PM
Vi question. Cutting a specific lines blackzone Linux - General 1 07-19-2006 07:10 PM
Unique lines based on specific fields. carl.waldbieser Programming 6 08-21-2005 03:26 PM
Joining multiple lines and summing fields elconde Programming 1 02-13-2004 11:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration