AWK ORS Builtin Description Linux: How to use Output record separator field ?

In this post, we will look what is AWK ORS builtin variable and how to use it.

AWK ORS:- ORS stands for output record separator and it is used to replace the existing “/n” record separator with a new customized character sequence.

To understand this, lets have a look at an example or scenario.

1. Default awk print statement
By default, each line is treated as one record, and the default output record separator is “\n” i.e. new line character.

[root@nglinux ~]# cat /etc/passwd | awk '{print}'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
### Output truncated ###

2. Now setting Output record separator from “\n” to “~~~~~”

[root@nglinux ~]# cat /etc/passwd | awk '{ORS="~~~~~";print}'
root:x:0:0:root:/root:/bin/bash~~~~~bin:x:1:1:bin:/bin:/sbin/nologin~~~~~daemon:x:2:2:daemon:/sbin:/sbin/nologin~~~~~adm:x:3:4:adm:/var/adm:/sbin/nologin~~~~~lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin~~~~~sync:x:5:0:sync:/sbin:/bin/sync~~~~~shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown~~~~~halt:
### Output truncated ###

3. Now lets set the ORS bultin variable value to “### This is one passwd record” and see its interesting effect.

[root@nglinux ~]# cat /etc/passwd | awk '{ORS=" ### This is one passwd record \n";print}' | head -3
root:x:0:0:root:/root:/bin/bash ### This is one passwd record 
bin:x:1:1:bin:/bin:/sbin/nologin ### This is one passwd record 
daemon:x:2:2:daemon:/sbin:/sbin/nologin ### This is one passwd record 
[root@nglinux ~]# 

I hope your concept about ORS AWK builtin variable is now clear and now you can make use of it.

Do post your comment/suggestions below.

Leave a Reply

Your email address will not be published.