Search : in
By :

Copy text from one file to another

Last answer on Aug 18, 2009 8:43:52 am BST priestex, on Jul 15, 2009 8:00:54 pm BST 
 Report this message to moderators

Hello,

I need to copy text between a start and an end pattern from one file to another.
For example,
Lets say I have a file with the following content:

hello world1
hello world2
hello world3
hello world4
hello world5
beep

I need a shell script that can produce the following file:
hello world2
hello world3
hello world4
hello world5
beep

I do not know the line numbers for the range I want to copy, I just want to copy from the occurrence of 'hello world2' to the occurrence of 'hello world5'+1 line

In case you are wondering what I am trying to do here, I want to run a thread dump on my machine using kill -3 <pid> but the output of this goes to standard output which in my case is directed to the jboss out file which has a lot of other logs in it. So I want to run this script to extract the thread dumps into a separate file once the kill has been executed. Needless to say, there might be more than one thread dumps in the jboss.out file

Any help will be appreciated.
Thank you

Configuration: Linux
Firefox 3.5

Best answers for « Copy text from one file to another » in :
Inserting text in a file ShowInserting text in a file Introduction Syntax Introduction It is sometimes useful to insert text in the header of one or more document (s) or in a specific part of the document (before or after a specific line). The publisher of...
[Sed] Delete one or more lines from a file Show[Sed] Delete one or more lines from a file Removing one (or several) line (s) of a file Syntax: sed '{[/]||[/]}d' sed '{[/][,][/]d' /.../=delimiters n = line number...
Renaming multiple files in batch ShowRenaming multiple files in batch Native features Using a third-party program Renaming a large number of files can quickly become tedious. Fortunately, there are tools to automate this task in many cases: Native features On Windows...
Download CopyTrans Suite ShowCopyTrans Suite is a set of practical tools for your Ipod or Iphone. It contains: · CopyTrans v2.37 · CopyTrans Photo v2.21 · iLibs v1.13 · iCloner v1.07 · CopyTrans Doctor v1.015 · CopyTrans Manager v0.720 · SyncGuardian...
Download Teracopy ShowTeraCopy is a compact program designed to copy and move files at the maximum possible speed.It is free for Home Users. Features: Copy files faster. TeraCopy uses dynamically adjusted buffers to reduce seek times. Asynchronous copy speeds up...
Download Yamipod ShowYamiPod (acronym of Yet Another Ipod Manager) is a freeware allowing to manage your iPod like iTunes. Functions : No installation is necessary automatic Recognition of the ipod Copy of MP3 / files AAC of / towards the ipod Reading /...
FTP protocol (File Transfer Protocol) ShowIntroduction to FTP protocol FTP protocol (File Transfer Protocol) is, as its name indicates a protocol for transferring files. The implementation of FTP dates from 1971 when a file transfer system (described in RFC141) between MIT machines...
Using FTP commands ShowThe FTP protocol FTP (File Transfer Protocol) is a protocol — meaning a standard language that lets two machines communicate — used so that computers of different types (or with different operating systems) can transfer files over a...
MIME (Multipurpose Internet Mail Extensions) ShowIntroduction to MIME MIME (Multipurpose Internet Mail Extensions) is a standard which was proposed by Bell Communications in 1991 in order to expand upon the limited capabilities of email, and in particular to allow documents (such as images,...

1

closeup22, on Jul 15, 2009 10:43:20 pm BST

Hi,

Just copy paste.


with the below command


Ctrl C for copying

Ctrl V for pasting

Reply to closeup22

2

priestex, on Jul 16, 2009 4:24:00 pm BST

I need to do this in a shell script so that the thread dump gets generated before the server shuts down and get the thread dump from the jboss.out file to a separate file.

Reply to priestex

4

SenHu, on Aug 17, 2009 3:40:51 pm BST
  • +1

The simplest way, if your input file is at /folder/somefile.txt,


# Script lines.txt
# Read input file into a string variable.
var str input ; cat "/folder/somefile.txt" > $input
# Remove everything before "hello world2".
stex "]^hello world2^" $input > null
# Remove everything after "hello world5\n".
stex "^hello world5\n^[" $input > null
# $input now has only the desired portion. Print it.
echo $input




Script is in biterscripting ( http://www.biterscripting.com ). To try, save the script as /Scripts/lines.txt, start biterscripting, enter the following command.


script lines.txt


Script can also be called directly from another program, or another shell, as,


/biterScripting/biterScripting.exe lines.txt


Make sure you use the correct file location instead of /folder/somefile.txt. Note the double quotes, ^, [ and ] in the string extractor command syntax. Feel free to translate the script to any other language.

Sen

Reply to SenHu

5

 jipicy, on Aug 18, 2009 8:43:52 am BST
  • +1

Hi,

jp@MDK:~/tmpfs ssh$ cat plop
hello world1
hello world2
hello world3
hello world4
hello world5
beep

jp@MDK:~/tmpfs ssh$ sed -n '/world2/{:z;N;/world5$/!bz;N;p}' plop > bar

jp@MDK:~/tmpfs ssh$ cat bar
hello world2
hello world3
hello world4
hello world5
beep

jp@MDK:~/tmpfs ssh$
;-))
$ man woman
Il n'y a pas de page de manuel pour woman.

Reply to jipicy