1

under command :gpart show

i get the output 
=>      34  31457213  ada0  GPT  (15G)
        34       128     1  freebsd-boot  (64k)
       162  29360000     2  freebsd-ufs  (14G)
  29360162   1572864     3  freebsd-swap  (768M)
  30933026    524221        - free -  (10GB)

how can i extract just 14G from 3rd line and 10GB from last line?

the 14/10GB values are never the same, which makes my work harder to do.

i manage to get some results using

cat test | sed 's/\|/ /' | awk '{print $6}{print $5}' but i still have to extract just that info,

any ideas? please thanks.

2
  • 1
    :gpart show |awk 'NR == 3 || NR == 5 { print $NF} ' |sed "s/(//;s/)//" Commented Oct 30, 2016 at 4:07
  • If you specifically want the freebsd-ufs partition, you should filter on that rather than hoping it will always be line 3... Commented Oct 30, 2016 at 9:15

3 Answers 3

2

With awk:

awk -F '[()]' 'NR==3 {print $(NF-1)} END{print $(NF-1)}' file.txt
  • -F '[()]' sets the field delimiter as ( or )

  • NR==3 {print $(NF-1)} prints the desired field for the third line

  • END{print $(NF-1)} prints the desired field for the last line

Example:

% cat file.txt
    34  31457213  ada0  GPT  (15G)
        34       128     1  freebsd-boot  (64k)
       162  29360000     2  freebsd-ufs  (14G)
  29360162   1572864     3  freebsd-swap  (768M)
  30933026    524221        - free -  (10GB)

% awk -F '[()]' 'NR==3 {print $(NF-1)} END{print $(NF-1)}' file.txt
14G
10GB
Sign up to request clarification or add additional context in comments.

Comments

2

You can try below solution -

vipin@kali:~$ cat kk.txt
     34  31457213  ada0  GPT  (15G)
        34       128     1  freebsd-boot  (64k)
       162  29360000     2  freebsd-ufs  (14G)
  29360162   1572864     3  freebsd-swap  (768M)
  30933026    524221        - free -  (10GB)
   vipin@kali:~$ awk -F'\(|\)' 'NR==3 || NR==5 {print $(NF -1)}' kk.txt
14G
10GB

explanation -

use escape character \ to use () and | for multiple Field seperator and NR to select multiple lines and NF-1 to print last column when () is field seperator.

Comments

1
$ cat file 
      34  31457213  ada0  GPT  (15G)
        34       128     1  freebsd-boot  (64k)
       162  29360000     2  freebsd-ufs  (14G)
  29360162   1572864     3  freebsd-swap  (768M)
  30933026    524221        - free -  (10GB)
$ egrep -o '\([0-9]+\w+\)$' file | sed -n '3p;5p'
(14G)
(10GB)

matches regex '\([0-9]+\w+\)$' i.e 1 or more digits followed by 1 or more word chars, both contained in round brackets and using sed to print 3rd and 5th line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.