Visit Our UNIX and Linux User Community


Bash script for searching a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script for searching a string
# 1  
Old 01-14-2009
Bash script for searching a string

Hi,
I have a file that contains thousands of records. Each record starts with "New Record". I need to search this file based on a given value of "timestamp" and write all the records that match this timestamp into a new file.
I was able to locate the existence of a given value of timestamp using grep, but could not figure out how to extract the record and put it into a file. I am new to scripting and any help will be greatly appreciated.Thanks.
Here is the format of the records in that file:

New Record
//several lines of text
<timestamp>2009-01-14</timestamp>
//several lines of text

New Record
//several lines of text
<timestamp>2009-01-14</timestamp>
//several lines of text
# 2  
Old 01-14-2009
Code:
sed -e '/./{H;$!d;}' -e 'x;/timestamp/!d;' yourfile.txt


replace "timestamp" with your timestamp
# 3  
Old 01-14-2009
Thanks funksen. The code you wrote does not extract the entire record. If there is a match in timestamp (i.e. 2009-01-14) then the entire record including the words "New Record" should be copied. In short, if a timestamp matches then copy everything between the tags "New Record" and next "New Record". Thanks.
# 4  
Old 01-14-2009
Are you familiar with output redirection? Use your grep command and just redirect the output using the '>' symbol.

For example:
Code:
grep "timestamp" filename > newfile

Padow
# 5  
Old 01-14-2009
Thanks Padow, but that gets only the line that contains the "timestamp". I want the entire record to be extracted that contains the given "timestamp". Here is an example, if the file contains this :
New Record
//several lines of text
<timestamp>2009-01-14</timestamp>
//several lines of text

and if I search for 2009-01-04 then my output should be
New Record
//several lines of text
<timestamp>2009-01-14</timestamp>
//several lines of text
# 6  
Old 01-14-2009
If your version of grep supports it, you can use the -p flag which will grab the entire paragraph. If the version you are using does not, try searching your system for other versions of the grep command.

Code:
find / -name grep

Padow
# 7  
Old 01-14-2009
Quote:
Originally Posted by shoponek
Thanks funksen. The code you wrote does not extract the entire record. If there is a match in timestamp (i.e. 2009-01-14) then the entire record including the words "New Record" should be copied. In short, if a timestamp matches then copy everything between the tags "New Record" and next "New Record". Thanks.
my code takes the entire paragraph which contains the timestamp, from empty line to empty line, grep -p like padow says is doing the same

the problem is I guess, that the record contains empty lines


unfortunately I don't know how to do this Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #598
Difficulty: Medium
C does not support a traditional foreach loop.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need comand or script for append text after searching for the desired string

Hi all, i have a generated report in unix in the following command like input.txt 47.85,10 0124,42.35,8 0125,3.5,2 the input file format is fixed I need the my output file with append text as below output.txt 0124 amount:42.35 0125 amount:3.5 0124 count : 8 0125... (34 Replies)
Discussion started by: hemanthsaikumar
34 Replies

2. Shell Programming and Scripting

Passing string from SQL to a BASH script

OS Solaris 10, DB oracle 10g Hello, We currently have a BASH script that runs and moves image files from a remote server to the local db server. A snippet of the code shows that we are picking up all Images that are 'mtime -1' some code... for file in `ssh user@10.200.200.10 'find... (3 Replies)
Discussion started by: JonP
3 Replies

3. Shell Programming and Scripting

Requesting help to replace a string by my bash script

Hello every1, I need help to replace a string in a file by my bash script. Find: log4j.appender.toLogFile.layout.ConversionPattern= %d %5p (%F:%L) - %m%n= %d %5p (%F:%L) - %m%n Replace: log4j.appender.toLogFile.layout.ConversionPattern= %d %5p (%F:%L) - %m%n I tried by sed, but kept... (7 Replies)
Discussion started by: titanic4u
7 Replies

4. Shell Programming and Scripting

searching the required string and appending string to it.

Hi all, I have some data in the form of adc|nvhs|nahssn|njadnk|nkfds in the above data i need to write a script so thet it will append "|||" to the third occurnace in the string ..... the outout should look like adc|nvhs|nahssn||||njadnk|nkfds Thanks, Firestar. (6 Replies)
Discussion started by: firestar
6 Replies

5. Shell Programming and Scripting

SPLIT STRING in bash shell script

i need one help.... if i have a string like aaaaa,bbbbb,ccccc,aaaaa How to to split the string and check howmany times aaaaa will be in that string? Thanks (7 Replies)
Discussion started by: karthinvk
7 Replies

6. Shell Programming and Scripting

Cut out string in bash script

Hi all, I'm trying to extract string from variable in BASH. That's probably trivial for grep but I couldn't figure it out. I want to get name, there's sometimes digit after it, but it should be left out. STRING=http://name5.domain.com:8000/file.dat Could someone help me with that? Any... (10 Replies)
Discussion started by: cootue
10 Replies

7. Shell Programming and Scripting

bash script tail when string found do something

Okay, I have two scripts, the first one does some stuff, and comes to a point where it has this: Right here it runs a quick script to start something that writes to a log file. /usr/bin/tail -f ${pathVar}/nohup_${servVar}.out | while read -r line do ] && continue cd ${pathVar}... (0 Replies)
Discussion started by: cbo0485
0 Replies

8. Shell Programming and Scripting

Performing fast searching operations with a bash script

Hi, Here is a tough requirement , to be served by bash script. I want to perform 3,00,000 * 10,000 searches. i.e. I have 10,000 doc files and 3,00,000 html files in the file-system. I want to check, which of the doc files are referred in any html files. (ex- <a href="abc.doc">abc</a>)... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

9. Shell Programming and Scripting

bash shell script string comparison

I want to remove a line that has empty string at second field when I use cut with delimeter , like below $cat demo hello, mum hello, #!/bin/sh while read line do if then # remove the current line command goes here fi done < "demo" i got an error message for above... (4 Replies)
Discussion started by: bonosungho
4 Replies

10. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

Featured Tech Videos