Operating System - HP-UX
1753724 Members
5075 Online
108799 Solutions
New Discussion юеВ

Re: updating a file - scripting help

 
SOLVED
Go to solution
RamkumarA
Respected Contributor

updating a file - scripting help

Hi,

A strange issue. i got a file like this:
-- ---- - - - ---------
id 1
name aaa
age 20
comments "studing in collee"

id 2
name bbb
age 15
comments "like NY"

id n
name zzz
age 19
comments
comments "test"
-- ---- - - - ---------

i would like to update all "comments".
say i need to add numbers in order to all comments. like this:

-- ---- - - - ---------
id 1a
name aaa
age 20
comments "studing in college[1]"

id 1b
name bbb
age 15
comments "like NY[2]"

id n
name zzz
age 19
comments
comments "test[x]"
-- ---- - - - ---------

note -
1. some comments may not have any comment at all. we should not add the number in that one.
2. comments and a comment inside double quotes
only needs to be updated. if there is no comment, no need to update.

thanks...
6 REPLIES 6
Hein van den Heuvel
Honored Contributor
Solution

Re: updating a file - scripting help

What does this have to do with HP UX?

So how far did you get trying to solve this yourself?

Are those numbers simply incremented starting with 1?

Anyway...

How about:

$ awk '/^comme.*"$/{i++;sub(/"$/,"[" i "]\"")} 1' old > new
$ cat new
id 1
name aaa
age 20
comments "studing in collee[1]"

id 2
name bbb
age 15
comments "like NY[2]"

id n
name zzz
age 19
comments
comments "test[3]"
------------------------------
/^comme.*"$/ = look for lines starting with 'comme', stuff, ending with a double-quote.

{i++; = if found start a block, increment counter i
sub(/"$/, = substitute a double quote at the end of the line
"[" i "]\"")} = by a piece of string, into $0

1 = true = take default action = print $0

Good luck,
Hein.

RamkumarA
Respected Contributor

Re: updating a file - scripting help

great, thanks a lot,
one more issue:
-------------------------------------
id 1a
name aaa
age 20
comments "1 2 3" case
SET
new_class
comments "studing in college[1]"
-------------------------------------
each set contains two comments. the second "comments" needs to be updated with the name. for example:
-------------------------------------
id 1a
name aaa
age 20
comments "1 2 3" case
SET
new_class
comments "studing in college -name aaa"
-------------------------------------
Hein van den Heuvel
Honored Contributor

Re: updating a file - scripting help

Ah, changing the rules.
What problem are you really trying to solve?
What I see for now is only reducing the quality of the data, polluting the data, if it is to be processed 'downstream'.

If the data looks like you posted, with only the second comment having a closing double quote then you can use:

awk '/^name/{nam=$0} /^comme.*"$/{sub(/"$/," - " nam "\"")} 1' old > new

This just remembers the name, to be substituted when comment is seen.

Now if the two comment lines do not have a pattern to distinguish them, then we either need a count or a second indicator like that line with "SET".

for example:

awk '/^name/{x=0; nam=$0} /^SET/ {x=1} x && /^comme.*"$/{x=0;sub(/"$/," - " nam "\"")} 1' old > new

So here, when we see name, we clear a flag x and remember the name line.
When it sees 'SET' it assumes it is ready for the second comment line and indicated that by setting the flag x
When the x is set, and a line starting with comment comes along it does the deed.

Hein.
RamkumarA
Respected Contributor

Re: updating a file - scripting help

Hein,

this is about my HP-OpenView application file modification task. the real file looks so complex and single lines are actually two or three lines long. so i thought to ask a simple question and map the unix command.

And this above command worked perfect for this above example, but for my real issue, it didnt work.

this is what i used:
awk '/^DESCRIPTION/{x=0; DES=$0} /^SET/ {x=1} x && /^TEXT.*"$/{x=0;sub(/"$/," - " DES "\"")} 1' bmcs-backup-test > bmcs-backup-test-new

anyhow, i am getting little concerns that what if this modified some wrong lines also.
that too, when my file has more than 10k lines, its not easy whether modifciation went perfect or not.

so, i would like to spilit this task.
1. put line numbers in the whole file(just for verification)
2. grep all the DESCRIPTIONS and TEXT lines and put it into a second file.
3. now copy the DESRIPITIONS into second TEXT.
4. now we can verify this simply by seeing itself.
5. put the lines back into the original file.
6. remove the line numbers.

i think this idea is good, but a long route.
RamkumarA
Respected Contributor

Re: updating a file - scripting help

Hi,

Any other ideas on this?

Thanks,
Ramkumar A.
RamkumarA
Respected Contributor

Re: updating a file - scripting help

awk '/^DESCRIPTION/{x=0; DES=$0} /^SET/ {x=1} x && /^TEXT.*"$/{x=0;sub(/"$/," - " DES "\"")} 1'

Hi Gurus,

this above command not works. something is wrong. dont know how to troubleshoot also.
so, let me learn this command in parts and troubleshoot it.

what these does:
1. {x=0; DES=$0}
2. {x=1} x &&
3. {x=0;sub(/"$/," - " DES "\"")}
4. and the 1 at the end

something is wrong in these 0s and 1s. probably.

thanks again..