Forum Stats

  • 3,873,344 Users
  • 2,266,537 Discussions
  • 7,911,514 Comments

Discussions

Replacing a specific column range of blanks with 0s in a data file

rjsosi
rjsosi Member Posts: 251 Bronze Badge

Hi,

We're using Redhat Linux 7.9 with bsh.

I want to go through a data file and replace all the fields between columns 10 and 20 that have blanks with 0s.


So if I have an example file of:

"#######################

#######################

#######################

#########      ######

#######################

#######################

#######################

#######################"

I want the script to make it look like :

"#######################

#######################

#######################

#########000000000######

#######################

#######################"

#######################

#######################"


Thanks!

Comments

  • Nik
    Nik Blocked Member Posts: 2,879 Bronze Crown

    Hi.

    To write a more efficient solution, more data is required (spaces are continuous or not, only numbers or other characters are used in the string, can spaces be found in other positions?).

    Check out this script for a starting point:

    # more t
    123456789012345678901234567890123456789012345678901234567890
    1234567890 2345678901234567890123456789012345678901234567890
    1234567890     678901234567890123456789012345678901234567890
    12345678901234   8901234567890123456789012345678901234567890
    # more c
    awk '{
    s="" ;
    for (i=10;i<20;i++) {
     if(substr($0,i,1) == " " ) s=s "0"
      else s=s substr($0,i,1)
     }
     print substr($0,1,9) s substr($0,20,length($0)-19)
    }' t
    #
    # ./c
    123456789012345678901234567890123456789012345678901234567890
    123456789002345678901234567890123456789012345678901234567890
    123456789000000678901234567890123456789012345678901234567890
    123456789012340008901234567890123456789012345678901234567890
    #
    
    
    

    Regards,

    Nik

  • rjsosi
    rjsosi Member Posts: 251 Bronze Badge

    I'm updating this. Because I work at a bank I had to give an example that wouldn't let me show bank data. It was not an accurate description of the issue. I've mocked up the data more accurately replacing the data with all 0s. The below data is more accurate. What is important is where the blanks are. AS you can see there are row that look like they contain mostly meta data info and are different from the actual data rows. That's what I'm trying to filter out. In my real example The first row is the example of the row I'm trying to fix. It has a blank starting at position 99-112. That's the position I want to change to all '0s'. The second row is an example of a regular row with no errors in it. The third and fourth rows are the meta data rows I want to skip. In the actual case I've chosen column positions 45-55 as the blank columns I want to identify that tell me to skip those rows, This is because in both the first two data rows, those columns are guaranteed to have data in them. Hopefully that clears it up. The data example is shown below.

    ```no-highlight

    0000  000000000              00000000000000000000000000000000000000000000000000000       00000000000000000000000000000000000000000000 0.00000000 00000000 00           000  000                  00000000000000000                                                              000854  

    0000  00000000000 0000000         00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.00000000 00000000             000  000                  00000000000000000                                                              000855  

    0000  000000                                                                                                                                                                                             000863  

    0000    0000000000000000000000000000000000 0000000000000000000000000000000000000000000000       T00000000000000000000000000000            00000        00000000                    00000000000000000000000               00000    000000000000000000000000000000000  000                            000864

    ```

    As you can see the data above is wrapping. It looks like they've changed the forum site since last I used. We used to have tools that let us create halve or highlight our data such that it gave us slice bar if the data rows were too long which is my case. Is there another forum site in Oracle that has these tools? other wise how can I format the above data so the rows don't wrap?

    Thanks Again!

  • Nik
    Nik Blocked Member Posts: 2,879 Bronze Crown

    Hi.

    1. You can change style of line at Your post.

    -> Click Mouse on left side of text. It will look like:

    Example of code block.
    Long line:1: 
             1         2         3         4         5         6         7         8         9         0         1         2         1         1         1         1         1         1         1   
    
    line 2:
    123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
    



    You also can select some sting and change style for it:


    I do not understand your problems description.

    I see some broken masked lines. Is not clear where every lines start and stopped what lines should be skeeped.

    Can You formal describe what need do like:

    • Ignore line if : length < N characters (or some other rules).
    • Change space from position x to position y on 0

    ....


    Regards,

    Nik

  • rjsosi
    rjsosi Member Posts: 251 Bronze Badge
    edited Jul 16, 2021 5:03PM

    Hi Nik,

    I am not able to left click on that block of text to get the "Code Block:" choice: I keep getting left click menu you get when you left click anywhere on the page. Can you be more specific as to how I can get the "Code Block" Menu? I really miss the old formatting tools this forum use to have.

    Seriously are ALL the forums like this now? This new version is so stripped down. Where are the formatting tools we used to have across the top?

    Where are the Names of the Oracle ACEs down the side of the pane?

    Is this a different Forum site in Oracle where can I get back to the old sites. I can't even find my old posts here.

    Why have they gone to this stripped down version? It's not intuitive al all.

  • Nik
    Nik Blocked Member Posts: 2,879 Bronze Crown

    Hi.

    I am general user, so can not comment why design of site is changed.

    About change of formatting:

    You type some text at can see this symbol at the left:

    Click left button of mouse on this symbol. You can see this menu:

    Click left button of mouse on: ", You are got this:


    Click on "Code Block"

    You are also can try other options for see results.


    Regards,

    NIk

  • rjsosi
    rjsosi Member Posts: 251 Bronze Badge
    edited Jul 16, 2021 7:44PM

    Hi Nick,


    OK, I was able to do it. However My goal is to get the lines from the 4 rows I pasted to fit on eone line each so I need to format it so I shows a slide bar. So do you know how I can get a slide bar for the data as it goes past the end of the screen and as a res8ult is wrapping around?


    Thanks!