0

I have 31 .ctl files in a directory, they looks like this:

load data CHARACTERSET AL32UTF8  
infile '../dane/kontakty_Biura_wyborcze.csv' "str '\n'"
append
into table ODI_PUW_OSOBY2
fields terminated by ';'
OPTIONALLY ENCLOSED BY '"' AND '"'
trailing nullcols
           ( LP CHAR(4000),
             WOJEWODZTWO CHAR(4000),
             POWIAT CHAR(4000),
             GMINA CHAR(4000),
             NAZWA_INSTYTUCJI CHAR(4000),
             KOD CHAR(4000),
             MIEJSCOWOSC CHAR(4000),
             ADRES CHAR(4000),
             NAZWISKO_I_IMIE CHAR(4000),
             FUNKCJA CHAR(4000),
             TEL_SLUZB_STACJON_1 CHAR(4000),
             TEL_SLUZB_STACJON_2 CHAR(4000),
             TEL_SLUZB_STACJON_3 CHAR(4000),
             TEL_SLUZB_KOM_1 CHAR(4000),
             TEL_SLUZB_KOM_2 CHAR(4000),
             FAX_SLUZB_1 CHAR(4000),
             FAX_SLUZB_2 CHAR(4000),
             EMAIL_SLUZB_1 CHAR(4000),
             EMAIL_SLUZB_2 CHAR(4000),
             WWW CHAR(4000),
             TYP CONSTANT "Biura wyborcze.",
             ODI_SESJA_ID CONSTANT "20130717144702"
             ODI_STATUS CONSTANT "0",
             IMIE EXPRESSION     "pg_odi_utils.zwroc_imiona(pg_odi_utils.usun_przyrostki(:NAZWISKO_I_IMIE),0)",
             NAZWISKO EXPRESSION "pg_odi_utils.zwroc_nazwisko(pg_odi_utils.usun_przyrostki(:NAZWISKO_I_IMIE),0)"
       )

There are 31 files like this. I need to replace value in this line:

ODI_SESJA_ID CONSTANT '20130717144702'

to new timestamp, the same for all files. Current timestamp is not known (I mean value that exists in file currently, in this case '20130717144702').

So I need to (for each file found in directory):

  • find line starting from ODI_SESJA_ID
  • replace value after 'ODI_SESJA_ID CONSTANT ' with new one
  • the rest lines in file should stay untouched

What is the best way to do this using bash? Should I use sed or similar tools? How?

3 Answers 3

1

Something like:

sed 's/\(^[ \t]\+ODI_SESJA_ID\ CONSTANT\).*/\1 \"newtimestamp\"/' tmp

should work.

Group the string that will be retained, adding the placeholder (\1) in the replacement string. Replace newtimestamp with whatever value you prefer, of course.

Sign up to request clarification or add additional context in comments.

Comments

0

I would do this using sed like so:

sed -i "/^[ \t]*ODI_SESJA_ID CONSTANT/s/'[^']\+'/'REPLACEMENT'/" *.ctl

The -i flag to sed means it modifies the files in place, so I usually try it on a single file first with the -e flag instead of the -i flag and confirm that sed's output is what I was looking for.

Explanation:

  1. The double-quotes protect my regex from the shell.
  2. /^[ \t]*ODI_SESJA_ID CONSTANT/ matches only the lines that start with whitespace followed by 'ODI_SESJA_ID CONSTANT'.
  3. s/'[^']\+'/'REPLACEMENT'/ substitutes 'REPLACEMENT' (quoted) for the first quoted portion of the text on matching lines.

The document at http://www.catonmat.net/blog/wp-content/uploads/2008/09/sed1line.txt (top Google hit for 'sed one liners' is pretty helpful for quickly dispatching these sort of tasks.

1 Comment

Not working for me... no error but no effect as well, files seems to be untouched
0

I found some simplest solution, it seems to be good:

sed -i 's/.*ODI_SESJA_ID.*/             ODI_SESJA_ID CONSTANT "'$(date +%s)'",/' *.ctl

It replaces lines that contains ODI_SESJA_ID to new value. Not very elegant, because it replaces entire line, instead of only value that need to be processed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.