Extracting/condensing text from multiple files to multiples files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting/condensing text from multiple files to multiples files
# 1  
Old 05-06-2011
Extracting/condensing text from multiple files to multiples files

Hi Everyone,

I'm really new to all this so I'm really hoping someone can help. I have a directory with ~1000 lists from which I want to extract lines from and write to new files. For simplicity lets say they are shopping lists and I want to write out the lines corresponding to apples to a new file for each list i.e. list1apples, list2apples etc. So far the best I've come up with is to use grep on each individual list:

grep Apples list1.text > list1apples.txt
grep Apples list2.text > list2apples.txt

Is there an easier way of doing this so that I can extract the required text from each document to a new one without having to modify the command for each one? As I said this is simplified example, in reality each of the lists has a completely different name given by 4 random letters and numbers.

Alternatively finding a way of deleting everything from these files apart from the desire text would also work.

Any help would be greatly appreciated!
# 2  
Old 05-06-2011
Code:
for i in list*.text
do
grep Apples "$i" > ${i%.*}apples.txt
done

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 05-06-2011
Brilliant. Easily modified this for my files. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting values based on line-column numbers from multiple text files

Dear All, I have to solve the following problems with multiple tab-separated text file but I don't know how. Any help would be greatly appreciated. I have access to Linux mint (but not as a professional). I have multiple tab-delimited files with the following structure: file1: 1 44 2 ... (5 Replies)
Discussion started by: Bastami
5 Replies

2. Shell Programming and Scripting

Extracting specific files from multiple .tgz files

Hey, I have number of .tgz files and want to extract the file with the ending *results.txt from each one. I have tried for file in *.tgz; do tar --wildcards -zxf $file *results.txt; doneas well as list=$(ls *.tgz) for i in $list; do tar --wildcards -zxvf $i *.results.txt; done... (1 Reply)
Discussion started by: jfern
1 Replies

3. Shell Programming and Scripting

Extracting non multiple files via script

Hi, Can somebody help me? I am testing a demo with the given function PATH525="/uscms/home/emily/READme/extra/data/" TEMP=temp FileName=DataFileName CopyFiles() { # PATHNAME="$paths" ... (9 Replies)
Discussion started by: emily
9 Replies

4. Shell Programming and Scripting

extracting information from multiple files

Hello there, I am trying to extract (string) information ( a list words) from 4 files and then put the results into 1 file. Currently I am doing this using grep -f list.txt file1 . and repeat the process for the other 3 files. The reasons i am doing that (a) I do know how to code (b) each file... (4 Replies)
Discussion started by: houkto
4 Replies

5. Programming

extracting text files

i m unable to extract data from one text files to different text files..i am able to concat two text files in d same file ---------- Post updated at 03:21 PM ---------- Previous update was at 03:16 PM ---------- i want a c program for it (2 Replies)
Discussion started by: asd123
2 Replies

6. Shell Programming and Scripting

matching and extracting info from text files

Hi all, I have two .txt file i.e. First text file: 2 4 1 4 Second text file 2 1.nii.gz 4 334.nii.gz 1 12.nii.gz 4 134.nii.gz If entry in 1st column of 1st text file matches the 1st column of 2nd text file, then copy the file (name of which is the second column) associated with... (4 Replies)
Discussion started by: vd24
4 Replies

7. Shell Programming and Scripting

Extracting columns from multiple files with awk

hi everyone! I'd like to extract a single column from 5 different files and put them together in an output file. I saw a similar question for 2 input files, and the line of code workd very well, the code is: awk 'NR==FNR{a=$2; next} {print a, $2}' file1 file2 I added the file3, file4 and... (10 Replies)
Discussion started by: orcaja
10 Replies

8. UNIX for Dummies Questions & Answers

Extracting columns from multiple files with awk

hi everyone! I already posted it in scripts, I'm sorry, it's doubled I'd like to extract a single column from 5 different files and put them together in an output file. I saw a similar question for 2 input files, and the line of code workd very well, the code is: awk 'NR==FNR{a=$2; next}... (1 Reply)
Discussion started by: orcaja
1 Replies

9. UNIX for Advanced & Expert Users

Extracting files with multiple links-perl

i want to write a perl script that gets/displays all those files having multiple links (in current directory) (4 Replies)
Discussion started by: guptesanket
4 Replies

10. UNIX for Advanced & Expert Users

Extracting the required text from log files

It would be highly appreciable if any one helps me in this. I am trying to get it done through Java but I love unix and believe it can be done within minutes with couple of lines. The input log file is a text file contains multiple entries seperated by a blank line. Each seperated entry... (7 Replies)
Discussion started by: hareeshram
7 Replies
Login or Register to Ask a Question