Simple (not for me) string search script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple (not for me) string search script
# 1  
Old 05-09-2011
Bug Simple (not for me) string search script

hi there,

I am a complete newb to bash but am going to try and make a script to help me seach text files for strings of interest, any help that one of you gurus could pass on will be greatly received!!

I want to write something like:

in "text1.txt" find "string1" and copy out the line output to a new text file
e.g.
string1 this is the data that follows on from string1 for approximately 230 chars

I will eventually make the script more intelligent but this is just a start.

Thanks in advance people
# 2  
Old 05-09-2011
For something like this I have to ask if this is home-/coursework?
# 3  
Old 05-09-2011
Code:
man grep

# 4  
Old 05-09-2011
more simlpy Smilie add below line to script
Code:
 grep string1 text1.txt >newfile

# 5  
Old 05-09-2011
Hi and welcome to the forum and Unix community.
grep(1) is the tool you are looking for.
Code:
grep string1 text1.txt

will output all lines that contain 'string1'

grep is a filter -- it is line based. It reads in a line, does a search, and prints the line if found; and takes the next line to process.

You can use -A<num> option to get <num> lines After the match, like this:
Code:
grep -A3 string file

string to search for needs to be in double quotes if it contains spaces. Same for file. You can use variables, of course:
Code:
str="pattern to search for"
grep "$str" someFile.txt

Look at the manual pages to find about grep's options on your system.
Code:
man grep

# 6  
Old 05-09-2011
speedy

Hi,
thanks for help.

Its not for homework (I'm 34) lol

I am trying to learn to make scripts to analyse text files that are output from automatic surveys.

Thanks people
# 7  
Old 05-09-2011
Try:
Code:
perl -ln0e '/string1.{230}/gs;print $&' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep command in Linux in a script where the search string has a space

I have a file xyz with the following content PPPL 0123 PPPL 0006 POFT 0923 POFT 1111 WENT 2323 SEND 2345 I also have another file named MasterFile where it contains the above mentioned data million times with different digits at the end for example some times it contains SEND 9999 or WENT... (4 Replies)
Discussion started by: knijjar
4 Replies

2. Shell Programming and Scripting

How to Search a string in a file in shell script?

I have a text file which is generated when the batch job is run. This batch may take few mins to run. When completed, the last line of the text file would be process completed. I need a shell script which will wait for this file till the process completed is printed in it, once found, it would move... (2 Replies)
Discussion started by: Lalat
2 Replies

3. Shell Programming and Scripting

Linux shell script, search by an input string

So, there is a large file where I have to conduct several search using bash shell scripting. The file is like this: TITLE and AUTHOR ETEXT NO. Aspects of plant life; with special reference to the British flora, 56900 by Robert Lloyd... (1 Reply)
Discussion started by: Philia
1 Replies

4. Shell Programming and Scripting

Shell script to search all files for every string in another file

Hello All I have a pattern.txt file in source directory ((/project/source/) in linux server and data looks like: 123abc17 234cdf19 235ifg20 I have multiple log files in log directory (/project/log/) in linux server and data for one log file looks like: <?xml version="1.0" processid... (11 Replies)
Discussion started by: pred55
11 Replies

5. Shell Programming and Scripting

UNIX shell script to search a string in a file

Hi folks, I am new for shell script, I hope somebody to help me to write shell script My requirement is below steps 1. I have apache access.log i.e located in /var/log/httpd/ Ex. 127.0.0.1 - - "GET... (14 Replies)
Discussion started by: Chenchireddy
14 Replies

6. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

7. Shell Programming and Scripting

Script to search a string

Hi all Am having records as below file1 ---- abc 12a a2b am trying to write a script to identify the character is present in the specified array validchar=a b c d e 1 rec=`cat file1` am getting the records in loop for i in $rec do (4 Replies)
Discussion started by: ragu.selvaraj
4 Replies

8. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

9. Shell Programming and Scripting

Shell Script to Search for a particular String and copy the timestamp to a variable

Hi, We Perfrom Loads to the database through a Perl script which generates a statistics file. I need to read the statistics. the Statistics file looks something like below: Process Beginning - 08-26-2010-23.41.47 DB2 CONNECTION SUCCESSFUL! Ready to process and load file: FILENAME # of... (2 Replies)
Discussion started by: Praveenkulkarni
2 Replies

10. Shell Programming and Scripting

shell script to search a string and delete the content

Hi, I've a shell script e.g. #!/bin/bash echo "Enter the next hop id" read nhid echo "enter the IP address" read IP echo "enter the interface name" read name echo "enter the enable/disable state" read state exit 0 now from this script i want to search strings in another (.cam) ... (6 Replies)
Discussion started by: vic_mnnit
6 Replies
Login or Register to Ask a Question