- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: updating a file - scripting help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-21-2009 01:24 AM
тАО10-21-2009 01:24 AM
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...
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-21-2009 03:37 AM
тАО10-21-2009 03:37 AM
SolutionSo 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-21-2009 06:32 AM
тАО10-21-2009 06:32 AM
Re: updating a file - scripting help
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"
-------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-21-2009 06:45 AM
тАО10-21-2009 06:45 AM
Re: updating a file - scripting help
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2009 02:50 AM
тАО10-22-2009 02:50 AM
Re: updating a file - scripting help
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-01-2009 04:52 PM
тАО11-01-2009 04:52 PM
Re: updating a file - scripting help
Any other ideas on this?
Thanks,
Ramkumar A.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-03-2009 03:27 AM
тАО11-03-2009 03:27 AM
Re: updating a file - scripting help
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..