Juls
Juls

Reputation: 55

Remove hostnames from a single line that follow a pattern in bash script

I need to cat a file and edit a single line with multiple domains names. Removing any domain name that has a set certain pattern of 4 letters ex: ozar.

This will be used in a bash script so the number of domain names can range, I will save this to a csv later on but right now returning a string is fine.

I tried multiple commands, loops, and if statements but sending the output to variable I can use further in the script proved to be another difficult task.




Upvotes: 3

Views: 199

Answers (2)

ghoti
ghoti

Reputation: 46846

If all the domains are on a single line separated by spaces, this might work:

awk '/ozar/ {next} 1' RS=" " file.txt

This sets RS, your record separator, then skips any record that matches the keyword. If you wanted to be able to skip a substring provided in a shell variable, you could do something like this:

$ s=ozar
$ awk -v re="$s" '$0 ~ re {next} 1' RS=" " file.txt

Note that the ~ operator is comparing a regular expression, not precisely a substring. You could leverage the index() function if you really want to check a substring:

$ awk -v s="$s" 'index($0,s) {next} 1' RS=" " file.txt

Note that all of the above is awk, which isn't what you asked for. If you'd like to do this with bash alone, the following might be for you:

while read -r -a a; do
  for i in "${a[@]}"; do
    [[ "$i" = *"$s"* ]] || echo "$i"
  done
done < file.txt

This assigns each line of input to the array $a[], then steps through that array testing for a substring match and printing if there is none. Text processing in bash is MUCH less efficient than in a more specialized tool like awk or sed. YMMV.

Upvotes: 1

karakfa
karakfa

Reputation: 67497

you want to delete the words until a space delimiter

 $ sed 's/ozar[^ ]*//g' file

 win.ad.win.edu win_fl. ap.allk.org allk.org  website.com

Upvotes: 1

Related Questions