[bash] remove some entries from output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash] remove some entries from output
# 1  
Old 12-07-2018
[bash] remove some entries from output

Hello everyone,

I have the following script

Code:
sudo lshw -class disk | egrep -E "product|logical name|size:" | awk ' {print;} NR % 3 == 0 { print ""; }'

This is the current output on my machine

Code:
       product: BUP Slim SL 
       logical name: /dev/sdh
       size: 1863GiB (2TB)

       product: STORAGE DEVICE
       logical name: /dev/sdc
          logical name: /dev/sdc

       product: STORAGE DEVICE
       logical name: /dev/sdd
          logical name: /dev/sdd

       product: STORAGE DEVICE
       logical name: /dev/sde
          logical name: /dev/sde

       product: STORAGE DEVICE
       logical name: /dev/sdf
          logical name: /dev/sdf

       product: STORAGE DEVICE
       logical name: /dev/sdg
          logical name: /dev/sdg

       product: OCZ-VECTOR150
       logical name: /dev/sda
       size: 223GiB (240GB)

       product: Samsung SSD 860
       logical name: /dev/sdb
       size: 465GiB (500GB)

       product: BD-RE  BH12LS35
       logical name: /dev/cdrom
       logical name: /dev/sr0

       product: DRW-24B1ST   a
       logical name: /dev/cdrw
       logical name: /dev/dvd

       logical name: /dev/dvdrw
       logical name: /dev/sr1

But, I would like the following

Code:
       product: OCZ-VECTOR150
       logical name: /dev/sda
       size: 223GiB (240GB)

       product: Samsung SSD 860
       logical name: /dev/sdb
       size: 465GiB (500GB)

So my question is... How do I achieve the the above output and getting rid of everything else?
# 2  
Old 12-07-2018
You need jq installed for this. The maintainers describe jq as a sed-like tool for json files.
Code:
$ sudo lshw -class disk -json | jq -c . | grep size | jq '{product,logicalname,size}'
{                         
  "product": "Backup+  Desk",
  "logicalname": "/dev/sda",
  "size": 3000592977920
}
{
  "product": "Micron_M510_2.5_",
  "logicalname": "/dev/sdb",
  "size": 256060514304
}

The first instance of jq minimises the output to one device per line. sed is then used to reduce the output to only devices with a size field. jq is used once again to pull out the product name, logical name and size for each remaining device. You can then use awk or some other tool to reformat the output into something more palatable, if you desire.

jq will probably be in your Linux repository and should be easy to install.

Andrew
# 3  
Old 12-07-2018
Is there any feature in the unfiltered lshw output that allows us to discriminate between desired and undesired devices? In your above sample, that might be size, but that would also output /dev/sdh.


Try piping through
Code:
awk '/product/ {T1 = $0; T2 = ""} /logical name/ {T2 = $0}  /size:/ {print T1; print T2; print $0; print ""}'


Last edited by RudiC; 12-07-2018 at 07:40 AM..
These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 12-07-2018
Quote:
Originally Posted by RudiC
Is there any feature in the unfiltered lshw output that allows us to discriminate between desired and undesired devices? In your above sample, that might be size, but that would also output /dev/sdh.
Oops! Missed that! I agree that there should be something better to filter out on than size. It was simply the one that worked for my system.

Andrew
# 5  
Old 12-07-2018
Thank you so much for your help guys!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash to remove find and remove specific extension

The bash below executes and does find all the .bam files in each R_2019 folder. However set -x shows that the .bam extension only gets removed from one .bam file in each folder (appears to be the last in each). Why is it not removing the extension from each (this is $SAMPLE)? Thank you :). set... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Bash - remove duplicates without sort

I need to use bash to remove duplicates without using sort first. I can not use: cat file | sort | uniq But when I use only cat file | uniq some duplicates are not removed. (4 Replies)
Discussion started by: locoroco
4 Replies

3. Shell Programming and Scripting

Bash, remove numbers after colon

Hello All, I was wondering if someone might know how to do this. I have a word list that is format like the example below. I need to take away the :number after that... is there some kind of command I could use to remove them? 123456:5562 password:1507 123456789:989 qwerty:877... (7 Replies)
Discussion started by: 3therk1ll
7 Replies

4. Shell Programming and Scripting

bash - remove duplicates

I need to use a bash script to remove duplicate files from a download list, but I cannot use uniq because the urls are different. I need to go from this: http://***/fae78fe/file1.wmv http://***/39du7si/file1.wmv http://***/d8el2hd/file2.wmv http://***/h893js3/file2.wmv to this: ... (2 Replies)
Discussion started by: locoroco
2 Replies

5. Shell Programming and Scripting

How to remove '^[[00m' in bash file?

Hi, This should be a simple one: I run the following commands in bash and ksh respectively but got differenant results: # ls -l /var/log > /tmp/a # vi /tmp/a In bash shell, I got: ^ In ksh, I got: total 828552 -rw-r----- 1 root root 189 Aug 9 00:00 acpid -rw-r----- 1 root... (7 Replies)
Discussion started by: aixlover
7 Replies

6. Shell Programming and Scripting

How to remove comments from a bash script?

I would like to remove comments from a bash script. In addition, I would like to remove lines that consist of only white spaces, and to remove blank lines. #!/bin/bash perl -pe 's/ *#.*$//g' $1 | grep -v ^]*$ | perl -pe 's/ +/ /g' > $2 # # $1 INFILE # $2 OUTFILE The above code... (10 Replies)
Discussion started by: LessNux
10 Replies

7. Shell Programming and Scripting

remove ] followed by newline in bash

I need to remove ] followed by newline character to convert lines like: line1] line2 into: line1line2 (4 Replies)
Discussion started by: locoroco
4 Replies

8. Shell Programming and Scripting

Selective remove awk-bash

Hi I have a folder that contain up to 1000 subfolder. Genereally i need just 2 or 3 of them and i can know apriori the name of the subfolder i want to keep. I'd like to erase all the unnecessary subfolders. I've tried with: ls -l | awk '$8 !~ /name_fold/{print "rm -r " $8}'| sh and it... (4 Replies)
Discussion started by: Dedalus
4 Replies

9. Shell Programming and Scripting

Compare entries in two files, output one of them in bash

I have two files which are of the following format: File1: Unnamed Unnamed Boston Unnamed New_York Unnamed File2: San_Francisco Chicago Portland Austin Orlando Los_Angeles In the case where an entry in File1 is "Unnamed", I want to output the name from the same line in File2.... (5 Replies)
Discussion started by: forthekill
5 Replies

10. Shell Programming and Scripting

Why generate "ash and bash" different output for same bash script?

Hi, For my bash script, terminal with bash is generate an OK output and program works right. already, terminal with ash have "line 48: syntax error: Bad substitution" output and program don't work. :confused: (0 Replies)
Discussion started by: s. murat
0 Replies
Login or Register to Ask a Question