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

后端 未结 10 1987
清歌不尽
清歌不尽 2020-12-03 06:47

I tried this:

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


        
相关标签:
10条回答
  • 2020-12-03 07:26
    awk '{ if ($0 !~ /^#/){printf "address=/%s/127.0.0.1 \n",$0}}' <your_input_file>
    
    0 讨论(0)
  • 2020-12-03 07:29

    You can filter with awk:

    awk '!/^#/{print"address=/"$0"/127.0.0.1"}' file
    
    0 讨论(0)
  • 2020-12-03 07:35
    [ "${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

    0 讨论(0)
  • 2020-12-03 07:36

    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
    
    0 讨论(0)
  • 2020-12-03 07:36

    Maybe you can try

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

    Check the ~ in operand!

    0 讨论(0)
  • 2020-12-03 07:39

    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 讨论(0)
提交回复
热议问题