-2

and thanks in advance. I have a huge database where all lines start like this:

(497, 36390, 495, 88, 89, 2, 'xxdurango/a-...

(498, 36391, 1, 93, 100, 1, 'xxsalamanca...

(499, 36392, 498, 94, 95, 2, 'xxsalamanca/noti...

(500, 36393, 498, 96, 97, 2, 'xxsalama...

(501, 36394, 1, 101, 108, 1, 'xxg...

I need to change the third column after (#, #,

I am trying to use grep <.,.,> <(.,.,>

all this grep commands select other values in the lines.

I want to make a search and replace (using BBEdit), all of the third column needs to change to the same value.

Need to search for: begining of line,+ numeral1 (,) numeral2(,)

Or something like: begining of line, 3digit number (,) four digit number (,)

Any hint?

thanks

1 Answers1

0

The use of grep can only show the lines that match the pattern you want, so you get all or nothing, you won't get the separation of fields that you require. There are other tools that would make this a lot easier, for instance, sed, which uses similar regular expressions but can edit the stream (its name is a contraction of Stream Editor. As an example the following would change the third field to YYYY:

sed -r 's/^(\(([^,]+,){2})[^,]+,/\1 YYYY,/p' input_filename

The command is broken down as follows:

  • -r turns on Extended Regular Expressions
  • s/ is the start of the Search and Replace command
  • ^ Anchors the seach at the start of the line
  • ( begins a grouping that we will refer to later
  • \( is the literal opening bracket at the start of the line
  • ([^,]+,) Reads as one or more characters that aren't comma followed by a comma, and treat as a single unit
  • {2} Says that the previous unit is repeated twice
  • ) Closes the entire pattern to this point as a group that is referred to later
  • [^,]+, Is the same as above, non-commas followed by a comma
  • / Marks the change from the search pattern to the replacement
  • \1 is replaced by the first group in the pattern space (everything up to the second comma)
  • YYYY is our literal replacement
  • /p Ends the replacement pattern and says to print out the change

Using something like awk would be even easier:

awk -F, '{OFS=","; $3="YYYY"; print}' input_filename

It should be pretty obvious how this works, you probably only need to know that -F, sets the input field separator to , and OFS="," does the same for the output field separator used by print. Note that we are simply using the comma to delimit fields, so the first field would include the opening bracket. Since you only want to change the third field, this isn't an issue. If you wanted to change the first field, you'd need to take that into account.

Another option would be to use cut and paste, but I'll leave that as an exercise.

ajdonnison
  • 36
  • 1