searching for content of files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers searching for content of files
# 15  
Old 03-07-2007
Quote:
Originally Posted by Aretai
thx for answer, but I got only some errors (I use ksh):
ksh: command: bad option(s) Smilie

Do you think it would be possible to make a bash script - new command

mygrep

that would use regular expressions to track "Binary file..." at the beginning and "matches" at the end and replace it with given strings (for example "Found" instead of "Binary file..." and "End" instead of "matches") Smilie

thx in advance Smilie

Regards,
Aretai
Code:
command | sed "s/^.*aretai\([^ ]*\) .*/\1/"

Replace command by the actual command which produce the following text
Code:
Binary file /opt/users/aretai/test/projects/test.txt matches
Binary file /opt/users/aretai/test/projects/test1.txt matches
Binary file /opt/users/aretai/test/projects/test2.txt matches
Binary file /opt/users/aretai/test/projects/test3.txt matches
Binary file /opt/users/aretai/test/projects/test4.txt matches

mygrep
Code:
replacement1=$1
replacement2=$2
sed "s/^Binary file/$replacement1/;s/matches$/$replacement2/" file

Code:
mygrep Found End

# 16  
Old 03-08-2007
Re:

thx,

It works v.good. Except there are some files that r matched and r not Binary.
../projects/test.txt:test

I tried to modify sed command, but failed.
Let me know if this is possible

Aretai
# 17  
Old 03-08-2007
Can you post those sample lines and output you need ?
# 18  
Old 03-08-2007
Re:

Quote:
Originally Posted by anbu23
Can you post those sample lines and output you need ?
OK:

Code:
Binary file /opt/users/aretai/test/projects/test/test.txt matches
Binary file /opt/users/aretai/test/projects/test/test1.txt matches
Binary file /opt/users/aretai/test/projects/test/test2.txt matches
Binary file /opt/users/aretai/test/projects/test/test3.txt matches
Binary file /opt/users/aretai/test/projects/test/test4.txt matches
/opt/users/aretai/test/projects/test.txt:test

The last line is not a binary file but a file created in vi. Output I'd like to generate is:

Code:
test/test.txt
test/test1.txt
test/test2.txt
test/test3.txt
test/test4.txt
test.txt:test

Then I'd like to define mygrep command so that user could run it like this
Code:
mygrep 'test'

and he (or she) would get:

Found test/test.txt End
Found test/test1.txt End
Found test/test2.txt End
Found test/test3.txt End
...
Found test.txt:test End

thx for help
# 19  
Old 03-08-2007
Code:
replacement1=Found
replacement2=End
sed -e "/^Binary/! {s/^/$replacement1 /;s/$/ $replacement2/;s,/.*/,,}" -e "/Binary/{s/^Binary file/$replacement1/;s/matches$/$replacement2/;s,/.*$1/,$1/,;}" f

# 20  
Old 03-09-2007
Re:

Have I missed anything?

s,/.*$1/,$1/,;}" f

What does f at the end mean. And is it for my own command - i.e. mygrep. Can you tell me the steps to code it? Like: where mygrep should be stored?
Do I have to use bash? vi mygrep.sh if so how it can be run by users?

mygrep 'test'?

just some more info your answers r v.good
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Searching content of a variable using awk

so i'm running a variation of the following command a few times in my script: echo "${TOTALRunning}" | awk -F"" '/'"${PROCSEARCH}"'/ {print $2}' | tr '\n' '|' unfortunately i cant paste the content of the variable TOTALRunning into this thread for security reasons. what i want to do is... (9 Replies)
Discussion started by: SkySmart
9 Replies

3. 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

4. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

5. Shell Programming and Scripting

search for content in files. Name of files is in another file. Format as report.

Hi I have multiple files in a folder and one file which contains a list of files (one on each line). I was to search for a string only within these files and not the whole folder. I need the output to be in the form File1<tab>string instance 2<tab> string instance 2<tab>string instance 3... (6 Replies)
Discussion started by: pkabali
6 Replies

6. Shell Programming and Scripting

Searching across multiple files if pattern is available in all files searched

I have a list of pattern in a file, I want each of these pattern been searched from 4 files. I was wondering this can be done in SED / AWK. say my 4 files to be searched are > cat f1 abc/x(12) 1 abc/x 3 cde 2 zzz 3 fdf 4 > cat f2 fdf 4 cde 3 abc 2... (6 Replies)
Discussion started by: novice_man
6 Replies

7. Shell Programming and Scripting

Files searching

I have a list of files in directory and i should write a script if any of these files contains words given in a text file test.txt. the words can be case ignored and word should match. The output should be the name of the directory in which the file is present followed by list of file names Eg:... (1 Reply)
Discussion started by: kinny
1 Replies

8. Shell Programming and Scripting

searching content of files in the current and sub directories

Hi I was wondering why command 2 doesn't work like command 1 below. 1. find . -exec grep "test" '{}' \; -print 2. ls -R | grep "test" I am trying to search "test" from all the files in the current and sub directories. What's wrong with my command 2? Thanks in advance for your help (4 Replies)
Discussion started by: tiger99
4 Replies

9. Shell Programming and Scripting

Searching and Removing File Content

Hi, I am trying to search a character in a file and remove it from that file.... My file looks something like this: test1.txt ckj12300_00|123|var1|10.2 ckj00200_12|444|var2|11.2 ckj00200_14|4556|var3|33.5 c00200_00_000|4558|var4|33.5 ckj00200_14|4553|var5|33.5... (7 Replies)
Discussion started by: rkumar28
7 Replies

10. UNIX for Dummies Questions & Answers

Searching files..?

hi there can anyone tell me how to search and copy files under unix? im writing shell scripts with 'vi' and 'pico' something like read directoryName if then echo Copying the files copy those *.src files to sub1(another directory) using cp else ... (4 Replies)
Discussion started by: nickaren
4 Replies
Login or Register to Ask a Question