LinuxQuestions.org
Help answer threads with 0 replies.
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 06-10-2016, 09:28 AM   #1
jegadezz
Member
 
Registered: Oct 2015
Location: Bangalore,India
Distribution: Fedora,Ubuntu
Posts: 32

Rep: Reputation: Disabled
Extract specific words from a line by using Shell


I hope someone here can help me. I have a line in a text file looking like this:

Disk /dev/mmcblk0: 15.7 GB, 15737028608 bytes, 30736384 sectors

I want to extract only the Size of mmcblk0.

I would prefer the solution being bash using sed or grep.

Thank you in advance,
 
Old 06-10-2016, 09:54 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by jegadezz View Post
I hope someone here can help me. I have a line in a text file looking like this:

Disk /dev/mmcblk0: 15.7 GB, 15737028608 bytes, 30736384 sectors

I want to extract only the Size of mmcblk0. I would prefer the solution being bash using sed or grep.
Ok..we'll be glad to HELP you, so what have you done/tried to do this?? Read the "Question Guidelines" in my posting signature...asking us to do this for you is fairly rude, as is even specifying what you want it done IN.

To start, read the man page on grep, then read the man page on either awk or cut. You can build your own solution from those.

EDIT:
Much like some of your other threads, where you simply asked for a handout:
http://www.linuxquestions.org/questi...in-4175574824/
http://www.linuxquestions.org/questi...er-4175576806/
http://www.linuxquestions.org/questi...nt-4175580501/

Last edited by TB0ne; 06-10-2016 at 09:58 AM.
 
Old 06-10-2016, 10:12 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Have to agree here ... at least have a go
 
Old 06-10-2016, 10:23 AM   #4
jegadezz
Member
 
Registered: Oct 2015
Location: Bangalore,India
Distribution: Fedora,Ubuntu
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thank You TB0ne. you Push me to do my self and Finally I found the trick by own.

fdisk -l |grep -w "/dev/mmcblk0" |cut -d':' -f2|cut -c1-5
 
Old 06-10-2016, 10:28 AM   #5
dab1414
Member
 
Registered: May 2011
Location: OK, USA
Distribution: Slackware 14.1_64
Posts: 76

Rep: Reputation: 52
I haven't used sed much, but curious if this would this work?
Code:
fdisk -l | sed -n '/mmcblk0:/,/,/p'
 
Old 06-10-2016, 01:10 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by dab1414 View Post
I haven't used sed much, but curious if this would this work?
Code:
fdisk -l | sed -n '/mmcblk0:/,/,/p'
You don't have to be curious, try and see what you get? The only thing would be to first check the output of the fdisk command as you may not have an entry with 'mmcblk0' in it

@OP - your current solution assumes you already know the length of the string which refers to your size information, so you might get returned ' 15.7', but this gives you no real
idea of the size as there is no measurement, ie. MB,GB,TB ...

Knowing the format of the data I would use sed or awk as a quick and easy way to get the detail:
Code:
fdisk -l | sed -r '/mmcblk0:/s/^.*: ([^,]*).*/\1/'

fdisk -l | awk -F"[, ]" '/mmcblk0:/{print $3,$4}'
 
1 members found this post helpful.
  


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] get two/more specific words on a line and print next few lines Kashif_Bash Programming 11 04-26-2012 12:15 AM
Awk to extract phrase between two words on a line? grob115 Programming 12 05-26-2010 09:46 PM
copy files containing specific words in a specified line abenmao Linux - Newbie 5 08-28-2008 09:04 AM
How do I extract characters from several words on a line? MheAd Linux - Newbie 22 06-23-2008 10:22 PM

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

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