0

I have a file named "rules.txt", this file contains the following below:

Jones 2     IP1   1P2

The thing now is that "Jones 2" is supposed to be one word. How do I get my script to understand this?

All lines of the file are consistent. I basically have NAT rule names, start ip address and end ip address. The NAT rule names are separated by spaces and there is a tab between the IP addresses.

I don't want to do any splitting. I just want to read the "Jones 2" and also read IP1 and IP2 as 3 separate things in my while loop.

  • 2
    Is it consistent, that the first two (space separated) fields should be treated as one? Is there always exactly two more fields? Are all of the separators space, or are some tab? – ctrl-alt-delor Dec 2 '18 at 15:34
  • Please be more specific as to what you expect should happen. Do you want to split each line of the file on sequences of 2 or more spaces (trival with awk -F' {2,}')? What do you want to do with the result? – Stéphane Chazelas Dec 2 '18 at 15:38
  • @ctrl-alt-delor yes this consistent in the file I am using. I basically have NAT rule names, start ip address and end ip address. The NAT rule names are separated by spaces and there is a tab between the IP addresses. – bonafideaf Dec 2 '18 at 16:39
  • @StéphaneChazelas I don't want to do any splitting. I just want to read the "Jones 2" and also read IP1 and IP2 as 3 separate things in my while loop. – bonafideaf Dec 2 '18 at 16:40
  • If fields IP1 and IP2 are separated by tabs, then a simple cut -f1 will return Jones 2, and more over cut -f2 will return IP1 and `cut -f3' will return IP2 – George Vasiliou Dec 2 '18 at 17:43
0

You could start with

perl -pe 's/,/./g;s/  +/,/g' rules.txt

then delimit by comma. Both this in the comment with awk use regular expressions which are the popular tool set for manipulating text.

( +) is a synonym for ( {2,}) both meaning "two or more spaces". The "+" is preceded by two spaces but stackexchange inline code blocks do not respect white spaces...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.