Add letters


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Add letters
# 1  
Old 12-01-2009
Add letters

I want to add letters A,B,C,… in front of every line of input while printing them out using PERL.
eg
A file is parsed as a cmd line arg and its context will be displayed as
A line1...
B line 2..

I tried this..but I want better and perfect solution!

Code:
!perl -p
my $counter;
BEGIN { $counter = q{A}; }
print $counter++;

Source(s):
The -p will make it print every input line automatically.

Last edited by pludi; 02-24-2010 at 01:51 AM.. Reason: code tags, please...
# 2  
Old 02-24-2010
MySQL

are you trying like this?

file a.txt

1 Line ...
2 Line ...
3 Line ...
4 Line ...
5 Line ...
6 Line ...
7 Line ...
8 Line ...
9 Line ...
10Line ...


For adding Letters A,B.C to each line see the following perl Example code.

Code:
open(MY,"txt.1");


for( (A..J) )
{
            my $Line = <MY>;
        print "$_ ";
                print $Line;
                print "\n";

}

Output:

A 1 Line ...

B 2 Line ...

C 3 Line ...

D 4 Line ...

E 5 Line ...

F 6 Line ...

G 7 Line ...

H 8 Line ...

I 9 Line ...

J 10Line ...
# 3  
Old 02-24-2010
First question: if it works, why do you want to change it?
Second question: when you hit line numbers > 26, how should it progress? As AA, AB, AC, ... or just stop?

@ungalnanban: Your solution only works if you know the number of lines beforehand.
# 4  
Old 02-25-2010
aadi_uni Code is works. But I gave one more solution for achieve that.

For Example Only I gave the Example.

we need to find the number of line in the file then based up on the line we can expand the characters like.

A..Z
A..ZZ
A..ZZZZ

for example : if your total line number has 3 digits use based on the "ZZZ"

the characters will add after A..Z, AA,AB....ZZZ.

Thanks. pludi.
# 5  
Old 02-25-2010
If you don't know the number of lines in your file,then use the following script.It will print the desired format for you.

Code:
open (MY,"inputfile");

my $ind='A';
while( $ind)
{
            my $Line=<MY>;
            last unless $Line;
            print "$ind ";
            print $Line if($Line);
            $ind++;
}


Last edited by pludi; 02-25-2010 at 04:50 AM.. Reason: code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Random letters

Hi there, first of all this is not homework...this is a new type of exercise for practicing vocabulary with my students. I have a file consisting of two columns, separated by a tab, each line consisting of a word and its definition, separated by a line break. What i need is to replace a... (15 Replies)
Discussion started by: eldeingles
15 Replies

2. UNIX for Dummies Questions & Answers

How to cut only letters?

I was wondering how I could cut only the names of items from the following list: spoons50 cups29 forks50 plates29 I used "man cut" and thought -c would help, but the items have different character lengths. Please note that there is no space between the item and number (so I can't use... (10 Replies)
Discussion started by: PTcharger
10 Replies

3. Shell Programming and Scripting

Randomize letters

Hi, Is there a tool somewhat parallel to rev, but which randomizes instead of reverses? I've tried rl, but I can only get it to randomize words. I was hoping for something like this echo "hello" | ran leolh less simpler solutions are also welcome. Sorry if the question is... (21 Replies)
Discussion started by: jeppe83
21 Replies

4. Shell Programming and Scripting

Need to add letters to a column and add in a new column subtracting from another column

So I have this input 1 10327 rs112750067 T C . PASS DP=65;AF=0.208;CB=BC,NCBI 1 10469 rs117577454 C G . PASS DP=2055;AF=0.020;CB=UM,BC,NCBI 1 10492 rs55998931 C T . PASS DP=231;AF=0.167;CB=BC,NCBI 1 10583 rs58108140 G A ... (3 Replies)
Discussion started by: kellywilliams
3 Replies

5. UNIX for Advanced & Expert Users

Deleting First 10 letters in a line

Hi, Could any one of you let me know any simple Unix command for deleting first 10 letters of first line in unix? Eg: 123456789ABC --Input ABC--Output Thanks Sue (9 Replies)
Discussion started by: pyaranoid
9 Replies

6. Shell Programming and Scripting

Need to strip few letters

Hey guys.. Can experts help me in achieving my purpose.. I have a file which contains email address of some 100 to 1000 domains, I need only the domain names.. Eg: abc@yahoo.com hd@gamil.com ed@hotmail.com The output should contain only Yahoo.com ... (5 Replies)
Discussion started by: achararun
5 Replies

7. Shell Programming and Scripting

trim letters

Hello, I have a list of words.. ranging from 4 to any characters long.. to not more than 20 though. How can I select only first seven letters of the list of words? example:- wwwwwwwwww eeeee wererreetf sdsarddrereewtewt sdsdsds sdsd ereetetttt ewtwertwrttrttrtrtwtrww I... (10 Replies)
Discussion started by: fed.linuxgossip
10 Replies

8. Shell Programming and Scripting

If condition is -lt than 5 letters trouble.

Hi, In an If condition, how can I specify if the variable are less than 5 letters, echo “error”. Something like this one: echo –n “Your name please: “ read name if ; then Echo “Your name must be at least 5 letters length” exit fi Thanks a lot… (6 Replies)
Discussion started by: TARFU
6 Replies

9. Shell Programming and Scripting

transposing letters

Hi, I've written a shell function in bash that reads letters into an array, then outputs them in one column with: for n in "${array}"; do echo $n done I was wondering if anyone knew how i would transpose the letters that are output by the for loop. Right now my output is: aabbcc... (4 Replies)
Discussion started by: myscsa2004
4 Replies

10. UNIX for Dummies Questions & Answers

capital letters GONE!

I have an odd issue. I am trying to copy some files/folders to my linux box via a burned CD which I created on my mac. When I browse the files on the mac (or my windows box), everything looks fine (some of the folder names start with a capital letter, which is needed for everything to work... (8 Replies)
Discussion started by: blogg
8 Replies
Login or Register to Ask a Question