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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a string and then return the next 20 characters in multiple files
# 1  
Old 09-25-2013
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 the middle is the invoice number. If i can find the position of the 'BIG' pattern, then grab the next 30 characters, I can extrapolate the invoice number I need.

I basically need to pull out all 2000+ invoice numbers and put them in one file, one invoice number per line.

Any help is much appreciated??

SAMPLE input:
Code:
TEST FILE|USING|NEW|SYSTEM|BIG|20130924|49685234|THIS ISNT THE END|BYE

output needed:
Code:
BIG|20130924|49685234


keep in mind i need to do this to 2000+ files in one directory.

THANKS!

Jennifer

Last edited by Corona688; 09-25-2013 at 03:34 PM..
# 2  
Old 09-25-2013
I only count 18 characters after BIG, if that's the case you can use this:
Code:
grep -oE BIG.\{18\} file

# 3  
Old 09-25-2013
Hi,
Your demand:
Code:
grep -o 'BIG.\{1,30\}' file

But, maybe better
Code:
grep -o 'BIG|\([^|]\+|\)\{1,2\}' file

where file is a list of file ==> * for all in directory
-h option if you don't want the file name in the resultat.

Regards.
# 4  
Old 09-25-2013
Thank you both but when i try those command, it says it doesn't recognize the -o flag?
# 5  
Old 09-25-2013
Ok,
with sed:
Code:
sed -n 's/.*\(BIG.\{1,30\}\).*/\1/p'

Code:
sed -n 's/.*\(BIG|\([^|]\+|\)\{1,2\}\).*/\1/p'

regards.
# 6  
Old 09-25-2013
thanks for the sed, but I am not sure how to use that with a list of files?
# 7  
Old 09-25-2013
as explain for grep:
Code:
sed .... *

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

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. UNIX for Dummies Questions & Answers

insert multiple characters in string

Hello, newb here :o How do I add square brackets before and after the first character in a string using sed? e.g. 0123456 123456 My attempts have been fruitless. sed 's/.\{0\}//' Thanks. (2 Replies)
Discussion started by: shadyuk
2 Replies

3. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

4. Shell Programming and Scripting

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": find . -name "example.xml" -print that would print me a list like the example here: ./dir1/dir2/example.xml... (5 Replies)
Discussion started by: boijie
5 Replies

5. Shell Programming and Scripting

How to find particular string in multiple files

Hello friends, I have find a paticular string from the files present in my user for example: a username and password is hardcoded in multiple files which present in the my user.so I have to search about username in which files it is available.there are several dirctories are there,so... (5 Replies)
Discussion started by: sivaranga001
5 Replies

6. Shell Programming and Scripting

How to find particular string in multiple files with in the current directory.

Hello friends, Plz suggest the find command, How to search a string in a paticular string in miltiple files with current dirctory.:) Thanks in advance. Siva Ranganath Ch (2 Replies)
Discussion started by: sivaranga001
2 Replies

7. Shell Programming and Scripting

find string from multiple dir and redirect to new files

Hi, I am new to script and I want find one string from multiple files in diff directories and put that out put to new file. Like I have A,B & C directories and each has multiple files but one file is unic in all the directories like COMM.txt Now I want write script to find the string... (8 Replies)
Discussion started by: Mahessh123
8 Replies

8. Shell Programming and Scripting

shell script to find and replace string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (11 Replies)
Discussion started by: pharos467
11 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

Find and replace a string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (2 Replies)
Discussion started by: pharos467
2 Replies
Login or Register to Ask a Question