Extract content from several txt-files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract content from several txt-files
# 1  
Old 06-12-2008
Extract content from several txt-files

Hi!

Im trying to write a script in ksh that creates a single txt-file from specific content in several other txt-files.
From these files I want to extract all text after 'WORD' and before '=', regardless of number of lines and other content.

I have tried cat and guess I need ${parameter##pattern} like below (only text after WORD), but it doesnt work:

FILENAME=/data/files/N01_????_B*

cat ${FILENAME##*WORD} > /data/files/allfiles.txt

Is it possible to do it this way or is there another way?

/Lars
# 2  
Old 06-12-2008
Code:
perl -0777 -nle'print $1 if /WORD(.*?)=/s' /data/files/N01_????_B* > /data/files/allfiles.txt

For many files:

Code:
perl -0777 -nle'
  print $1 if /WORD(.*?)=/s;
  close ARGV 
    if eof
' /data/files/N01_????_B* > /data/files/allfiles.txt

# 3  
Old 06-12-2008
Thanks, this look good! Smilie

But I see now that I actually should include 'WORD' and '=' in the extraction. I also want to delete any line break (eol) between 'WORD' and '='.
# 4  
Old 06-12-2008
Code:
perl -0777 -ne'
  if (/(WORD.*?=)/s) {
    $_ = $1; 
    $_ =~ tr/\n//d and print;
   }
  close ARGV 
    if eof
' /data/files/N01_????_B* > /data/files/allfiles.txt

Delete or substitute with space?
# 5  
Old 06-12-2008
Substitute with space.
# 6  
Old 06-12-2008
Code:
perl -0777 -ne'
  if (/(WORD.*?=)/s) {
    $_ = $1; 
    $_ =~ tr/\n/ / and print;
   }
  close ARGV 
    if eof
' /data/files/N01_????_B* > /data/files/allfiles.txt

# 7  
Old 06-16-2008
How do I print each extraction on a new line?

I also need to remove carriage returns in the input files.
I tried:
$_ =~ tr/\n \r// and print;
and that seem to work
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract the filename and write to .txt

I'm new to this forum and also to UNIX scripting. I need a command to extract the filename from the path and write to .txt file. Thanks in advance for your guidance. (23 Replies)
Discussion started by: Ram Kumar_BE
23 Replies

2. Shell Programming and Scripting

Extract few content from a huge list of files

I have a huge list of files (about 300,000) which have a pattern like this. .I 1 .U 87049087 .S Am J Emerg .M Allied Health Personnel/*; Electric Countershock/*; .T Refibrillation managed by EMT-Ds: .P ARTICLE. .W Some patients converted from ventricular fibrillation to organized... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

3. Shell Programming and Scripting

Compare two txt files,mismatches will be in new txt files

Hi, Below are the sample data for txt files. txt file 1 Partnumber|catgroup_id 10001082|46016 10001093|4680 10001093|386003 10001093|463004 10003251|683 10003251|63005 10003252|463005 10003252|4683 10003260|463005 10003260|4683 10003264|4683 10003264|463005 13420000|67... (5 Replies)
Discussion started by: Ankita Talukdar
5 Replies

4. Shell Programming and Scripting

Reset different and multiple .txt file content to 0

I need help. I have to create a cron job that will reset the value of different and multiple .txt file to 0. Example: Actual 172_21.txt = 25 192_101.txt = 10 192_168.txt = 5 10_10.txt = 3 After the cron job 172_21.txt = 0 192_101.txt = 0 192_168.txt = 0 10_10.txt = 0 The cron... (2 Replies)
Discussion started by: jasperux
2 Replies

5. UNIX for Dummies Questions & Answers

Help with Aligning the content of a txt file

Hello friends Please help me to display the content of a file in specific aligned manner. for ex. the content of the file may be >$TEST WELCOME HI HELLO UNIX SHELL SCRIPTING >$ I want to display the content like . TEST WELCOME HI HELLO ... (18 Replies)
Discussion started by: rajmohan146
18 Replies

6. UNIX for Dummies Questions & Answers

sed insert content of file.txt to multi files

Ive this sed & find command find /home/www/ -name footer.php -exec sed -i 's/<\/body>/file.txt\n<\/body>/' what I need to place content of file.txt before </body> in all footer.php files file.txt content is google analytic script which is like 7 lines any help to adjust my command to... (2 Replies)
Discussion started by: xmoe
2 Replies

7. UNIX for Dummies Questions & Answers

Extract numbers from .txt file

I need to extract all the p-value numbers and the rho numbers from a .txt file and write them as coma separated values in a new file. Ideally I would get two files in the end, one for p- values and one for rho. Any suggestions? I appreciate your help!!! The .txt file looks essentially like this... (5 Replies)
Discussion started by: eggali
5 Replies

8. Shell Programming and Scripting

extract words from txt using perl

Hi, i will deal with txt file and i want to use perl to extract number of words from this txt ex :if the txt file is a story which contains person names and iwant to extract these names and there is something else that these names which i want perl to extract must match the words (person names) ... (2 Replies)
Discussion started by: eng_shimaa
2 Replies

9. Shell Programming and Scripting

Extract from txt file

I have data as follow in the txt file. I want to skip line starting with '#' sign. #command program abc defmt exp refmt ... ... I want to store abc exp .... in a array. I want to store defmt refmt in a array I need command to read each line in the file. I need... (6 Replies)
Discussion started by: ekb
6 Replies

10. Solaris

list files .txt and .TXT in one command

Dear experts, In a directory i have both *.TXT and *.txt files. I have a script- for file in `ls *.txt`; do mv $file /tmp/$file How to list both *.txt and*.TXT file in one command so that script will move both .txt or .TXT whatever it find. br//purple (4 Replies)
Discussion started by: thepurple
4 Replies
Login or Register to Ask a Question