How to copy or cut specific rows from appended file with some conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to copy or cut specific rows from appended file with some conditions
# 1  
Old 12-04-2012
How to copy or cut specific rows from appended file with some conditions

Hi I have one file which is containing about 5000 rows and 20 columns

I will just explain about my requirement here briefly with sample file, I have attached also, please help....me..

Code:
1    28.25    36.42
5    28.26    36.42
10    28.23    36.43
15    28.22    36.43
20    28.2    36.42
25    28.19    36.43
0    28.25    36.42
1    28.26    36.42
5    28.23    36.43
10    28.22    36.43
15    28.2    36.42
20    28.19    36.43
25    28.25    36.42
3    28.26    36.42
5    28.23    36.43
10    28.22    36.43
15    28.2    36.42
20    28.19    36.43

consider 1st column only

here 1st station profile is 5 to 25, script has copy or cut 5 rows in a file, and to paste it in new file...

2nd profile starts from 0 to 25 script has copy or cut 7 rows in a file, and to paste it in another new file...

3rd profile starts from 3 to 20 script has copy or cut 5 rows in a file, and to paste it in another new file... till the end of file

Last edited by nex_asp; 12-08-2012 at 03:49 AM.. Reason: modify..
# 2  
Old 12-04-2012
Try:
Code:
awk 'NR==1 || $1<p{close(f); f="newfile_" ++n}{print>f; p=$1}' infile


Last edited by Scrutinizer; 12-04-2012 at 06:59 PM.. Reason: Added file close
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-04-2012
Given the simple assumption that a marker should be a smaller value in the first column than in the line previus for creation of a new file the following should do in bash:

Code:
rm file.*; export nf=0;lpiv=10000; while read l; do  piv=$(echo $l | cut -d" " -f1); if test "$piv" -lt $lpiv; then ((nf++)); echo "$l" >> file.$nf ; echo $piv;else echo "$l" >> file.$nf; fi ; lpiv=$piv  ; done < Input.txt

sigh: humble & slow
# 4  
Old 12-04-2012
Quote:
Originally Posted by Aqualung71
Given the simple assumption that a marker should be a smaller value in the first column than in the line previus for creation of a new file the following should do in bash:

Code:
rm file.*; export nf=0;lpiv=10000; while read l; do  piv=$(echo $l | cut -d" " -f1); if test "$piv" -lt $lpiv; then ((nf++)); echo "$l" >> file.$nf ; echo $piv;else echo "$l" >> file.$nf; fi ; lpiv=$piv  ; done < Input.txt

sigh: humble & slow
And unreadable. Do all of your scripts looks like that? Is your Enter key broken?
This User Gave Thanks to Scott For This Post:
# 5  
Old 12-04-2012
Quote:
Originally Posted by nex_asp
Hi I have one file which is containing about 5000 rows and 20 columns

I will just explain about my requirement here briefly with sample file, I have attached also, please help....me..

Code:
1    28.25    36.42
5    28.26    36.42
10    28.23    36.43
15    28.22    36.43
20    28.2    36.42
25    28.19    36.43
0    28.25    36.42
1    28.26    36.42
5    28.23    36.43
10    28.22    36.43
15    28.2    36.42
20    28.19    36.43
25    28.25    36.42
3    28.26    36.42
5    28.23    36.43
10    28.22    36.43
15    28.2    36.42
20    28.19    36.43

consider 1st column only

here 1st station profile is 5 to 25, script has copy or cut 5 rows in a file, and to paste it in new file...

2nd profile starts from 0 to 25 script has copy or cut 6 rows in a file, and to paste it in another new file...

3rd profile starts from 3 to 20 script has copy or cut 5 rows in a file, and to paste it in another new file... till the end of file
I don't understand what a station profile is nor how a shell script is supposed to figure out what lines to ignore and what lines to copy into the new files.

Scrutinizer's awk script makes a perfectly reasonable assumption that you want to take each grouping of lines with increasing values in the 1st column into separate files. But, you say that the 1st new file should contain 5 rows while Scrutinizer's awk script will make newfile_1 contain 6 rows. You say that the 2nd new file should contain 6 rows, but Scrutinizer's awk script will make newfile_2 contain 7 rows. The only match is that you say the the 3rd new file should contain 5 rows and Scrutinizer's script will print 5 rows into newfile_3.

Please explain what logic should be used to produce the output you want AND please post the output that you believe should appear in those three files (in CODE tags).
# 6  
Old 12-04-2012
Quote:
Originally Posted by Scott
And unreadable. Do all of your scripts looks like that? Is your Enter key broken?
Sorry though, just a bad habit of one-lining...
# 7  
Old 12-08-2012
Sorry, there were some mistakes, Scrutinizer scripts is absolutely fine..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut specific column from 2 file and merge

Hi ALL, I have two file. I need to combine these two file based on a layout. I used the below code and able to extract the record. But now able to insert that to a 3'rd file in between the extract FILE 1 CAID NUMBER 1-20 TID NUMBER 21-22 LABEL CHAR 23-44 BASE 45-60... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

2. Shell Programming and Scripting

Extract duplicate rows with conditions

Gents Can you help please. Input file 5490921425 1 7 1310342 54909214251 5490921425 2 1 1 54909214252 5491120937 1 1 3 54911209371 5491120937 3 1 1 54911209373 5491320785 1 ... (4 Replies)
Discussion started by: jiam912
4 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Deleting specific rows from a text file

How do I go about deleting specific rows from a text file (given row number)? (5 Replies)
Discussion started by: evelibertine
5 Replies

5. UNIX for Dummies Questions & Answers

Help with deleting specific rows from a text file

I know this is a complicated question but I will try to illustrate it with some data. I have a data file that looks like the following: 1341 NA06985 0 0 2 46.6432798439 1341 NA06991 NA06993 NA06985 2 48.8478948517 1341 NA06993 0 0 1 45.8022601455 1340 NA06994 0 0 1 48.780669145 1340... (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

How to copy specific file.txt in specific folder?

hye there... i have a problem to copy file in specific folder that will change the name according to host,time(%m%s) and date(%Y%M%D) example folder name: host_20100531.154101801 this folder name will always change... but i just want to copy the AAA.txt and BBB.txt file.. really need... (17 Replies)
Discussion started by: annetote
17 Replies

7. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

8. Shell Programming and Scripting

Copy Limited rows from one file to another

Hi, The file contains 1000 of rows can you please let me know How to copy 1-10 and 30-40 rows from one file to another. thanks :) (3 Replies)
Discussion started by: ravi214u
3 Replies

9. UNIX for Dummies Questions & Answers

How to copy set of files with date appended to their name

Hi gurus, I have set of files in a directory ex : test_file1.csv test_file2.csv test_file3.csv my requirement is to copy these files into another directory but with date appended to the name : like after copying the files should look like : test_file1_07072008.csv ... (7 Replies)
Discussion started by: sish78
7 Replies

10. Shell Programming and Scripting

cut specific records from a file

I am trying to cut the first 10 characters from a file only if the file has 'xyz' in field 185-188. I tried this cat filename | cut -c1-10 but this gives me all the records regardless of what is in field 185-188. Is this doable ? Thanks in advance for responses. (2 Replies)
Discussion started by: jxh461
2 Replies
Login or Register to Ask a Question