Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Reading a file line by line and print required lines based on pattern Post 303040524 by balu1234 on Wednesday 30th of October 2019 04:44:23 PM
Old 10-30-2019
Reading a file line by line and print required lines based on pattern

Hi All,

i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field.

i am using centos 7 Operating system.

want to read below file.

Code:
[root@matilda final]# cat /tmp/d5
NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="" MOUNTPOINT=""
NAME="/dev/sda1" TYPE="part" SIZE="500M" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda" MOUNTPOINT="/boot"
NAME="/dev/sda2" TYPE="part" SIZE="29.5G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda" MOUNTPOINT=""
NAME="/dev/mapper/centos-root" TYPE="lvm" SIZE="56.5G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda2" MOUNTPOINT="/"
NAME="/dev/mapper/centos-swap" TYPE="lvm" SIZE="3G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda2" MOUNTPOINT="[SWAP]"
NAME="/dev/sda3" TYPE="part" SIZE="30G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda" MOUNTPOINT=""
NAME="/dev/mapper/centos-root" TYPE="lvm" SIZE="56.5G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda3" MOUNTPOINT="/"
NAME="/dev/sr0" TYPE="rom" SIZE="603M" OWNER="root" GROUP="cdrom" MODE="brw-rw----" PKNAME="" MOUNTPOINT=""

Desired output :

Code:
Mountpoint:"/boot"
 Type:"part"

Mountpoint:"/"
 Type:"lvm"
Mountpoint:"[SWAP]"
 Type:"lvm"

Mountpoint:"/"
 Type:"lvm"

i am using below shell script , but i am unable to ignore the lines which contains Mountpoint value is empty.


My Script :
Code:
#!/bin/bash
FILE=/tmp/d5
while read LINE; do
mountpoint=$(echo "$LINE" | awk -F " " '{print$8}'|awk -F "=" '{print$2}')

disk=$(echo "$LINE" | awk -F " " '{print$1}'|awk -F "=" '{print$2}')
Type=$(echo "$LINE" | awk -F " " '{print$2}'|awk -F "=" '{print$2}')

if [ "$mountpoint" != "" ];then

      echo -e "Mountpoint:$mountpoint \n Type:$Type"

fi
done < $FILE

i am getting below output ;

Code:
Mountpoint:""
 Type:"disk"
Mountpoint:"/boot"
 Type:"part"
Mountpoint:""
 Type:"part"
Mountpoint:"/"
 Type:"lvm"
Mountpoint:"[SWAP]"
 Type:"lvm"
Mountpoint:""
 Type:"part"
Mountpoint:"/"
 Type:"lvm"
Mountpoint:""
 Type:"rom"

Can someone please help me on this issue.

Last edited by jim mcnamara; 10-30-2019 at 06:34 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

2. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

3. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

4. Shell Programming and Scripting

grep based on pattern in a line and print the column before that

$ cat file.log Message Number = : Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length I have the above line in the... (4 Replies)
Discussion started by: kalidass
4 Replies

5. Shell Programming and Scripting

Sed print range of lines between line number and pattern

Hi, I have a file as below This is the line one This is the line two <\XMLTAG> This is the line three This is the line four <\XMLTAG> Output of the SED command need to be as below. This is the line one This is the line two <\XMLTAG> Please do the need to needful to... (4 Replies)
Discussion started by: RMN
4 Replies

6. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

7. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

8. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

9. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

10. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies
All times are GMT -4. The time now is 04:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy