How to copy or cut specific rows from appended file with some conditions | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-04-2012
nex_asp nex_asp is offline
Banned
 
Join Date: Dec 2012
Last Activity: 21 May 2013, 2:14 AM EDT
Location: India
Posts: 70
Thanks: 20
Thanked 0 Times in 0 Posts
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
Attached Files
File Type: txt Input.txt (261 Bytes, 17 views)

Last edited by nex_asp; 12-08-2012 at 02:49 AM.. Reason: modify..
Sponsored Links
    #2  
Old 12-04-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 27 July 2014, 3:45 PM EDT
Location: Amsterdam
Posts: 9,281
Thanks: 260
Thanked 2,303 Times in 2,066 Posts
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 05:59 PM.. Reason: Added file close
The Following User Says Thank You to Scrutinizer For This Useful Post:
nex_asp (12-08-2012)
Sponsored Links
    #3  
Old 12-04-2012
Aqualung71 Aqualung71 is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 30 June 2014, 11:36 AM EDT
Location: Karlsruhe, Baden-Wuerttemberg north of Black Forrest
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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
Scott's Avatar
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 28 July 2014, 2:24 PM EDT
Location: Zürich
Posts: 8,433
Thanks: 328
Thanked 1,030 Times in 901 Posts
Quote:
Originally Posted by Aqualung71 View Post
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?
The Following User Says Thank You to Scott For This Useful Post:
joeyg (12-04-2012)
Sponsored Links
    #5  
Old 12-04-2012
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 July 2014, 2:28 PM EDT
Location: San Jose, CA, USA
Posts: 4,161
Thanks: 162
Thanked 1,418 Times in 1,202 Posts
Quote:
Originally Posted by nex_asp View Post
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).
Sponsored Links
    #6  
Old 12-04-2012
Aqualung71 Aqualung71 is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 30 June 2014, 11:36 AM EDT
Location: Karlsruhe, Baden-Wuerttemberg north of Black Forrest
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Scott View Post
And unreadable. Do all of your scripts looks like that? Is your Enter key broken?
Sorry though, just a bad habit of one-lining...
Sponsored Links
    #7  
Old 12-08-2012
nex_asp nex_asp is offline
Banned
 
Join Date: Dec 2012
Last Activity: 21 May 2013, 2:14 AM EDT
Location: India
Posts: 70
Thanks: 20
Thanked 0 Times in 0 Posts
Sorry, there were some mistakes, Scrutinizer scripts is absolutely fine..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with deleting specific rows from a text file evelibertine UNIX for Dummies Questions & Answers 1 06-28-2011 02:03 PM
How to copy specific file.txt in specific folder? annetote Shell Programming and Scripting 17 06-02-2010 12:50 PM
Deleting specific rows in large files having rows greater than 100000 manish2009 Shell Programming and Scripting 9 12-11-2009 12:17 PM
Copy Limited rows from one file to another ravi214u Shell Programming and Scripting 3 12-17-2008 05:15 AM
How to copy set of files with date appended to their name sish78 UNIX for Dummies Questions & Answers 7 07-07-2008 05:21 PM



All times are GMT -4. The time now is 02:28 PM.