Visit Our UNIX and Linux User Community


Removing Line numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing Line numbers
# 1  
Old 08-07-2009
Removing Line numbers

Hi all,

I have a file consisting of lines in such a format: [sentence number (e.g. M1) and word[ I] separated by space and M1 EOS for fullstop (.) ]

e.g
Code:
M1  I
M1 have
M1 a 
M1 file
M1 consisting
M1 of
M1 lines
M1 in 
M1 such
M1 a
M1 format
M1 EOS
M2 This
M2 is 
M3 an
M3 example
M3 EOS

Now I want to get back to original format as:

I have a file consisting of lines in such a format.
This is an example.


I want to write a Perl script for this purpose. I have started the script as follows. But I don't know how to proceed further. Any help is appreciated to complete the script.
Code:
 open my $M_IN, "<:encoding(utf8)", "$ARGV[0]" or die "Can't open file $ARGV[0]: $!";
 while(my $my_input= <$M_IN>)
        {
                @m_input_text= split /\s+/, $my_input;
                
        }


Last edited by Yogesh Sawant; 08-07-2009 at 10:05 AM.. Reason: added code tags
# 2  
Old 08-07-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 3  
Old 08-07-2009
Code:
perl -i -pe 's/^M. //g' file

# 4  
Old 08-07-2009
Quote:
Originally Posted by my_Perl
...
Now I want to get back to original format as:

I have a file consisting of lines in such a format.
This is an example.

I want to write a Perl script for this purpose.
...
Code:
$
$
$ cat data.txt
M1  I
M1 have
M1 a
M1 file
M1 consisting
M1 of
M1 lines
M1 in
M1 such
M1 a
M1 format
M1 EOS
M2 This
M2 is
M3 an
M3 example
M3 EOS
$
$ perl -ne '{chomp; s/^M\d[ ]+//g; s/[ ]*$//; $x .= " ".$_}
            END {$x =~ s/^ //; $x =~ s/ EOS[ ]*/.\n/g; print $x}' data.txt
I have a file consisting of lines in such a format.
This is an example.
$
$

tyler_durden
# 5  
Old 08-08-2009
You can also accomplish this without perl

Code:
awk '/^M[0-9]+[ \t]+/&&$2!="EOS"{printf("%s ",$2)}$2=="EOS"{printf(".\n")}' data.txt| sed 's/ \.$/./'


Previous Thread | Next Thread
Test Your Knowledge in Computers #227
Difficulty: Easy
According to NetMarketShare, in September 2019 BSD had a 0.01% global market share of the desktop computing market.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Grep for a line containing only 5 numbers

How would you grep for a line containing only 5 numbers? Something like this. 10 2 12 1 13 (4 Replies)
Discussion started by: cokedude
4 Replies

2. Shell Programming and Scripting

Removing Numbers

Hi, Below is a scattered representation of numbers . 1 2 11 22 11 22 1 2 1 2 11 22 1 2 11 22 I need to display only the following sequence "" and delete of the remainder from the output. The output should look like (2 Replies)
Discussion started by: Ananth12
2 Replies

3. Shell Programming and Scripting

Removing comma just from numbers

Hi Guys, I want to remove commas from just the numbers in the file. So both sides of the comma should be numbers. Input file Johan 1,234 nb jan 123,3 hi, hello, bye 12,345,54 hejhej Desired output: Johan 1234 nb jan (6 Replies)
Discussion started by: Johanni
6 Replies

4. Shell Programming and Scripting

Removing a line IF the next line contains string

So, I've been working on a project which takes layer 7 metadata from pcap dumps and archives it. However, there is a lot of dataless information that I don't want in my output. I know of ways to produce the output I want from the input file below, but I want a method of doing this, regardless of... (2 Replies)
Discussion started by: eh3civic
2 Replies

5. Shell Programming and Scripting

Assign Line Numbers to each line of the file

Hi! I'm trying to assign line numbers to each line of the file for example consider the following.. The contents of the input file are hello how are you? I'm fine. How about you? I'm trying to get the following output.. 1 hello how are you? 2 I'm fine. 3 How about you? ... (8 Replies)
Discussion started by: abk07
8 Replies

6. Shell Programming and Scripting

How to get evenly Line numbers?

hi there , i m new to unix , i d like to ask how can a get only even numbered lines matches with the word i search from txt file for example : 3461:1.D. The copyright laws of the place where you are located also govern 3471:1.E. Unless you have removed all references to Project... (17 Replies)
Discussion started by: brhn
17 Replies

7. Shell Programming and Scripting

More with line numbers

Hi All, How to get line numbers when we more on a file in LINUX thanks firestar (1 Reply)
Discussion started by: firestar
1 Replies

8. UNIX for Advanced & Expert Users

Add line numbers to end of each line

Hi i would like to add line numbers to end of each line in a file. I am able to do it in the front of each line using sed, but not able to add at the end of the file. Can anyone suggest The following code adds line number to start of each line sed = filename | sed 'N;s/\n/\t/' how can i... (5 Replies)
Discussion started by: rudoraj
5 Replies

9. Shell Programming and Scripting

removing all numbers from file

I have one me.txt file like aaa 765 gfre 534 jhk 321 cvvb 98 hftg 2 bdg2 hfuk 65 etc.. now I want to remove all numbers form that file using shell script and out put will be printed in me1.txt output: aaa gfre jhk cvvb hftg bdg hfuk please help me. (2 Replies)
Discussion started by: rinku
2 Replies

10. UNIX for Dummies Questions & Answers

removing line and duplicate line

Hi, I have 3 lines in a text file that is similar to this (as a result of a diff between 2 files): 35,36d34 < DATA.EVENT.EVENT_ID.s = "3661208" < DATA.EVENT.EVENT_ID.s = "3661208" I am trying to get it down to just this: DATA.EVENT.EVENT_ID.s = "3661208" How can I do this?... (11 Replies)
Discussion started by: ocelot
11 Replies

Featured Tech Videos