Delete all rows but leaving first and last ones


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Delete all rows but leaving first and last ones
# 1  
Old 12-26-2010
Delete all rows but leaving first and last ones

Hello,

Merry Christmas to all! I wish you the best for these holidays and the best for the next year 2011.

I'd like your help please, I need to delete all the rows in the third column of my file, but without touching nor changing the first and last value position, this is an example of my file structure:

Code:
1 5 1
2 5 1
3 5 1
4 5 1
5 5 1

and I need it like this:

Code:
1 5 1
2 5 
3 5 
4 5 
5 5 1

The length of my files varies between 3000 and 5000 records. I used tail and head (-1 in both cases) but this doesn't work given these records appear together at the end. I was thinking about replacing all the values in the third column with a blank space, avoiding the first and last column, but don't know how to do it, perhaps this a better way to do it.

I'd be very grateful for your suppot.

Thanks in advance.
# 2  
Old 12-26-2010
try this:
Code:
awk ' NR==1 {printf("%s", $0); next} 
      {printf("\n%s %s", $1, $2);extra=$3 }
      END {print " ", extra} ' inputfile > ourputfile

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 12-26-2010
Thank you so much! it works perfectly.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to delete identical lines while leaving one undeleted?

Hi, I have a file as follows. file1 Hello Hi His Hi Hi Hungry hi so I want to delete identical lines while leaving one of them undeleted. So desired output will be Hello Hi (2 Replies)
Discussion started by: beginner_99
2 Replies

2. Shell Programming and Scripting

Delete rows with conditions

Hi everyone, I will appreciate a lot if anyone can help me about a simple issue. I have a data file, and I need to remove some rows with a given condition. So here is a part of my data file: 5,14,1,3,3,0,0,-0.29977188269E+01 5,16,1,4,4,0,0,0.30394279900E+02... (4 Replies)
Discussion started by: hayreter
4 Replies

3. UNIX for Dummies Questions & Answers

How to delete rows?

I have an Output file which has the result YYYY 95,77 YYYY YYYY 95 YYYY 95 YYYY 95 YYYY 95 YYYY 95 YYYY 95 YYYY 95 YYYY 95 YYYY YYYY YYYY YYYY I would like to display the above along with a single line with above info. Final output should be YYYY 95 (3 Replies)
Discussion started by: priyanka.premra
3 Replies

4. Shell Programming and Scripting

delete the same rows

each individual (row) has genotype expressed for each SNP (column) file1.txt 1 1 A G A T G T A A A A A A A A A A A A A A A A A A A A A 2 2 G A A A A A A A A A A A A A A A A A A A A A A A A A A 3 3 A A A A A A A A A A A A A A A A A A A A A A A A A A A 4 4 G A G T A T A A A A A A A A A A A... (3 Replies)
Discussion started by: johnkim0806
3 Replies

5. UNIX for Dummies Questions & Answers

delete rows with a criteria

Hi, I would like to know how can I delete rows of a text file if from the 3rd column onwards there is only zeros? Thanks in advance (7 Replies)
Discussion started by: fadista
7 Replies

6. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

7. Shell Programming and Scripting

delete rows in a file based on the rows of another file

I need to delete rows based on the number of lines in a different file, I have a piece of code with me working but when I merge with my C application, it doesnt work. sed '1,'\"`wc -l < /tmp/fileyyyy`\"'d' /tmp/fileA > /tmp/filexxxx Can anyone give me an alternate solution for the above (2 Replies)
Discussion started by: Muthuraj K
2 Replies

8. Shell Programming and Scripting

sed/awk: Delete matching words leaving only the first instance

I have an input text that looks like this (comes already sorted): on Caturday 22 at 10:15, some event on Caturday 22 at 10:15, some other event on Caturday 22 at 21:30, even more events on Funday 23 at 11:00, yet another event I need to delete all the matching words between the lines, from... (2 Replies)
Discussion started by: GrinningArmor
2 Replies

9. Shell Programming and Scripting

How to delete particular rows from a file

Hi I have a file having 1000 rows. Now I would like to remove 10 rows from it. Plz give me the script. Eg: input file like 4 1 4500.0 1 5 1 1.0 30 6 1 1.0 4500 7 1 4.0 730 7 2 500000.0 730 8 1 785460.0 45 8 7 94255.0 30 9 1 31800.0 30 9 4 36000.0 30 10 1 15000.0 30... (5 Replies)
Discussion started by: suresh3566
5 Replies

10. UNIX for Dummies Questions & Answers

delete string in a text file leaving the first occurrence

Hi, How can i delete the second and subsequent occurrence of a particular string from a file ? eg) test.txt cattle bat battle mat matter cattle cattle my output file should be cattle bat battle mat matter (12 Replies)
Discussion started by: gopskrish
12 Replies
Login or Register to Ask a Question