Searching a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching a file
# 1  
Old 07-13-2009
Searching a file

Hi

I need to write a script which will read a file and will take one command line argument which is called CampaignId and the script should print all the records with the same campaignId that I have passed to it.

File Format is as below: -

Code:
2009-06-27 10:59:03,853 [MyClusteredScheduler_Worker-4] INFO [FailureReport] - ptid='2009-06-27 10:59:03' campaign='CampaignId' Value='@13038c7' reason='Time restriction.'
2009-06-27 10:59:03,858 [MyClusteredScheduler_Worker-4] INFO [FailureReport] - ptid='2009-06-27 10:59:03' campaign='CampaignId' Value='@f47355' reason='Time restriction.'
2009-06-27 10:59:03,860 [MyClusteredScheduler_Worker-4] INFO [FailureReport] - ptid='2009-06-27 10:59:03' campaign='CampaignId' Value='@bfe56d' reason='Time restriction.'
2009-06-29 10:59:37,872 [MyClusteredScheduler_Worker-1] INFO [FailureReport] - ptid='2009-06-29 10:59:37' campaign='CampaignId' Value='@4fcfde' reason='Time restriction.'
2009-06-29 10:59:37,875 [MyClusteredScheduler_Worker-1] INFO [FailureReport] - ptid='2009-06-29 10:59:37' campaign='CampaignId' Value='@1d7f901' reason='Time restriction.'
2009-06-29 10:59:37,877 [MyClusteredScheduler_Worker-1] INFO [FailureReport] - ptid='2009-06-29 10:59:37' campaign='CampaignId' Value='@1cd479' reason='Time restriction.'
2009-06-29 11:13:09,274 [MyClusteredScheduler_Worker-2] INFO [FailureReport] - ptid='2009-06-29 11:13:09' campaign='CampaignId' Value='@104e3f1' reason='Time restriction.'
2009-06-29 11:13:09,280 [MyClusteredScheduler_Worker-2] INFO [FailureReport] - ptid='2009-06-29 11:13:09' campaign='CampaignId' Value='@f6dd4' reason='Time restriction.'
2009-06-29 11:13:09,283 [MyClusteredScheduler_Worker-2] INFO [FailureReport] - ptid='2009-06-29 11:13:09' campaign='CampaignId' Value='@f3b5d9' reason='Time restriction.'
2009-06-29 11:21:21,380 [MyClusteredScheduler_Worker-3] INFO [FailureReport] - ptid='2009-06-29 11:21:21' campaign='CampaignId' Value='@1bcf5bc' reason='Time restriction.'
2009-06-29 11:21:21,383 [MyClusteredScheduler_Worker-3] INFO [FailureReport] - ptid='2009-06-29 11:21:21' campaign='CampaignId' Value='@a92a55' reason='Time restriction.'
2009-06-29 11:21:21,386 [MyClusteredScheduler_Worker-3] INFO [FailureReport] - ptid='2009-06-29 11:21:21' campaign='CampaignId' Value='@1a39f8' reason='Time restriction.'

I dont know how to start with .. Can anyone please guide????

Last edited by Yogesh Sawant; 07-13-2009 at 04:09 AM.. Reason: added code tags
# 2  
Old 07-13-2009
See if this helps

Code:
./script.sh <campaign id>

#!/bin/sh
cat campaign_report.txt | grep $1
exit 0

# 3  
Old 07-13-2009
You can use grep directly:
Code:
vim script.sh

#!/bin/bash
grep $1 $2
exit 0

Code:
chmod +x script.sh
./script.sh <campaign id> <youfile.txt>

# 4  
Old 07-14-2009
Sorry .. I didnt explain my problem completely ..

For the Campaign Id passed to the script, I need just the 'value' field which is in the single quotes and is in bold Font below

Value='@13038c7'

The above two solutions will provide the full line.
# 5  
Old 07-14-2009
Code:
# set ID ex. ID="@1a39f8"
grep "Value='$ID'" file | sed -e "s/^.*'CampaignId'//" -e "s/reason=.*$//"

# 6  
Old 07-14-2009
try this

Code:
./script.sh <campaign id>

#!/bin/sh
grep $1 campaign_report.txt | awk -F\' '{print $6}' | cut -c2-
exit 0


Last edited by ryandegreat25; 07-14-2009 at 06:04 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching the content of one file using the search key of another file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search key is... (3 Replies)
Discussion started by: csim_mohan
3 Replies

2. UNIX for Dummies Questions & Answers

Searching for a file...

Hi guys... Macbook Pro, OSX 10.7.5, bash... I want to search for a known file in a totally unknown location. This works perfectly:- capturepath=`find / -name 'sox' 2>/dev/null` Except it is crucyfyingly SSLLOOWW; and I mean SSLLOOWW! Is there any means of searching the entire FS quickly... (5 Replies)
Discussion started by: wisecracker
5 Replies

3. Shell Programming and Scripting

Searching a file inside a .tar.gz file by date

Hi, I would like to ask if there is a way to search for a file inside a .tar.gz file without extracting it? If there is, is there a way to search for that file by date? Thanks! (4 Replies)
Discussion started by: erin00
4 Replies

4. Shell Programming and Scripting

Searching file for pattern, output to file (BASH)

Hello, I'm trying to write a script in Bash to assist in pentesting. Essentially I'm looking to use a script to search for some terms in a log file and then send that key information into another file. The log files consist of HTTP and SSL information that someone creates while browsing and... (2 Replies)
Discussion started by: Sagesparten007
2 Replies

5. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

6. Shell Programming and Scripting

Help in searching a particular string in a file name (not inside the file contents)

Dear Unix Gurus, I am new to shell scripting and in the process of learing. I am trying to find whether a file name has today's date in MMDDYYYY format. I am using the following code and it doesn't seem like working. #!/usr/bin/ksh today=$(date '+%m%d%Y') echo today: $today file=`find... (4 Replies)
Discussion started by: shankar1dada
4 Replies

7. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

8. Shell Programming and Scripting

Searching for Log / Bad file and Reading and writing to a flat file

Need to develop a unix shell script for the below requirement and I need your assistance: 1) search for file.log and file.bad file in a directory and read them 2) pull out "Load_Start_Time", "Data_File_Name", "Error_Type" from log file 4) concatinate each row from bad file as... (3 Replies)
Discussion started by: mlpathir
3 Replies

9. Shell Programming and Scripting

searching a log file and appending to a .txt file

I'm new to shell scripting and am writing a script to help me log the free memory and hd space on a server. As of now, the script just runs 'df -h' and appends the output to a file and then runs 'top' and appends the output to a log file. What I want to do, is have the script also search the... (3 Replies)
Discussion started by: enator45
3 Replies

10. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question