0

I am going through a tutorial on awk which requires using '.awk' file. I am having issues going forward with it as awk is not working as expected. Here is the code I am using. Contents of the virt.awk file

BEGIN { RS="\n\n" ; }
$0 ~ search { print }

Now the expected result is I search for a pattern and the particular Record should be displayed. But when I try the following I get the entire file displayed.

Command I am using:

awk -f virt.awk search=domain1 virtualhosts.conf

No matter what I use in the search term, I get the entire file printed. All entries of domain1, domain2 domain3 etc Could someone please help?

Note: it is just a copy of virtualhosts.conf file and it is preformatted to have exactly the format i.e. blank line after each entry and no additional blank lines in between.

Contents of virtualhost.conf: (and the result I am getting always):

<VirtualHost 172.20.30.40:80>
DocumentRoot "/www/domain1"
ServerName www.domain1.com
#Other options
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
DocumentRoot "/www/domain2"
ServerName www.domain2.com
#Other options
</VirtualHost>

<VirtualHost 172.20.30.50:80>
DocumentRoot "/www/domain3-80"
ServerName www.domian3.org
#Other options
</VirtualHost>

<VirtualHost 172.20.30.50:8080>
DocumentRoot "/www/example2-8080"
ServerName www.example.org
#Other options
</VirtualHost>

Additional Details: I found that I was using mawk so I installed gawk but I still get the same result. I am using pop-os 20.04 and dash by the way (if that affects anything)

user@pop-os:~/linuxwork$ which awk
/usr/bin/awk
user@pop-os:~/linuxwork$ ll /usr/bin/awk
lrwxrwxrwx 1 root root 21 Mar 30 21:10 /usr/bin/awk -> /etc/alternatives/awk*
user@pop-os:~/linuxwork$ ll /etc/alternatives/awk
lrwxrwxrwx 1 root root 13 Aug  6 20:21 /etc/alternatives/awk -> /usr/bin/gawk*
user@pop-os:~/linuxwork$
1
  • works for me with gawk and mawk...
    – ilkkachu
    Commented Aug 6, 2021 at 15:28

1 Answer 1

4

You almost certainly have DOS line endings in your input file as then there'd be no \n\ns in the file, it'd all be \r\n\r\ns. Change RS="\n\n" to RS="" (which is what you really should be using anyway for portability to all awks) or RS="\r\n\r\n" or RS="\r?\n\r?\n" and see what output you get.

Also run cat -Ev virtualhost.conf and look for ^M$ at the end of each line indicating a Carriage Return (CR = \r = ^M) followed by a Line Feed (LF = \n = $).

See why-does-my-tool-output-overwrite-itself-and-how-do-i-fix-it for more information on DOS line endings and how to address them when you want to run Unix tools on such files.

4
  • Thank you, It wasn't \r because I created the file in vi and it is set to unix. But I did run cat -Ev virtualhost.conf and I noticed there was a space in the empty lines. The output had" $" in place of empty lines. I removed them and now it is working fine. Thank you so much for you help. I never thought it would be the input file.
    – Abhishek P
    Commented Aug 6, 2021 at 18:59
  • You're welcome, but the sample input you provided doesn't have blanks in the "empty" lines (otherwise we'd have spotted that immediately) so it's a surprise that they're there in the file you copied that sample input from. Going forward please make sure to copy/paste the sample input, output and code as-is from your computer into your question, don't retype it or whatever else it was you did to it for this question.
    – Ed Morton
    Commented Aug 6, 2021 at 19:02
  • Ya, it could be because I copy-pasted the code here without formatting it as code, and later on applied the code format. Anyways thanks, I will keep this in mind going forward.
    – Abhishek P
    Commented Aug 7, 2021 at 4:01
  • Since this solved your problem, please see unix.stackexchange.com/help/someone-answers for what to do next.
    – Ed Morton
    Commented Aug 7, 2021 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .