Find string in text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find string in text file
# 1  
Old 04-15-2011
Find string in text file

Hello!

Please, help me to write such script.
I have some text file with name filename.txt
I must check if this file contains string "test-string-first", I must cut from this file string which follows string "keyword-string:" and till first white-space and save it to some variable.
For example. File:
PHP Code:
Start 15022011 Eng 12-3-42
SN1232324422 11 test
-string-first
SN322211 securities
HH keyword
-string:123456@321.net mark (11-22
This file contains "test-string-first" and I need to get 123456@321.net string (between "keyword-string:" and first space)
# 2  
Old 04-15-2011
try:
Code:
var=`awk '/\<test-string-first\>/{getline;getline line;split(line,a," |:");print a[3]}' file
echo $var
123456@321.net

# 3  
Old 04-15-2011
Code:
# cat file
Start 15022011 Eng 12-3-42
SN1232324422 11 test-string-first
SN322211 securities
HH keyword-string:123456@321.net mark (11-22)

Start 15022011 Eng 12-3-42
SN1232324422 11 test-string-first
SN322211 securities
blah blah
HH keyword-string:12343333333@321.net mark (11-22)

blah
blah

$ ruby -0777 -ne '$_.split("test-string-first").each{|x| puts x[/keyword-string:(.[^\s]*)/,1] if x[/keyword/]}' file
123456@321.net
12343333333@321.net

# 4  
Old 04-16-2011
OK! Thnx!
But can I use such structure:

PHP Code:
if (check_exists_of('test-string-first','filename.txt'then
 MYVAR 
awk .... filename.txt
 
echo $MYVAR
else
 echo 
'file doesn't contain '''test-string-first@'' string!'
 
fi 
?
help me pls to change check_exists_of('test-string-first','filename.txt') and MYVAR = awk .... filename.txt statements to the correct.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. UNIX for Beginners Questions & Answers

Find and replace a string in a text file

Dear all, I want to find all the "," in my text file and then replace the commas to a tab. I found a script online but I don't know how to modify the script for my case. Any one can help? Thank you. @echo off &setlocal set "search=%1" set "replace=%2" set "textfile=Input.txt" set... (2 Replies)
Discussion started by: forevertl
2 Replies

3. Shell Programming and Scripting

Find string in file and find the all records by string

Hello I would like to get know how to do this: I got a big file (about 1GB) and I need to find a string (for instance by grep ) and then find all records in this file based on a string. Thanks for advice. Martin (12 Replies)
Discussion started by: mape
12 Replies

4. Shell Programming and Scripting

[Need help] perl script to find the occurance of string from a text file

I have two files 1. input.txt 2. keyword.txt input.txt has contents like .src_ref 0 "call.s" 24 first 0x000000 0x5a80 0x0060 BRA.l 0x60 .src_ref 0 "call.s" 30 first 0x000002 0x1bc5 RETI .src_ref 0 "call.s" 31 first 0x000003 0x6840 ... (2 Replies)
Discussion started by: acdc
2 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

6. Shell Programming and Scripting

How to find repeated string in a text file

I have a text file where I need to find the string = ST*850* This string is repetaed several times in the file, so I need to know how many times it appears in the file, this is the text files: ISA*00* *00* *08*925485USNR *ZZ*IMSALADDERSP... (13 Replies)
Discussion started by: cucosss
13 Replies

7. Shell Programming and Scripting

find string(s) in text file and nearby data, export to list help

Hi, So I'm kinda new to shell scripts and the like. I've picked up quite a bit of use from browsing the forums here but ran into a new one that I can't seem to find an answer for. I'm looking to parse/find a string AND the next 15 or so charachters that follow the string within a text file... (1 Reply)
Discussion started by: kar23me
1 Replies

8. Shell Programming and Scripting

Find a special string in a text document

Hi, i got stuck in creating a search job. I have a text file which looks like this: ====================== Backup - OK - Backup - OK - Backup - ERROR - Backup - WARNING - ====================== I want to search the text file for the string ERROR. If this string is present in the... (10 Replies)
Discussion started by: lnino
10 Replies

9. Shell Programming and Scripting

find a string in a file and add some text after that file

Hi Could you please help me out by solving teh below problem ? I have a file with as below source1|target1|yes source2|target2|no source1 is file in which i have to place some code under the <head> tag in it. What code i have to place in source1 is something like this "abcd.....<target1>... (5 Replies)
Discussion started by: Tasha_T
5 Replies

10. Shell Programming and Scripting

Looking for command(s)/ script to find a text string within a file

I need to search through all files with different file suffixes in a directory structure to locate any files containing a specific string (5 Replies)
Discussion started by: wrwelden
5 Replies
Login or Register to Ask a Question