Visit The New, Modern Unix Linux Community


Help to find string and return following characters from list of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to find string and return following characters from list of files
# 1  
Help to find string and return following characters from list of files

Hi,

I'm fairly new to UNIX, but hopefully some-one can help me with this:

I am using the following code to find files with the name "example.xml":

Code:
 
find . -name "example.xml" -print

that would print me a list like the example here:

./dir1/dir2/example.xml
./dira/dirb/example.xml

now here's the riddle for you guys; I want to be able to search in each file returned for the string "Version" and return the following x amount of characters or just that line where the pattern is matched if that is easier.


Hope you can help! Smilie

Leighton
# 2  
Code:
find . -name "example.xml" -exec grep Version {} \;

This User Gave Thanks to bartus11 For This Post:
# 3  
Try this to print filename and line containing string "Version"
Code:
find . -name example.xml -exec awk '/Version/{print FILENAME,$0}' {} \;

This User Gave Thanks to cabrao For This Post:
# 4  
Code:
find . -name "example.xml" -exec grep Version {} /dev/null \;

or
Code:
find . -name "example.xml" -print | xargs grep Version

This User Gave Thanks to citaylor For This Post:
# 5  
Code:
find . -name "example.xml" 2>/dev/null -exec grep -l Version "{}" \;

Try this ^^, it will only print the filename, where the search string is being found.
If that works, try this command without "-l" argument, and it should be that what you're looking for, I hope ;-)

Best regards
This User Gave Thanks to pseudocoder For This Post:
# 6  
Thank you for the quick replies, I'll try this now and let you know!

---------- Post updated at 04:44 PM ---------- Previous update was at 04:36 PM ----------

Thank you all, many way to skin this cat it seems. They all do the job Thank you for expanding on the first reply as the filename in the output has proved very useful. Smilie

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #555
Difficulty: Easy
In C, if double b = 3.14159265359; printf("b=%10f ", b); then the output with be b=3.141593
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Scripting | Return list of unique characters in files

Hi, I am trying to script the below, but I am not very good at it :( Your help would be greatly appreciated. 1. read all files in the directory in strings strings *.* 2. in each file, for each line that contains "ABCD", store characters located at position 521 and 522 of this line... (9 Replies)
Discussion started by: clippertm
9 Replies

2. Shell Programming and Scripting

Find/replace alpha characters in string

Hi, I would like to find a 3-letter character series in a string/variable and replace it with x's. An example set of strings is: 563MS333_101_afp_400-100_screening 563MS333_104-525_rjk_525_screening 563MS333_110_dlj_500-100_w24 563MS333_888-100_mmm_424_screening The only constants... (5 Replies)
Discussion started by: goodbenito
5 Replies

3. Shell Programming and Scripting

Find a string and then return the next 20 characters in multiple files

Hello all, I have a directory with 2000+ files. I need to look in each file for an invoice number. To identify this, i can search for the string 'BIG' and then retrieve the next 30 characters. I was thinking awk for this, but not sure how to do it. Each file contains one long string and in... (8 Replies)
Discussion started by: jdinero
8 Replies

4. Shell Programming and Scripting

Grep string in files and list file names that contain the string

Hi, I have a list of zipped files. I want to grep for a string in all files and get a list of file names that contain the string. But without unzipping them before that, more like using something like gzcat. My OS is: SunOS test 5.10 Generic_142900-13 sun4u sparc SUNW,SPARC-Enterprise (8 Replies)
Discussion started by: apenkov
8 Replies

5. Shell Programming and Scripting

Help with sed command - find a string between two characters

Hi, I have a xml file (Config.xml) <Header name="" TDate="" PDate=""> <Config> {"config" { "Nation" "Pri:|Sec:"}} </Config> </Header> Now I wanted to printed all the strings between "". I tried the following cat Config.xml | sed -n 's/.*\.*//p' ... (8 Replies)
Discussion started by: vivek_damodaran
8 Replies

6. Shell Programming and Scripting

Return error if - or certain characters are present in a list of strings

I have a list of strings, for example: set strLst = "file1 file2 file3 file4" I want to log an error if some of the fields happen to begin with -, or have characters like ; : ' , ? ] { = Which means for example setting set ierr = 1 (2 Replies)
Discussion started by: kristinu
2 Replies

7. UNIX for Dummies Questions & Answers

remove characters from list of files

done some homework on this-- after i remove up to and including the ) i want to take newfile.txt and use that list to remove the files from a file in my the directory pwd i have a input.txt file cat input,txt 1)mary.jpg 12)john.jpg 100)frankkfkdf .jpg i want to remove the characters in the... (1 Reply)
Discussion started by: plener
1 Replies

8. UNIX for Dummies Questions & Answers

Find in Files (special characters)

Well, I've searched the forum, but couldn't find an option, that would help me. I'm really a dummie in unix, so here it goes. I've got like 50k files in a single catalogue. One of them contains a string: Including the box/square brackets. I tried to find it manually, and use some search... (2 Replies)
Discussion started by: kalik
2 Replies

9. Shell Programming and Scripting

Script to find string and return next 2 lines

I am trying to create a script to search for a string within a file, and if found, return the next two lines. Example file:- msj mh query return this 1 return this 2 mjk mhj query return this 3 return this 4 So the script would identify the string "query" and then return the lines... (10 Replies)
Discussion started by: daveaasmith
10 Replies

10. UNIX for Dummies Questions & Answers

Help with find and replace w/string containing special characters

Can I get some help on this please, I have looked at the many post with similar questions and have tried the solutions and they are not working for my scenario which is: I have a text file (myfile) that contains b_log=$g_log/FILENAME.log echo "Begin processing file FILENAME " >> $b_log ... (4 Replies)
Discussion started by: CAGIRL
4 Replies

Featured Tech Videos