Perl : joining alternate lines


 
Thread Tools Search this Thread
Top Forums Programming Perl : joining alternate lines
# 1  
Old 11-29-2012
Tools Perl : joining alternate lines

Hi,

I need to join every alternate line in a file

for eg:input file
Code:
$ cat abc
abc
def
ghi
jkl

output
Code:
abc def
ghi jkl

code i wrote for this
Code:
$ cat add_line.pl
#!/usr/bin/perl -w

my $count=1;
#my $line=undef;
my @mem_line;
my $i=0;
my $x=0;

while(<>)
{
chomp;
$x=$count % 2;
#print "$x $_\n";
        if ( $x == 1 )
        {
                $mem_line[$i] = "$_";
        }
        else
        {
                $mem_line[$i - 1] .= " $_\n";
        }
$count+=1;
$i+=1;
}

print "@mem_line";

I am gettin the desired output but with the following error
Code:
./add_line.pl abc
Use of uninitialized value in join or string at ./add_line.pl line 26, <> line 4.
abc def
ghi jkl

can you please help my understand this error

Thanks
Sam
# 2  
Old 11-29-2012
Hi sam05121988,

Beacuse your are incrementing the i counter in every line. You should do it every two lines. Otherwise you will have empty slots in the array that print that warning message. Like:
Code:
while(<>)
{
chomp;
$x=$count % 2;
#print "$x $_\n";
        if ( $x == 1 ) 
        {   
                $mem_line[$i++] = "$_";
        }   
        else
        {   
                $mem_line[$i - 1] .= " $_\n";
        }   
$count+=1;
#$i+=1;
}

# 3  
Old 11-29-2012
Not perl, but try:
Code:
awk '{getline l;print $0,l}' input

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing alternate lines of code

Hi gents, Have only a passing familiarity with linux/shell at this point, so please forgive simple question. I have text files that have lines something like the following: a b c d d d e f e f e f a b (6 Replies)
Discussion started by: cabled
6 Replies

2. Shell Programming and Scripting

Joining broken lines and removing empty lines

Hi - I have req to join broken lines and remove empty lines but should NOT be in one line. It has to be as is line by line. The challenge here is there is no end of line/start of line char. thanks in advance Source:- 2003-04-34024|04-10-2003|Claims|Claim|01-13-2003|Air Bag:Driver;... (7 Replies)
Discussion started by: Jackceasar123
7 Replies

3. Shell Programming and Scripting

Process alternate lines in awk/sed/perl

hi.. i have a fasta file with the following format >sequence1 CCGGTTTTCGATTTGGTTTGACT >sequence2 AAAGTGCCGCCAGGTTTTGAGTGT >sequence3 AGTGCCGCAGAGTTTGTAGTGT Now, i want to read alternate line and add "GGGGGGGGGGG" to end of every sequence Desired output: >sequence1... (4 Replies)
Discussion started by: empyrean
4 Replies

4. Shell Programming and Scripting

Grep values on alternate lines

Hi, I have a file like 2011|ACC|.* 2013|ACC|.* 2011|ACCC|.* 2013|ACCC|.* 2013|ACCV|.* 2011|ADB|.* 2013|ADB|.* 2011|ADBC|.* 2013|ADBC|.* 2011|AIA|.* 2013|AXJ|.* 2013|NNN|.* .* represnts any alphanumeric characters after this part of the string I need a code to return only the... (3 Replies)
Discussion started by: sam05121988
3 Replies

5. Shell Programming and Scripting

Joining broken lines with awk or perl

Hi, I have a huge file with sql broken statements like: PP3697HB @@@@0 <<<<<<Record has been deleted as per PP3697HB>>>>>> FROM sys.xtab_ref rc,sys.xtab_sys f,sys.domp ur WHE RE rc.milf = ur.milf AND rc.molf = f.molf AND ur.dept = 'SWIT'AND ur .department = 'IND' AND share = '2' AND... (4 Replies)
Discussion started by: som.nitk
4 Replies

6. Shell Programming and Scripting

Insert string in alternate lines

Hi All, In continuation of my previous thread 'Add text at the end of line conditionally', I need to further modfiy the file after adding text at the end of the line. Now, I need to add a fixed charater string at alternate lines starting from first line using awk or sed.My file is now as below:... (10 Replies)
Discussion started by: angshuman
10 Replies

7. Shell Programming and Scripting

reading alternate lines of a file

hi, i have 2 files. file1: 1 2 3 4 5 6 file2: a b c d e f g h i (5 Replies)
Discussion started by: vidyaj
5 Replies

8. Shell Programming and Scripting

pattern matching lines using the date, and then joining the lines

Hi Guys, Was trying to attempt the below using awk and sed, have no luck so far, so any help would be appreciated. Current Text File: The first line has got an "\n", and the second line has got spaces/tabs then the word and "\n" TIME SERVER/CLIENT TEXT... (6 Replies)
Discussion started by: eo29
6 Replies

9. Shell Programming and Scripting

alternate lines

Hi, I'm new to Unix. I want to read the all the lines from a text file and write the alternate lines into another file. Please give me a shell script solution. file1 ----- one two three four five six seven newfile(it should contain the alternate lines from the file1) ------- one... (6 Replies)
Discussion started by: pstanand
6 Replies

10. UNIX for Dummies Questions & Answers

alternate lines from two files

A basic request two files want to combine them but on alternate lines (1 Reply)
Discussion started by: SummitElse
1 Replies
Login or Register to Ask a Question