search for content in files. Name of files is in another file. Format as report.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search for content in files. Name of files is in another file. Format as report.
# 1  
Old 07-28-2012
search for content in files. Name of files is in another file. Format as report.

Hi I have multiple files in a folder and one file which contains a list of files (one on each line). I was to search for a string only within these files and not the whole folder. I need the output to be in the form

Code:
File1<tab>string instance 2<tab> string instance 2<tab>string instance 3
File2<Tab>string instance 1

I managed to get the output , but with each instance on a new line with the following

Code:
grep string `cat filenamelist.txt`

I need it in tabbed form, was hoping tr would do it however it made all the items on the same line including the 2 files.

Code:
grep string `cat filenamelist.txt` | tr "\n" "\t"

how can I format it the way I want without a loop?
# 2  
Old 07-28-2012
Why without a loop?

Regards,
Alister
# 3  
Old 07-28-2012
I thought it should be possible without a loop and i remember having done something similar before. its just driving me nuts.
If it is absolutely necessary or much simpler to use a loop i wouldnt mind it, however would be great if it was without one

Also I bet a friend it is possible Smilie
# 4  
Old 07-28-2012
What if the file doesn't contain a match? Should its file name appear in the output or not? If so, should it be followed by a tab?

Regards,
Alister
# 5  
Old 07-28-2012
Both options are fine, however I imagine displaying a filename without any string match would be tougher. If the filename is displayed and it has no matched string it need not have a tab.
# 6  
Old 07-28-2012
Code:
while read f; do
    { printf '%s\n' "$f"; grep 'string' "$f"; } | paste -s -
done < filenamelist.txt

For your bet:
Code:
xargs -L1 sh -c '{ printf %s\\n "$2"; grep "$1" "$2"; } | paste -s -' sh 'string' < filenamelist.txt

Perhaps the implicit looping of xargs doesn't violate any restriction.

Regards,
Alister
# 7  
Old 07-29-2012
Thanks! I will try this out and get back to you. Is there anyway to make the
Code:
grep string `cat filelist.txt`

work the way I want it to?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Program to search for the files with specific format

Hi All, Your help on the below requirement is highly appreciated. I am building a program which should pick the file from a source directory and place them into target directory. While selecting the file in source it should look for some pattern in the file name. Ex: File Name 1:... (10 Replies)
Discussion started by: bharath561989
10 Replies

3. Shell Programming and Scripting

Search & Replace content of files using gsub in awk

Hi,I have 2 files master.txt & reference.txt as shown below & i require o/p as mentioned in file 3 using awk but content is not replacing properlymaster.txt:... (15 Replies)
Discussion started by: siramitsharma
15 Replies

4. Shell Programming and Scripting

"Search for files in a file and write these files to another file which sould be in some other direc

Hi , Need to shell script to extracts files names and write those to another file in different directory. input file is inputfile.txt abc|1|bcd.dat 123 david123 123 rudy2345 124 tinku5634 abc|1|def.dat 123 jevid123 123 qwer2345 124 ghjlk5634 abc|1|pqr.txt 123 vbjnnjh435 123 jggdy876... (1 Reply)
Discussion started by: dssyadav
1 Replies

5. Shell Programming and Scripting

Take a list if strings from a file and search them in a list of files and report them

I have a file 1.txt with the below contents. -----cat 1.txt----- 1234 5678 1256 1234 1247 ------------------- I have 3 more files in a folder -----ls -lrt------- A1.txt A2.txt A3.txt ------------------- The contents of those three files are similar format with different data values... (8 Replies)
Discussion started by: realspirituals
8 Replies

6. Shell Programming and Scripting

Need to build Shell Script to search content of a text file into a folder consist several files

Have to read one file say sourcefile containing several words and having another folder containing several files. Now read the first word of Sourcefile & search it into the folder consisting sevral files, and create another file with result. We hhave to pick the filename of the file in which... (3 Replies)
Discussion started by: mukesh.baranwal
3 Replies

7. Shell Programming and Scripting

copy content of file to another files from certain position

Hello Guys I have a directory of xml files (almost 500 ) Now in each xml file 3 new attributes will be added which are in a different single file Is there any way I can copy the whole content of single file at 6th line of all the files in the directory . Thanks a lot!!! (7 Replies)
Discussion started by: Pratik4891
7 Replies

8. SuSE

Search all files based on first and in all listed files search the second patterns

Hello Linux Masters, I am not a linux expert therefore i need help from linux gurus. Well i have a requirement where i need to search all files based on first patterns and after seraching all files then serach second pattern in all files which i have extracted based on first pattern.... (1 Reply)
Discussion started by: Black-Linux
1 Replies

9. UNIX for Advanced & Expert Users

Search files with specfic extention and later search content

Hi, I would appriciate if somebody can help me figure out how to search for all the *.xml file under a specific directory and subdirectroies (/home/username) and later search of content "<start>" inside the xml file returned by search. -Lovin.V (2 Replies)
Discussion started by: lovi_v
2 Replies

10. UNIX for Dummies Questions & Answers

How to search files containing a specific word in the content

Hi all, Lets say I have 3 files a.txt and b.txt and c.txt. a.txt has the following text ==================== apple is good for health b.txt has the following text ==================== apple is pomme in french c.txt has the following text ==================== orange has citric acid... (1 Reply)
Discussion started by: amjath78
1 Replies
Login or Register to Ask a Question