how to append a string to next line in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to append a string to next line in perl
# 1  
Old 04-09-2010
how to append a string to next line in perl

hi all , i have a requirement like this..
this just a sample script...

Code:
    $ cat test.sh
#!/bin/bash

perl -e '
open(IN,"addrss");
open(out,">>addrss");
@newval;

while (<IN>)
  {
    @col_val=split(/:/);
    if ($.==1)
    {
      for($i=0;$i<=$#col_val;$i++)
        {
          print("Enter value for $col_val[$i] : ");
          chop($newval[$i]=<STDIN>);
        }


     $str=join(":");
     $_="$str"
     print OUT;

    }
    else
      {
        exit 0;
      }
  }

close(IN);
close(OUT);

'

when i run this scipt...

Code:
$ ./test.sh 
Enter value for NAME : abc
Enter value for ADDRESS : asff35
Enter value for STATE : XYZ
Enter value for CITY : EIDHFF
Enter value for CONTACT
 : 234656758
$ cat addrss
NAME:ADDRESS:STATE:CITY:CONTACT
abc:asff35:XYZ:EIDHFF:234656758$

when ran it second time

Code:
$ cat addrss
NAME:ADDRESS:STATE:CITY:CONTACT
abc:asff35:XYZ:EIDHFF:234656758ioret:56fgdh:ghdgh:afdfg:987643221$

## it is appended in the same line...


i want it to be added to the next line.....

NOTE:
i want to do this by explitly using the filehandles in perl....and not with redirection operators in shell.

please help me!!!
# 2  
Old 04-09-2010
Maybe something like this ?

Code:
$
$
$ cat addrss
NAME:ADDRESS:STATE:CITY:CONTACT
$
$ cat -n f9.sh
     1  #!/usr/bin/bash
     2  perl -e '
     3  open(IN, "addrss");
     4  open(OUT, ">> addrss");
     5  while (<IN>) {
     6    chomp(@col_val=split(/:/));
     7    for($i=0; $i<=$#col_val; $i++) {
     8      print("Enter value for $col_val[$i] : ");
     9      chomp($newval[$i]=<STDIN>);
    10    }
    11    $str=join(":", @newval)."\n";
    12    print OUT $str;
    13    exit 0;
    14  }
    15  close(IN);
    16  close(OUT);
    17  '
$
$ ./f9.sh
Enter value for NAME : name_1
Enter value for ADDRESS : address_1
Enter value for STATE : state_1
Enter value for CITY : city_1
Enter value for CONTACT : contact_1
$
$ cat addrss
NAME:ADDRESS:STATE:CITY:CONTACT
name_1:address_1:state_1:city_1:contact_1
$
$
$ ./f9.sh
Enter value for NAME : name_2
Enter value for ADDRESS : address_2
Enter value for STATE : state_2
Enter value for CITY : city_2
Enter value for CONTACT : contact_2
$
$ cat addrss
NAME:ADDRESS:STATE:CITY:CONTACT
name_1:address_1:state_1:city_1:contact_1
name_2:address_2:state_2:city_2:contact_2
$
$

tyler_durden
# 3  
Old 04-09-2010
thanx a lot tyler.....it worked.!!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append this string to end of each line

Platform: Solaris 10 I have a file like below $ cat languages.txt Spanish Norwegian English Persian German Portugese Chinese Korean Hindi Malayalam Bengali Italian Greek Arabic I want to append the string " is a great language" at end of each line in this file. (3 Replies)
Discussion started by: omega3
3 Replies

2. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

3. Shell Programming and Scripting

Append string to first line of a file

Hi, Please suggest me to write unix command, HEADER20110101 string append to first line of a file.. Regards Akshu (3 Replies)
Discussion started by: akshu.agni
3 Replies

4. Shell Programming and Scripting

Perl search and append new line

Dear All, I want search two lines and append some string in between these lines. Input file tmp,123 ,10:123 tmp,666 ,50:999 tmp,2:19800 5,3:21. tmp,2:19800 55555555 tmp,2:19800 5,3:21.Output should be tmp,123 ,10:123 tmp,666 ,50:999 tmp,2:19800 (4 Replies)
Discussion started by: arvindng
4 Replies

5. Shell Programming and Scripting

Append string at start of line

Hi, I want to append # at the start of line wherever keyword xyz is found through stream editor? Is it possible? (18 Replies)
Discussion started by: db2cap
18 Replies

6. Shell Programming and Scripting

append string with spaces to a line

hi i have a file like (every string contains 16 chars) CTL1330000000000 0000 00 008000 0080000000 i need to form a line and write to a file CTL13300000000000000 00008000 0080000000 total chars should be 64 ... (2 Replies)
Discussion started by: Satyak
2 Replies

7. Shell Programming and Scripting

line contains a string write to new file otherwise append

hi i have the following function, reading file line by line, if line contains "Change state" i need to cut some fields otherwise i need to append the total line to the new file function parse() { while read LINE do echo $LINE |grep -q "Change state" if then echo $LINE |grep -q... (1 Reply)
Discussion started by: Satyak
1 Replies

8. UNIX for Dummies Questions & Answers

HOWTO Append String to End of Line

I have a comma delimited text file and need to appened ",000000" to the end of every line. For example: Before: "D700000","2006" ,"5000","Open Year" ,"Conversion" ,"Wk64","Productive Payroll $" ,1103.45 After: "D700000","2006" ,"5000","Open Year" ,"Conversion" ,"Wk64","Productive Payroll... (3 Replies)
Discussion started by: bggibson
3 Replies

9. Shell Programming and Scripting

perl: simple question on string append

I want to append a decimal number to a string. But I want to restrict the number to only 2 decimal points for e.g: my $output = "\n The number is = "; my $number = 2.3333333; $output = $output . $number; But I want the $output as: "The number is = 2.33"; and not 2.3333333 (I do not... (1 Reply)
Discussion started by: the_learner
1 Replies

10. Shell Programming and Scripting

Using PERL to append chars to each line

I tried using SED to do this, but I'm not having any luck with it. See the previous thread here. I have a program called AMStracker (on OS X) that spits out the values of the motion sensor in the HDD. It has output that looks like this: . . 3 0 -75 3 0 -76 3 0 -77 . . I need to... (5 Replies)
Discussion started by: c0nn0r
5 Replies
Login or Register to Ask a Question