Manipulating a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulating a file
# 1  
Old 06-14-2010
Manipulating a file

Hi everybody,

I need an urgent help with a BASH script. I have file which contains (besides the other data) the lines with the following structure identified by with keyword PCList:

Code:
        <PARAMETER NAME="PCList"
                TYPE="LIST_STRUCTURE"
                MODEL="{[PointCode:INTEGER],[LogNet:INTEGER]}"
                VALUE="{[402],
                                [10]}|
                               {[403],
                                [10]}"
                RANGE="{[0..16777215],[10..14]}"
                VISIBILITY="Yes"
                MODE="Dynamic"
                DESCRIPTION="List of all SIGTRAN RNC pointcodes with the associated logical network"
                RESPONSIBILITY="Site"/>

The VALUE can contain any number of items, which can be spread on any number of lines, idented by spaces or TABs as well, bounded by keywords VALUE and RANGE

I need to write scripts which would in three steps
1) join all VALUE items into 1 line separated by SPACE like this:

Code:
                VALUE="{[402], [10]}| {[403], [10]}"

2) remove last character "

3) add 2 more lines:

Code:
                       {[404], [10]}| {[405], [10]}| {[406], [10]}| {[407], [10]}| {[408], [10]}|
                       {[409], [10]}| {[410], [10]}| {[411], [10]}| {[412], [10]}| {[413], [10]}"

Final result should looks like this:

Code:
        <PARAMETER NAME="PCList"
                TYPE="LIST_STRUCTURE"
                MODEL="{[PointCode:INTEGER],[LogNet:INTEGER]}"
                VALUE="{[402], [10]}| {[403], [10]}|
                       {[404], [10]}| {[405], [10]}| {[406], [10]}| {[407], [10]}| {[408], [10]}|
                       {[409], [10]}| {[410], [10]}| {[411], [10]}| {[412], [10]}| {[413], [10]}"
                RANGE="{[0..16777215],[10..14]}"
                VISIBILITY="Yes"
                MODE="Dynamic"
                DESCRIPTION="List of all SIGTRAN RNC pointcodes with the  associated logical network"
                RESPONSIBILITY="Site"/>


If required scripts are too complicated to compose, as workaround would be accepted also direct replacement of certain single-line VALUE item of PCList structure

Code:
                VALUE="{[402], [10]}| {[403], [10]}"

by multi-line VALUE:

Code:
                VALUE="{[402], [10]}| {[403], [10]}|
                       {[404], [10]}| {[405], [10]}| {[406], [10]}|  {[407], [10]}| {[408], [10]}|
                       {[409], [10]}| {[410], [10]}| {[411], [10]}|  {[412], [10]}| {[413], [10]}"

Can anybody help please ASAP?

Thanks

---------- Post updated at 08:56 PM ---------- Previous update was at 06:28 PM ----------

As still no help, I am trying to do at least workaround mentioned for this problem. I try to use sed to find & replace single line VALUE string (without checking if it is part of PCList structure) like this:

Code:
cat .site.cfg | sed 's/VALUE=\"{\[402\], \[10\]}\| {\[403\], \[10\]}\
/VALUE=\"{\[402\], \[10\]}| {\[403\], \[10\]}|\
                       {\[404\], \[10\]}| {\[405\], \[10\]}| {\[406\], \[10\]}| {\[407\], \[10\]}| {\[408\], \[10\]}|\
                       {\[409\], \[10\]}| {\[410\], \[10\]}| {\[411\], \[10\]}| {\[412\], \[10\]}| {\[413\], \[10\]}\
/g' > myfile.txt

but from this input:
Code:
        <PARAMETER NAME="PCList"
                TYPE="LIST_STRUCTURE"
                MODEL="{[PointCode:INTEGER],[LogNet:INTEGER]}"
                VALUE="{[402], [10]}| {[403], [10]}"
                RANGE="{[0..16777215],[10..14]}"
                VISIBILITY="Yes"
                MODE="Dynamic"
                DESCRIPTION="List of all SIGTRAN RNC pointcodes with the associated logical network"
                RESPONSIBILITY="Site"/>

I get this output:
Code:
        <PARAMETER NAME="PCList"
                TYPE="LIST_STRUCTURE"
                MODEL="{[PointCode:INTEGER],[LogNet:INTEGER]}"
                VALUE="{[402], [10]}| {[403], [10]}|
                       {[404], [10]}| {[405], [10]}| {[406], [10]}| {[407], [10]}| {[408], [10]}|
                       {[409], [10]}| {[410], [10]}| {[411], [10]}| {[412], [10]}| {[413], [10]}
| {[403], [10]}"
                RANGE="{[0..16777215],[10..14]}"
                VISIBILITY="Yes"
                MODE="Dynamic"
                DESCRIPTION="List of all SIGTRAN RNC pointcodes with the associated logical network"
                RESPONSIBILITY="Site"/>

