How to pick complete line while working in Loop under Unix or Linux ?

Today we will see one of the useful tip in Linux.

That is when we try to search complete line text from a long file, then it take individual words and does not take individual lines.

I. First create a file with long lines like below.

[user@NGEL-user test]$ head final
Test Event Monitor : PING Status Critical for ngelinux16 - Site Cluster nfNGEL01 CLuster (alert_au)
host1.global.ngelinux.com_Monitor : Status Critical for NGEID on Cluster cluster_DBNGELdsg_DBNGELmdsg (alert_au)
host1.global.ngelinux.com_Monitor : Status Critical for NGEID on Cluster cluster_nfNGEL01_nfmaster00 (alert_au)
host1.global.ngelinux.com_Monitor : Status Critical for Job Submission on Cluster cluster_nfNGEL01_nfmaster00 (alert_au)
host1.global.ngelinux.com_Monitor : Status Critical for Job Submission on Cluster cluster_euNGEL01_euNGELm01 (alert_au)
host1.global.ngelinux.com_Monitor : Status Critical for Cluster Status on Cluster cluster_DBNGELps_DBcvl-DBNGELmimg (alert_au)
host1.global.ngelinux.com_Monitor : Status Critical for Bqueue Status on Cluster cluster_nfNGEL01_nfmaster00 (alert_au)
host1.global.ngelinux.com_Monitor : Status Bjob Status Critical for ip-172-17-11-250 - Site AWS_Ngoogle_Cloud Cluster awsNgoogleIPG01 (alert_au)
DBf-Monitor.ngelinux.com_Metric 'status' changed status from 'good' to 'error' (alert_au)
Script Execution failed on blrgoogle-Monitor.ngelinux.com server (contactjiming2@ngelinux.com)
[user@NGEL-user test]$

 

II. Default behavior in case of long lines: Picking up words not lines.

[user@NGEL-user test]$ for i in `cat final`; do echo $i ; done | head
Test
Event
Monitor
:
PING
Status
Critical
for
ngelinux16
-
[user@NGEL-user test]$ echo $IFS

[user@NGEL-user test]$

 

III. Expected Behavior: To get complete lines.

[user@NGEL-user test]$ IFS=$'\n'; for i in `cat final`; do echo $i ; done
Test Event Monitor : PING Status Critical for ngelinux16 - Site Cluster nfNGEL01 CLuster (alert_au)
host1.global.ngelinux.com_Monitor : Status Critical for NGEID on Cluster cluster_DBNGELdsg_DBNGELmdsg (alert_au)
host1.global.ngelinux.com_Monitor : Status Critical for NGEID on Cluster cluster_nfNGEL01_nfmaster00 (alert_au)
host1.global.ngelinux.com_Monitor : Status Critical for Job Submission on Cluster cluster_nfNGEL01_nfmaster00 (alert_au)
host1.global.ngelinux.com_Monitor : Status Critical for Job Submission on Cluster cluster_euNGEL01_euNGELm01 (alert_au)
host1.global.ngelinux.com_Monitor : Status Critical for Cluster Status on Cluster cluster_DBNGELps_DBcvl-DBNGELmimg (alert_au)
host1.global.ngelinux.com_Monitor : Status Critical for Bqueue Status on Cluster cluster_nfNGEL01_nfmaster00 (alert_au)
host1.global.ngelinux.com_Monitor : Status Bjob Status Critical for ip-172-17-11-250 - Site AWS_Ngoogle_Cloud Cluster awsNgoogleIPG01 (alert_au)
DBf-Monitor.ngelinux.com_Metric 'status' changed status from 'good' to 'error' (alert_au)
Script Execution failed on blrgoogle-Monitor.ngelinux.com server (contactjiming2@ngelinux.com)

 

 

The trick here is to set the internal field separator as $’\n’ i.e. the new line character falls at the end of the line.
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments