2

I have a comma-separated list in this rather simple format:

IPrangestart,IPrangeend,int number,date (delimited by slashes),Name (is dirty, contains spaces, dots, dashes, quotemarks etc)

The first three columns can't be empty. I want to to transform this to a standard firewall blocking viable format denoting the random separated by a dash:

IPrangestart-IPrangend

Sometimes the fields are empty. What's the quickest and smartest way to do this for thousands of lines? I tried RegEx like [A-Za-z] for each letter and [0-9] for each number, but that doesn't solve the issues with the random " . and similar stuff specified above...

I tried this RegEx but I don't know how to make it recognize the dash in between (\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}

2
  • 1
    Which fields can empty? The first two? IPrangestart and IPrangeend? Feb 28, 2022 at 21:58
  • 1
    No, the first three columns are always there Feb 28, 2022 at 22:01

2 Answers 2

5

If the ip related columns are always there (Based on your comment) use cut to keep the first two comma separated columns, and replace , with a -:

cut -d, -f1,2 --output-delimiter=-

If, for some reason, you haven't got access to GNU cut from coreutils(Thing which i doubt since you tagged the question with linux) and therefore you are missing the --output-delimiter = - option :

sed 's/^\([^,]*\),([^,]*).*/\1-\2/' file
1
awk -F "," 'BEGIN{OFS="-"}{print $1,$2}' File

output

IPrangestart-IPrangeend

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .