Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading lines in a file and avoiding lines with # with Bash

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.

like image 220
nixnotwin Avatar asked Nov 19 '11 17:11

nixnotwin


People also ask

How do you read each line in a file in Unix?

Syntax: Read file line by line on a Bash Unix & Linux shell file. The -r option passed to read command prevents backslash escapes from being interpreted. Add IFS= option before read command to prevent leading/trailing whitespace from being trimmed. while IFS= read -r line; do COMMAND_on $line; done < input.

How do you read each line in a file Linux?

If you want to read each line of a file by omitting backslash escape then you have to use '-r' option with read command in while loop. Create a file named company2. txt with backslash and run the following command to execute the script. The output will show the file content without any backslash.

What is while read line in shell script?

The while read loop. Here, cat reads each line from standard input and writes the line to standard output. It continues until the end-of-file condition is reached on file1, then it stops. In shell programming, we can read a line from standard input using the read command.


2 Answers

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.

like image 197
Keith Thompson Avatar answered Oct 18 '22 15:10

Keith Thompson


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 
like image 25
ДМИТРИЙ МАЛИКОВ Avatar answered Oct 18 '22 17:10

ДМИТРИЙ МАЛИКОВ