Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

move contents from one file to another based on line number or content

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-20-2010
robp2175 robp2175 is offline
Registered User
 
Join Date: May 2010
Last Activity: 1 June 2010, 12:35 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
move contents from one file to another based on line number or content

I want a script that will move everything beyond a certain line number or beyond a certain content word into another file.
For example, if file A has this:
first line
second line
third line
forth line
fifth line
sixth line

I want to run a script that will move everything beyond the third line to the END of file B, OR it would be even better if the script will move everything beyond the specific words "third line" into file B. That way I could just put in some random characters where I want the cutoff to be and never have to worry about the line number. I am not very good at scripting and do not know where to start. Any help would be greatly appreciated.
Sponsored Links
    #2  
Old Unix and Linux 05-20-2010
dunkar70 dunkar70 is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 23 May 2012, 10:22 AM EDT
Posts: 55
Thanks: 0
Thanked 2 Times in 2 Posts
What work have you started on this script? This forum is intended to help you resolve issues in your solutions, not necessarily as a consultancy to do the work for you. This is not to say you won't get a solution, but you really should try it on your own first.
Sponsored Links
    #3  
Old Unix and Linux 05-20-2010
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 2 July 2015, 3:06 PM EDT
Location: NM
Posts: 10,496
Thanks: 344
Thanked 869 Times in 807 Posts

Code:
awk ' ok==1 {print $0} 
       /third line/ {ok=1} ' inputfile > outputfile

substitute the words "thrid line" with your random characters. NOTE: printing starts with the LINE AFTER the file record with "third line" in it.
    #4  
Old Unix and Linux 05-20-2010
pseudocoder's Unix or Linux Image
pseudocoder pseudocoder is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 17 February 2012, 6:42 AM EST
Location: /home/sea
Posts: 602
Thanks: 40
Thanked 83 Times in 81 Posts
Try

Code:
awk '/third line/,0' FileA | awk 'NR>1' > FileB

The green marked part is optional, if you run the code without it, then the search string "third line" will appear in FileB as well.

Btw. I agree with dunkar70.
Sponsored Links
    #5  
Old Unix and Linux 05-20-2010
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 2 July 2015, 3:06 PM EDT
Location: NM
Posts: 10,496
Thanks: 344
Thanked 869 Times in 807 Posts
Actually it looks somewhat like homework, but not enough to warrant moderation.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print selection of line based on line number mohanm Shell Programming and Scripting 3 02-15-2010 03:28 AM
Remove lines based on contents of another file bashshadow1979 Shell Programming and Scripting 4 03-25-2009 12:32 PM
perform a check based on number of @ in a log line fed.linuxgossip Shell Programming and Scripting 12 02-26-2009 02:04 PM
Split File Based on Line Number Pattern shankster Shell Programming and Scripting 11 10-01-2008 09:49 AM
extracting a line based on line number narendra.pant Shell Programming and Scripting 2 09-20-2007 05:00 AM



All times are GMT -4. The time now is 07:09 PM.