Find the answer to your Linux question:
Results 1 to 6 of 6
Hello everyone, I was wondering if someone can help me. I am trying to insert a textline in a file, but I want to insert it on a particular line ...
  1. #1
    Linux User
    Join Date
    Jul 2007
    Location
    Greece
    Posts
    277

    adding text in a file PERL

    Hello everyone,

    I was wondering if someone can help me. I am trying to insert a textline in a file, but I want to insert it on a particular line number without replacing the text that is already there.
    The text I want to insert is Option "TwinView" "True" on line 7 and I want to insert it to the xorg file below.

    1) Section "Screen"
    2) Identifier "Screen1"
    3) Device "Videocard1"
    4) Monitor "Monitor1"
    5) DefaultDepth 24
    6) Option "MetaModes" "nvidia-auto-select, nvidia-auto-select"
    7) SubSection "Display"
    8 ) Viewport 0 0
    9) Depth 24
    10)Modes "1280x1024"
    11)EndSubSection
    12) EndSection

    The script I've written so far after reading a not very useful book called perl in 24 hours is below.


    $newline= 'This goes into text';
    open(FILE, ">>/home/yanni/scripts/testfiles/j_run") or die "Can't open file: $!\n";
    @textlines = <FILE>;
    while(<FILE>)
    {
    if { print FILE "$newline\n"; }
    else {print;}
    }
    close(FILE);


    First, I assign the text into a variable, then open the file for appending.
    Put the content of that file into an array and read it with a while command. My problem is that I don't know how to say to perl "after reading line 6, stop and print at line 7 without overwriting what's on that line". In the book it mentions the $_ variable, which confused me but i think that's what I need to use but I don't know how.

    Any suggestions please?

    Thanks a lot.

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Like Kirk, I change the problem. If learning perl is your goal, then we can talk about that, but I would use the stream editor sed for this:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1       Demonstrate append text to specific line with sed.
    
    echo
    set +o nounset
    LC_ALL=C ; LANG=C ; export LC_ALL LANG
    echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) sed
    set -o nounset
    echo
    
    FILE=${1-data1}
    
    echo " Data file $FILE:"
    cat $FILE
    
    echo
    echo " Results:"
    sed '/SubSection "Display"/s/$/ Option "TwinView" "True"/' $FILE
    
    exit 0
    Producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.11-x1, i686
    Distribution        : Xandros Desktop 3.0.3 Business
    GNU bash 2.05b.0
    GNU sed version 4.1.2
    
     Data file data1:
    1) Section "Screen"
    2) Identifier "Screen1"
    3) Device "Videocard1"
    4) Monitor "Monitor1"
    5) DefaultDepth 24
    6) Option "MetaModes" "nvidia-auto-select, nvidia-auto-select"
    7) SubSection "Display"
    8 ) Viewport 0 0
    9) Depth 24
    10)Modes "1280x1024"
    11)EndSubSection
    12) EndSection
    
     Results:
    1) Section "Screen"
    2) Identifier "Screen1"
    3) Device "Videocard1"
    4) Monitor "Monitor1"
    5) DefaultDepth 24
    6) Option "MetaModes" "nvidia-auto-select, nvidia-auto-select"
    7) SubSection "Display" Option "TwinView" "True"
    8 ) Viewport 0 0
    9) Depth 24
    10)Modes "1280x1024"
    11)EndSubSection
    12) EndSection
    I think sed can be cryptic, but it's probably easier than learning an entire new language like perl ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Linux User
    Join Date
    Jul 2007
    Location
    Greece
    Posts
    277
    Thanks a lot for your reply drl but I'd rather use Perl.
    I need to write a statement after if that says when you read line 7 print "something" below that line.

    What I have done so far is here:
    Code:
    open(FILE, "+>>/home/yanni/scripts/testfiles/j_run") || die "Can't open file: $!\n";
    @textlines = <FILE>; 
     while(<FILE>)
       {
           if  (@textlines: $textlines[7] { print FILE "something\n"; }
           else {print;}
       }
    close(FILE);
    With @textlines = <FILE>; the contents of the file are read into an array (according to the book), with while perl reads the file line by line and then I'm trying to say that when you read element 7 of the array which I think is line 7 (plz, correct me if I'm wrong) print "something".
    Last edited by devils casper; 01-19-2009 at 09:07 AM. Reason: Please use [code] ... [/code] tag.

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    OK, perl it is. In your situation, there are two basic ways of getting to the line of interest. One is by matching unique content, as we did with sed. The second is by counting lines -- because you know the number of the line. Neither method requires all lines to be in memory, because we either match or count a single line as we read. Therefore, we do not need to use an array. If the current line is of interest, we modify it, and in any case, we print it. First case:
    Code:
    #!/usr/bin/perl
    
    # @(#) p1       Demonstrate append text to a specific line, matching.
    
    use warnings;
    use strict;
    
    my ($debug);
    $debug = 0;
    $debug = 1;
    
    while (<>) {
      chomp;
      if (/SubSection "Display"/) {
        s/$/ Option "TwinView" "True"/;
      }
      print "$_\n";
    }
    
    print STDERR " ( Lines read: $. )\n";
    
    exit(0);
    Producing:
    Code:
    % ./p1 data1
    1) Section "Screen"
    2) Identifier "Screen1"
    3) Device "Videocard1"
    4) Monitor "Monitor1"
    5) DefaultDepth 24
    6) Option "MetaModes" "nvidia-auto-select, nvidia-auto-select"
    7) SubSection "Display" Option "TwinView" "True"
    8 ) Viewport 0 0
    9) Depth 24
    10)Modes "1280x1024"
    11)EndSubSection
    12) EndSection
     ( Lines read: 12 )
    Second:
    Code:
    #!/usr/bin/perl
    
    # @(#) p2       Demonstrate append text to a specific line, counting.
    
    use warnings;
    use strict;
    
    my ($debug);
    $debug = 0;
    $debug = 1;
    
    while (<>) {
      chomp;
      if ( $. == 7 ) {
        s/$/ Option "TwinView" "True"/;
      }
      print "$_\n";
    }
    
    print STDERR " ( Lines read: $. )\n";
    
    exit(0);
    Also producing:
    Code:
    % ./p2 data1
    1) Section "Screen"
    2) Identifier "Screen1"
    3) Device "Videocard1"
    4) Monitor "Monitor1"
    5) DefaultDepth 24
    6) Option "MetaModes" "nvidia-auto-select, nvidia-auto-select"
    7) SubSection "Display" Option "TwinView" "True"
    8 ) Viewport 0 0
    9) Depth 24
    10)Modes "1280x1024"
    11)EndSubSection
    12) EndSection
     ( Lines read: 12 )
    You may have noticed that the substitution is exactly the same as in sed, but we are using a lot more characters to do the same thing. That's why I would use sed.

    See perldoc perldoc for how to find documentation on perl constructs ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  5. #5
    Linux User
    Join Date
    Jul 2007
    Location
    Greece
    Posts
    277
    Thank you very much for your help drl.
    I have a better understanding on how

  6. #6
    Linux User
    Join Date
    Jul 2007
    Location
    Greece
    Posts
    277
    Thank you very much for your help drl.
    I now have a better understanding on how I can do this (i.e. appending text files), however there is something else.
    On your second script you state if $. is 7 do "that".
    $. is not defined, how come you make an if "statement" about it?
    Code:
    use warnings;
    use strict;
    
    my ($debug);
    $debug = 0;
    $debug = 1;
    
    while (<>) {
      chomp;
      if ( $. == 7 ) {
        s/$/ Option "TwinView" "True"/;
      }
      print "$_\n";
    }
    
    print STDERR " ( Lines read: $. )\n";
    
    exit(0);
    Also, can you please tell me why the angle operators are the only characters in your while expression? Shouldn't you include the filehandle in there? Just asking.
    Last edited by devils casper; 01-19-2009 at 09:06 AM. Reason: Please use [code] ... [/code] tag.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...