We help IT Professionals succeed at work.

Shell script to find a pattern and remove that specific line ?

93 Views
Last Modified: 2018-02-06
Hi Team,

I would like to find a patter sdb1,sdb2,sda1,sda2 etc..and need to remove the entire line. SO that i would like to get the dick details for only sda,sdb,sdc. Please help me on this. I tried many scenarios like below nothing worked out :( :(

#! /bin/bash

echo "`hostname -I|awk '{print $1}'`"

echo "------------------------"

RAM=`cat /proc/meminfo | grep -i MemTotal | awk '{$2=$2/1024/1024; print $2}'`
echo "RAM SIZE: ${RAM} MB"

CPUCORES=`cat /proc/cpuinfo | grep -i processor | wc -l`

echo "CPU Cores: ${CPUCORES}"
echo ""

echo "Disk Deatils:"
echo ""

echo "`lsblk -io KNAME,TYPE,SIZE,MODEL | grep -i "^sdd*"| sed '/\b[0-9]$\b/p'`"

Open in new window


devops@HKLVADEDM10 ~ $ ./hostdetails.sh
10.20.239.92
------------------------
RAM SIZE: 7.68766 MB
CPU Cores: 4

Disk Deatils:

sdb   disk   50G Virtual disk
sdb1  part   50G
sda   disk   50G Virtual disk
sda1  part  100M
sda2  part 49.9G
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Hi Bharath,

If you change this:
    echo "`lsblk -io KNAME,TYPE,SIZE,MODEL | grep -i "^sdd*"| sed '/\b[0-9]$\b/p'`"
to this:
    lsblk -io KNAME,TYPE,SIZE,MODEL | grep "^sd. "
then that will should give you want you want in this particular case.  Note the space after the dot!
But a more precise approach could be something like this:
    lsblk -io KNAME,TYPE,SIZE,MODEL | egrep -v "^sd[a-z][1-9]"

grep's '-i' switch is required only for case insensitive matching.
What were you trying to do when you grepped for "^sdd*"?  That's matching lines starting with "sd" plus 0 or more "d"s.
No need to "echo" that line, because it's already going to be sent to stdout.

Some notes for the rest of your script:
Lines like this:
    echo "CPU Cores: ${CPUCORES}"
    echo ""
can be abbreviated to this:
    echo -e "CPU Cores: ${CPUCORES}\n"
or even this:
    echo -e "CPU Cores: $CPUCORES\n"
if you want to reduce the number of lines in your script.

And this:
    CPUCORES=`cat /proc/cpuinfo | grep -i processor | wc -l`
can be abbreviated to this:
    CPUCORES=`grep -i processor /proc/cpuinfo | wc -l`
or possibly this, if "processor" appears in the cpuinfo file in lower case only:
    CPUCORES=`grep processor /proc/cpuinfo | wc -l`

tel2

Author

Commented:
Thanks a lot for your reply. But i need like below


devops@HKLVADEDM10 ~ $ lsblk -io KNAME,TYPE,SIZE,MODEL | egrep -i "^sd.*"
sdb   disk   50G Virtual disk
sdb1  part   50G
sda   disk   50G Virtual disk
sda1  part  100M
sda2  part 49.9G
devops@HKLVADEDM10 ~ $ lsblk -io KNAME,TYPE,SIZE,MODEL | egrep -i "^sd.*" | egrep -v "^s.*[0-9]$"
sdb   disk   50G Virtual disk
sdb1  part   50G
sda   disk   50G Virtual disk
sda1  part  100M
sda2  part 49.9G
devops@HKLVADEDM10 ~ $

I should search first  word which ends with number then have to ignore the entire line.
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
thanks
CERTIFIED EXPERT

Commented:
Thanks for the points, Bharath.

Please answer the questions in my previous post.  I refer to my sentences which end in '?'.

Thanks.
tel2

Author

Commented:
I used different patterns it worked out ... I keep your suggestion as my knowledge base appreciate your support

Author

Commented:
I used different patterns it worked out ... I keep your suggestion as my knowledge base appreciate your support
CERTIFIED EXPERT

Commented:
OK, but what did you use in the end?

Author

Commented:
I have grepped with part and ignored it and got only disk matched in it.
CERTIFIED EXPERT

Commented:
I mean what exact/complete command did you use?  Are you able to copy and paste it for me?
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.