How to remove lines with while loop whch were previously added
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to remove lines with while loop whch were previously added
Hi Folks,
I am a little confused here with my script.
I am reading from file; running in while loop; creating the zone.
This is working fine.
However, since I keep adding new entries; due to while loop those are getting added again and again.
Here is what I mean -
Quote:
_EXPL_WHITELIST="/zone/expl_wl_bl/customWL.txt"
if [ -f ${_EXPL_WHITELIST} ]
then
if [ -s ${_EXPL_WHITELIST} ]
then
cat $_EXPL_WHITELIST | while read line;do echo -e $line'\t'CNAME'\t'rpz-passthru.;done >> $_FINAL_WHITELIST
fi
fi
rndc reload
#!/bin/bash
#
_EXPL_WHITELIST="/zone/expl_wl_bl/customWL.txt"
while read line
do
printf "${line}\tCNAME\trpz-passthru.\n"
done <${_EXPL_WHITELIST} > ${_FINAL_WHITELIST}
#
your problem was >> $_FINAL_WHITELIST
>> appends to file
> overwrites file
since you are not really doing much, you could just use sed
Code:
sed 's/.*/&\tCNAME\trpz-passthru./' "${_EXPL_WHITELIST}"
Last edited by Firerat; 11-16-2019 at 07:03 AM.
Reason: fixed <<<$_EX.. should be <$_EX..
It is good to replace the non-portable "echo -e" with "printf".
But the printf format string (first argument) understands things like %s and %f in addition to \t and \n.
So if $line had a % character it would go wrong in the format string.
So better have variables in the following arguments:
I think almost everything was already told, so here are just a few minor comments:
1. use shellcheck to check your shell script, that will give you some hints to write more robust code.
2. for example it will suggest to use: read -r line
3. a not-so-nice solution (comparing to the suggested ones) would be: you can sort -u the result, which will remove all the duplicates.
I am reading from file; running in while loop; creating the zone.
This is working fine.
However, since I keep adding new entries; due to while loop those are getting added again and again.
Here is what I mean -
Code:
_EXPL_WHITELIST="/zone/expl_wl_bl/customWL.txt"
if [ -f ${_EXPL_WHITELIST} ]
then
if [ -s ${_EXPL_WHITELIST} ]
then
cat $_EXPL_WHITELIST | while read line;do echo -e $line'\t'CNAME'\t'rpz-passthru.;done >> $_FINAL_WHITELIST
fi
fi
rndc reload
In the next iteration again first lines get added again and again.
I need to find a solution -
so that only new entries will get appended.
Any idea folks?
you have two files
_EXPL_WHITELIST
_FINAL_WHITELIST
you cat $_EXPL_WHITELIST into the final list file. how are you getting text added into your first file? why not take how you're getting NEW text added into the first file then just append it to your last file instead, by passing that step that looks like it keeps a running tally of what you're doing, like your repeating a step.
appended new entries into file1
use file1 to append the entries into file2.
repeat
using 2 files with almost matching data
Code:
#!/usr/bin/env bash
file1=/home/userx/test/finalList
file2=/home/userx/test/customList
while read keep
do
while read add
do
if [[ "$add" != "$keep" ]]
then
echo "$add"
Add2Array+=( "$add" )
fi
done < $file2
done < $file1
#to get rid of dups due to method used to find
#not entered into final file
declare -A dupes
for i in "${Add2Array[@]}"; do
if [[ -z ${dupes[$i]} ]]; then
NEWARRAY+=("$i")
fi
dupes["$i"]=1
done
unset dupes # optional
printf "[%s]" "${Add2Array[@]}"
echo
printf "[%s]" "${NEWARRAY[@]}"
echo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.