Reading lines in a file and avoiding lines with #

2020-01-27 05:32发布

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条回答
在下西门庆
2楼-- · 2020-01-27 05:53

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
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-01-27 05:54

Maybe you can try

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

Check the ~ in operand!

查看更多
闹够了就滚
4楼-- · 2020-01-27 05:57

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
查看更多
Evening l夕情丶
5楼-- · 2020-01-27 05:58
awk '{ if ($0 !~ /^#/){printf "address=/%s/127.0.0.1 \n",$0}}' <your_input_file>
查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-01-27 06:00

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 #.

查看更多
聊天终结者
7楼-- · 2020-01-27 06:04

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
查看更多
登录 后发表回答