delete first number of even-numbered lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete first number of even-numbered lines
# 1  
Old 06-08-2009
delete first number of even-numbered lines

Hello !

I am trying to delete a number from the even-numbered lines of a pipeline after having extracted and sorted the desired data from an original text file using sed...

Code:
sed  -r 's/\([0-9]*\)[^\(]*\(([^\)]*)\),[^,]*,[^,]*,([^\(]*)[^:]*: [^ ]* \w*\=([0-9]*).*/\3,\1/g'  | sort -n

then the data looks like :

Quote:
1,0.0833607576
1,0.0835698182
2,0.0834306364
2,0.0836052121
3,0.0836289697
3,0.0838195758
4,0.0837380909
4,0.0839200909
5,0.0837818182
5,0.0839539394
Now, I am trying to delete the number of the even-numbered lines so as to append the lines of same number together and to get something like :

Quote:
1,0.0833607576,0.0835698182
2,0.0834306364,0.0836052121
3,0.0836289697,0.0838195758
4,0.0837380909,0.0839200909
5,0.0837818182,0.0839539394
I thought of using something like :
  • for deleting the number :
    Code:
    sed -r '1~2p;s/[0-9]*,(.*)/\1/g'

  • for joining lines of same number :
    Code:
    awk '{if (NR%2==1) {getline X}; print $0,",",X}'


but yet, with the sed command line I don't get what I want :
Quote:
1,0.0833607576
0.0833607576
0.0835698182
2,0.0834306364
0.0834306364
0.0836052121
3,0.0836289697
0.0836289697
0.0838195758
4,0.0837380909
0.0837380909
0.0839200909
5,0.0837818182
0.0837818182
0.0839539394
I thank you for your precious help !
# 2  
Old 06-08-2009
Something like this?

Code:
sed 'N;s/\n.*,/,/' file

# 3  
Old 06-08-2009
Yeah ! it seems to work ! Smilie
I am trying to understand what it means exactly (I am new to sed and shell scripting...) Smilie

-----Post Update-----

actually I don't succeed in understanding what makes it subsitute the second line with the line without the first number...
can you explain please ?
Thanks !!

-----Post Update-----

Okey ! I just got it :

it means : substitute the 'new line' and 'everything including the first comma' by nothing... Smilie

fun !
# 4  
Old 06-08-2009
Quote:
Originally Posted by ShellBeginner
Okey ! I just got it :

it means : substitute the 'new line' and 'everything including the first comma' by nothing... Smilie
To be more precise: substitute the 'new line' and 'everything including the first comma' by a comma.Smilie

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find to delete lines with pattern and even or odd number

In the below directory I am trying to delete all lines with a .bam extention that have the pattern IonCode_ followed by an even number. I am also trying to delete all lines with a .fastq extention that have the pattern IonCode_ followed by an odd number. I was going to use find but can see all... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

How to delete lines above a certin line number in bash shell

Hi, I have written a script that returns the line number of the pattern i want and i stored the line number in a variable.Now i want to delete all the lines in a file above this line number which is stored in a variable. i am using sed '1,$getlinenumberd' > file1.txt which is not working(wrog... (5 Replies)
Discussion started by: learninguser235
5 Replies

3. UNIX for Dummies Questions & Answers

How to delete lines above a certin line number in bash shell

Hi, I have written a script that returns the line number of the pattern i want and i stored the line number in a variable(getlinenumber).Now i want to delete all the lines in a file above this line number which is stored in a variable. i am using sed '1,$getlinenumberd' > file1.txt which is... (2 Replies)
Discussion started by: learninguser235
2 Replies

4. Shell Programming and Scripting

Sed delete certain number of empty lines

Hi What I'm trying to do is delete every blank line upto a certain number, so for instance I only want to delete the first 4 empty lines within a file, I know how to delete every line with: sed '/^$/d' But I can't figure out how to limit it to only the first 4 occourances I though it... (5 Replies)
Discussion started by: duonut
5 Replies

5. Shell Programming and Scripting

How to delete several lines from file by line number?

Hi I am using the following command to delete a line from the file by line number: line_number=14 sed "${line_number}d" inputfilename > newfilename Is there a way to modify this command to specify the range of lines to be deleted, lets say from line 14 till line 5 ? I tried using the... (5 Replies)
Discussion started by: aoussenko
5 Replies

6. Shell Programming and Scripting

Delete lines based on line number

I have a file with ~200K lines, I need to delete 4K lines in it. There is no range. I do have the line numbers of the lines which I want to be deleted. I did tried using > cat del.lines sed '510d;12d;219d;......;3999d' file > source del.lines Word too long. I even tried... (2 Replies)
Discussion started by: novice_man
2 Replies

7. Shell Programming and Scripting

delete multiple lines by line number

I have file with 10000 records and i need to delete the lines in single shot based on line number range say from 10 to 51 , 53 to 59 , 105 to 107, 311 to 592 etc... between range works fine for me but how to achive for above case? please help sed '10,51 {d}' infile > outfile (5 Replies)
Discussion started by: zooby
5 Replies

8. Shell Programming and Scripting

Delete all lines that start with a bigger number of a specific one.

E.g. the file is like this: I want to delete all lines that begin with a number larger than 2, ignoring the lines that doesn't begin with a number! PS:'2' is actually a variable that can have a lot of values:b:i bet you got it (10 Replies)
Discussion started by: hakermania
10 Replies

9. UNIX for Dummies Questions & Answers

delete multiple lines by line number

I have been googling, but cannot find that works for me. I have a text file tmp.out with contents: sadfsdf sdfosuidhfousdhof soduhf osdfu osudfhosudhfd sdfgsdfg asdfiojhsdf asdoludhflsdjfhskldjfhsdjdlfsjdhnlj h sdja ouahsdjdafkljsa oljhljh I have another file... (11 Replies)
Discussion started by: ChicagoBlues
11 Replies
Login or Register to Ask a Question