while I would expect only this result:
Code:
        <PARAMETER NAME="PCList"
                TYPE="LIST_STRUCTURE"
                MODEL="{[PointCode:INTEGER],[LogNet:INTEGER]}"
                VALUE="{[402], [10]}| {[403], [10]}|
                       {[404], [10]}| {[405], [10]}| {[406], [10]}| {[407], [10]}| {[408], [10]}|
                       {[409], [10]}| {[410], [10]}| {[411], [10]}| {[412], [10]}| {[413], [10]}"
                RANGE="{[0..16777215],[10..14]}"
                VISIBILITY="Yes"
                MODE="Dynamic"
                DESCRIPTION="List of all SIGTRAN RNC pointcodes with the associated logical network"
                RESPONSIBILITY="Site"/>

Can anybody understand and explain why?

Last edited by sameucho; 06-14-2010 at 01:44 PM..
# 2  
Old 06-14-2010
Code:
awk 'BEGIN {quote=0} 
     
      $1~/^VAL/{
                quote=1; 
                printf( "%s ", $0)
                next
               }
      quote {if ($0 ~ /"$/)
             {
                gsub(/"$/, ""); 
                gsub(/^ /, "");
                printf("%s\n", $1)
                quote=0;
                print "                       {[404], [10]}| {[405], [10]}| {[406], [10]}| {[407], [10]}| {[408], [10]}|"
                printf("                       {[409], [10]}| {[410], [10]}| {[411], [10]}| {[412], [10]}| {[413], [10]}%c\n", 34);
             } 
             else 
             {
               printf("%s ", $1)
             }
             next
            }
       ! quote {print}     
     '  filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Manipulating column in a file in UNIX

Hi I have a file content.txt where I want to replace the value of second column of the file with half of the value of that column. I only have to replace if the second column starts with COM_, rest all values have to be same eg, cat content.txt |COM_A|123|JHV|8475 |ABC|2765|BV|876... (7 Replies)
Discussion started by: infyanurag
7 Replies

2. Shell Programming and Scripting

need help on manipulating a file

Hi, I need a shell/command to achieve this task. I've a delimited unloaded file from oracle in a scrambled format as shown below with many blank lines in it, I'm just trying to tailor it in a format that would be compatible to view and load it to a IDS db. Here is the problem ... (1 Reply)
Discussion started by: divak
1 Replies

3. Shell Programming and Scripting

Manipulating csv file

We need to convert a field in a csv file which is in cents to dollars.(divide by 100) in our shell script. Can some body help me? (3 Replies)
Discussion started by: Deepthz
3 Replies

4. Shell Programming and Scripting

Help Manipulating Large Csv File

Hello everyone, I am trying to manipulate a large .csv file where I have output similar to the following - http://imgur.com/TEXD8.png The result that I am looking for would be to consolidate the first column, but combine the second and third column so it still relates to the first. I... (8 Replies)
Discussion started by: xxwohxx
8 Replies

5. Shell Programming and Scripting

Manipulating the etc/passwd file with sed

How can i use sed to extract the user name and home directory from the /etc/passwd/ file on my server. (11 Replies)
Discussion started by: Pauline mugisha
11 Replies

6. Shell Programming and Scripting

parsing a file and manipulating the contents

Hi I have a text file as follows BOB 14/14 TOM 94/94 SAM 3/3 CRIS 13/13 TOM 6/6 CRIS 27/27 SAM 2/2 JACK 25/25 CRIS (6 Replies)
Discussion started by: shellignorant
6 Replies

7. UNIX for Dummies Questions & Answers

Help!! manipulating file

Hi all, I need help manipulating the file below. Here is what I needed to do. First, I have to replace INSUPD to DELETE. Then I need to change the content of the file around by flipping the contents in the file from the bottom to the top (start from "CMD") How should I attack this? Here... (2 Replies)
Discussion started by: sirrtuan
2 Replies

8. Solaris

Manipulating File

Help...please. I have a log that contains Warning Authentication Failed: User GHDT88998HS doesn't exit: The User GHDT88998HS could not be found Mar 22, 2008 5:22:22AM com.hometel.ttm.auth.userlogin. about maybe a thousand entries failed user acct message How can I grab just the username... (2 Replies)
Discussion started by: rivendell500
2 Replies

9. Shell Programming and Scripting

Manipulating a text file

hey guys, need ur expert help. m a core banker got stuck in someting techie and cant find a solution have manged to extract a file from oracle apps in a format that looks something like this... REC- A b c d x INV- A b... (6 Replies)
Discussion started by: komalkg
6 Replies

10. UNIX for Advanced & Expert Users

Manipulating output file

I have a file containing two fields, Name and Time, with about 57 lines in this file. I am struggling to create a loop that will cut out the first ten lines of this file and echo it to the screen. Can anybody help me please. (1 Reply)
Discussion started by: mariner
1 Replies
Login or Register to Ask a Question