Seeking help with search


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Seeking help with search
# 1  
Old 10-31-2014
Seeking help with search

Hello All,

I'm looking for some help with grepping for two specific strings in files with multiple lines. For instance, I have files and the content looks like this:

Code:
=====Start=====
Record:1
        Field 1 = aaaaaaaaaa
        Field 2 = bbbbbbbbbb
        Field 3 = 1234567890
        Field 4 = 0000000123
        Field 5 = ccccccccccc
=====End=======

I would like to search through the file looking for 1234567890. I can easily grep on that but I also need the value of field 4 (and I don't know the value of field 4) and I need to out put this to a text file. Essentially the output text file should contain only:

Code:
        Field 3 = 1234567890
        Field 4 = 0000000123

How do I search for this?

Code:
for file in *
 do
 grep 1234567890
 ?????
 done > results.txt


Last edited by bbbngowc; 10-31-2014 at 09:52 PM..
# 2  
Old 10-31-2014
Unless there are LOTS of files, try:
Code:
grep -F -e 1234567890 -e "Field 4" * > results.txt

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-31-2014
Thanks for the response.

I was just looking at the contents of the files further and looks as though I'll need something a little different. It appears as though 1234567890 can appear in field 3 or field 4. Smilie

So I'm thinking perhaps an "IF" statement? Something like...

Code:
if Field3=1234567890
 then print Field3 and Field4
else if field4=1234567890
 then print Field3 and Field4
fi

# 4  
Old 10-31-2014
awk somewhat resembles grep on steroids, it scans lines but is a proper language with variables and columns and things.
Code:
awk '($3 == V) || ($4 == V) { print $3,$4 }' V=1234567890 inputfile


Last edited by Corona688; 10-31-2014 at 10:27 PM..
# 5  
Old 10-31-2014
Try:
Code:
sed -n '/Field 3/{N;/1234567890/p;}' file*

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 10-31-2014
Quote:
Originally Posted by Corona688
awk somewhat resembles grep on steroids, it scans lines but is a proper language with variables and columns and things.
Code:
awk '($3 == V) || ($4 == V) { print $3,$4 }' V=1234567890 inputfile

I must be doing it wrong. It's not returning a value. A simple grep echos the 1234567890 value so I know it's in there, but the awk statement is not finding it.

$3 and $4 are representations of Fields 3 and 4 correct?

---------- Post updated at 10:02 PM ---------- Previous update was at 09:49 PM ----------

This appeared to have worked for what I want

Code:
grep -B 1 -A 1 "1234567890" *.txt

I didn't know grep had a B (before) and A (after) option that allows you to print the line before and the line after the search string.
# 7  
Old 10-31-2014
Yes but that is equivalent to (GNU) grep -C 1 and it will show 3 lines per match instead of two..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Seeking Alternative for diff in hp

Hi , I have to use HP-unix OS to get difference between 2 files.while i tried a piece of code in other OS(linux/unix) as below, it worked fine & the output is desired one. diff --suppress-common-lines -y file_1 file_2 >d.txt The same doesn't works in HP -unix. Any help shall be... (6 Replies)
Discussion started by: vinil
6 Replies

2. Shell Programming and Scripting

[grep] seeking more than one word

Hello everybody, How can I do seek several words with grep command ? I didn't find how to do that in the man for I'm French. Then I tried man bash | grep -in control flow But flow is interpreted as an input file Then I tried man bash | grep -in "control" -a "flow" but doesn't seem to work... (4 Replies)
Discussion started by: Oddant
4 Replies

3. Programming

Seeking Some libs for AIX 5.3

hello everybody! I m compiling some program with the g++ on AIX 5.3 and it needs some library that i didn't find them i am new with the AIX here is the compilation error : g++ -Daix -fpic -o printps -lxercesc1_1 -L/oracle/OraHome/lib32/ -L/epost2/blitz/lib -lhmltods -lhmltops -lgeneric... (0 Replies)
Discussion started by: eternalflame
0 Replies

4. Shell Programming and Scripting

seeking help with shell script

I am trying to update a script which I had created to monitor tablespace usage. Originally the sql spooled out to a text file anything with more than 75% used. I have been asked to change this. Now the sql must spool out all tablespaces. The script I have to write should scan the file for... (4 Replies)
Discussion started by: Niadh
4 Replies

5. UNIX for Dummies Questions & Answers

Seeking help...Urgent!!! Please help me...

Can any1 please help me answer a couple of this question? 1) What is the process management of UNIX? (single task, multitasks, etc...) 2) What is the process management of Linux? (single task, multitasks, etc...) 3) What is the type of process of UNIX? (process, thread, etc...) 4)... (1 Reply)
Discussion started by: blind02002
1 Replies

6. UNIX for Dummies Questions & Answers

Seeking UNIX documentation

Hi, I am looking for documentation (PDF, DOC, books, web ...) about UNIX scripts. What could you advice me ? Thanks. (3 Replies)
Discussion started by: Filippo
3 Replies

7. Shell Programming and Scripting

awk: seeking to bytes

can I seek to a particular byte in a file and replace it using awk? if so, how? (8 Replies)
Discussion started by: karyn1617
8 Replies

8. UNIX for Dummies Questions & Answers

Control Panel seeking.

Hi, guys. My friends and I used to rent space from our ISP, and they applied Cpanel to help us to config. Recently we just upgrade to the dedicated server plan, and there is nothing but only the os has been installed. Since Cpanel is the commercial software, we cannot afford it, we need to find a... (2 Replies)
Discussion started by: HOUSCOUS
2 Replies

9. Filesystems, Disks and Memory

seeking a doc on lvm etc...

Hi. I wanted to increase my knowledge base with unix. can you help me find a like that talks about luns, physical volumes, volume groups, logical volumes then mountpoints? i would appreciate it thanks Jigar (1 Reply)
Discussion started by: jigarlakhani
1 Replies
Login or Register to Ask a Question