38

I tried this:

file="myfile"
while read -r line
do
    [[ $line = \#* ]] && continue
    "address=\$line\127.0.0.1"
done < "$file"

This code doesn't avoid the lines that begin with comments. Even if I don't have any comments, dnsmasq tells that there are errors.

Its going to be a dnsmasq conf file, and it will read and insert domain names like so: address=\mydomain.com\127.0.0.1.


EDIT:1

Input file:

domain1.com
domain2.com
domain3.com
#domain4.com
domain5.com

Output should be:

address=/domain1.com/127.0.0.1
address=/domain2.com/127.0.0.1
address=/domain3.com/127.0.0.1
address=/domain5.com/127.0.0.1

I will drop the script in /etc/dnsmasq.d/ directory so that dnsmaq.conf can process it when dnsmasq is started.

10 Answers 10

47

To skip lines starting with #:

grep -v '^#' myfile | while read -r file ; do
    ...
done

Modify the grep command as needed to, for example, skip lines starting with whitespace and a # character.

2
  • 1
    I am not the downvoter, but maybe they prefer the explicit subshell over implicit one? Such as: while IFS= read -r TI; do echo "${TI}"; done < <(grep -v '#' ./items)
    – JGurtz
    Mar 6, 2019 at 20:27
  • This one is faster than the original answer. Because while read the whole file and do grep is slower.
    – MaXi32
    Jul 3, 2021 at 19:59
29

It's safer to use [[ "$line" = "\#*" ]]

Btw, address="\\${line}\\127.0.0.1"

UPD:

If I've understand you right you need to change every uncommented domains to address=\domain\127.0.0.1. It could be done fast and easy with sed, there is no need in bash-program.

$> cat ./text
domain1.com
domain2.com
domain3.com
#domain4.com
domain5.com

$> sed -r -e 's/(^[^#]*$)/address=\/\1\/127.0.0.1/g' ./text2
address=/domain1.com/127.0.0.1
address=/domain2.com/127.0.0.1
address=/domain3.com/127.0.0.1
#domain4.com
address=/domain5.com/127.0.0.1

If you need to remove commented lines, sed can do it too with /matched_line/d

$> sed -r -e 's/(^[^#]*$)/address=\/\1\/127.0.0.1/g; /^#.*$/d' ./text2 
address=/domain1.com/127.0.0.1
address=/domain2.com/127.0.0.1
address=/domain3.com/127.0.0.1
address=/domain5.com/127.0.0.1

UPD2: if you want to do all that stuff inside the bash script, here is your code modification:

file="./text2"
while read -r line; do
    [[ "$line" =~ ^#.*$ ]] && continue
    echo "address=/${line}/127.0.0.1"
done < "$file"

And it's output:

address=/domain1.com/127.0.0.1
address=/domain2.com/127.0.0.1
address=/domain3.com/127.0.0.1
address=/domain5.com/127.0.0.1
7
  • address= is not a variable. Its a string that should get repeated in all the lines.
    – nixnotwin
    Nov 19, 2011 at 17:55
  • What you mean repeated? You want to replace all lines like #* with address=\${line}\127.0.0.1? Nov 19, 2011 at 18:29
  • 1
    It will be great if you show file that you have and show file that you want. Nov 19, 2011 at 18:55
  • I couldn't get dnsmasq to parse the script by adding the link to the script in dnsmasq.conf. But I made the init file of dnsmasq to execute the script and added the link to the file that was got as an output in dnsmasq.conf. And it worked.
    – nixnotwin
    Nov 20, 2011 at 10:09
  • 1
    The comment matching should be [[ "$line" = "#"* ]]. There may be some discrepancy between systems / versions. Mar 1, 2016 at 21:53
9

Comment lines can and often do begin with whitespace. Here's a bash native regex solution that handles any preceeding whitespace;

while read line; do
  [[ "$line" =~ ^[[:space:]]*# ]] && continue
  ...work with valid line...
done
2
  • I tested this under GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) with comments having zero, one or two leading spaces and/or tabs. I'm not sure why, but using [[ "$line" =~ ^# ]] && continue worked the same.
    – samwyse
    Mar 6 at 19:21
  • Digging deeper, while IFS= read line does not strip surrounding white-space from the line being read.
    – samwyse
    Mar 6 at 19:28
7
[ "${line:0:1}" = "#" ] && continue

This takes the string, gets the substring at offset 0, length 1:

"${line:0:1}"

and checks if it is equal to #

= "#"

and continues looping if so

&& continue

http://www.tldp.org/LDP/abs/html/string-manipulation.html

1
  • Thanks for this, I found this very easy to follow and well explained and without invoking additional complexity (sed, awk and that weird creepy syntax that starts with ^) Mar 18, 2020 at 15:43
5

Only one working for me was:

while IFS=$'\n' read line
do  
    if [[ "$line" =~ \#.* ]];then
        logDebug "comment line:$line"
    else
        logDebug "normal line:$line"
    fi
done < myFile
3

You can filter with awk:

awk '!/^#/{print"address=/"$0"/127.0.0.1"}' file
2
  • Can you explain more your answer please?
    – Zulu
    Jul 15, 2015 at 1:20
  • First it says not to print the line that starts with number sign and then add on either side of original text, $0, what was asked for. Jul 15, 2015 at 10:17
2

This could also be accomplished with 1 sed command:

file="myfile"

sed -i".backup" 's/^#.*$//' $file

This will modify the file in-place (creating a backup copy first), removing all lines starting with a #.

0
1

It has 3 parts. Please read each to understand clearly

  1. To remove # line ----- awk -F'#' '{print $1}' t.txt
  2. To remove a blank line created by # ---- awk 'NF > 0'
  3. To print in required format. ------awk '{print "address=/"$0"/127.0.0.1"}'

So Total Script Needed is,

**awk -F'#' '{print $1}' t.txt | awk 'NF > 0' | awk '{print "address=/"$0"/127.0.0.1"}'**

Output :

address=/domain1.com/127.0.0.1
address=/domain2.com/127.0.0.1
address=/domain3.com/127.0.0.1
address=/domain5.com/127.0.0.1
0
awk '{ if ($0 !~ /^#/){printf "address=/%s/127.0.0.1 \n",$0}}' <your_input_file>
-1

Maybe you can try

[[ "$line"~="#.*" ]] && continue

Check the ~ in operand!

1
  • 3
    it should be '=~' not '~='
    – dm76
    Nov 12, 2014 at 18:13

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.