how to grep a specific pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to grep a specific pattern
# 1  
Old 02-12-2009
how to grep a specific pattern

Hi
I have a file in the following format:
ftrn facc ttrd feed xref
#1 1365 1366 1367 1097 1098
#2 1368 1369 1370 1095 1096
#3 1371 1372 1373 1065 1066
#4 1374 1375 1376 1099 1100
#5 1377 1378 1379 1080 1081
#6 1380 1381 1382 1101 1102
#7 1383 1384 1385 1331 1332
etc....

Is there a way/commands to retrieve the number which is located on the cross of let's say #5 and feed , which is 1080. May be somebody did something similar before. Thanks a lot for any help or advice -A
# 2  
Old 02-12-2009
correction the format of the file looks like that:
ftrn facc ttrd feed xref
#1 1365 1366 1367 1097 1098
#2 1368 1369 1370 1095 1096
#3 1371 1372 1373 1065 1066
#4 1374 1375 1376 1099 1100
#5 1377 1378 1379 1080 1081
#6 1380 1381 1382 1101 1102
#7 1383 1384 1385 1331 1332
etc....
# 3  
Old 02-12-2009
Sorry, the the file looks like that:
.....ftrn facc ttrd feed xref
#1 1365 1366 1367 1097 1098
#2 1368 1369 1370 1095 1096
#3 1371 1372 1373 1065 1066
#4 1374 1375 1376 1099 1100
#5 1377 1378 1379 1080 1081
#6 1380 1381 1382 1101 1102
#7 1383 1384 1385 1331 1332
etc....
# 4  
Old 02-12-2009
you have FOUR entries in 'header': "ftrn facc ttrd feed xref"
and you have FIVE fields per line/record.

What's missing?
Are the leading '#' (#1, #2, etc) really in a file?

Please use vB Codes when posting data and/or code samples.
# 5  
Old 02-12-2009
Hammer & Screwdriver A solution, assuming those are spaces between fields

Code:
> cat file168
ftrn facc ttrd feed xref
#1 1365 1366 1367 1097 1098 
#2 1368 1369 1370 1095 1096 
#3 1371 1372 1373 1065 1066 
#4 1374 1375 1376 1099 1100
#5 1377 1378 1379 1080 1081
#6 1380 1381 1382 1101 1102
#7 1383 1384 1385 1331 1332

> cat find168.sh
#! /usr/bin/bash

ROWV="5"
COLV="feed"
FILEIN="file168"

# determine the column to match $COLV
COLP=`head -1 ${FILEIN} | tr " " "\n" | cat -n | grep "${COLV}" | awk '{print $1}'`
COLP=`echo $COLP+1 | bc`

# extract from the row $ROWV
grep "^#${ROWV}" ${FILEIN} | cut -d" " -f ${COLP}
 
> find168.sh
1080

# 6  
Old 02-12-2009
Assuming input file (ao.txt) looks like:
Code:
ftrn facc ttrd feed xref
1365 1366 1367 1097 1098
1368 1369 1370 1095 1096
1371 1372 1373 1065 1066
1374 1375 1376 1099 1100
1377 1378 1379 1080 1081
1380 1381 1382 1101 1102
1383 1384 1385 1331 1332

To get the 5-th row and the column under the 'feed' heading:

nawk -v row=5 -v col='feed' -f ao.awk ao.txt

ao.awk:
Code:
FNR==1 { for(i=1; i<=NF;i++) header[$i]=i; next }

FNR==row+1 { print $header[col]; exit }

# 7  
Old 02-12-2009
Code:
#!/usr/bin/perl
sub get{
	my($file,$line,$col)=(@_);
	open FH,"<$file";
	while(<FH>){
		chomp;
		if($.==1){
			my @temp=split(" ",$_);
			for($i=0;$i<=$#temp;$i++){
				if($col eq $temp[$i]){
					$num=$i;
					last;
				}
			}
		}
		if($.==$line+1){
			my @temp=split(" ",$_);
			print $temp[$num+1],"\n";
		}
	}
}
get("a.txt",5,"feed");

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

Grep pattern after specific line number in a file

Hi guys, I am running a while loop in a script ro read a file line by line. Now I want to run a grep only on the lines below the line I am that is being read by the while loop. Eg: If my while loop is on line 4 of the file, the grep only runs below line 4 and does not include line 1,2... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

4. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

5. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

6. Shell Programming and Scripting

How can i use grep to search a specific pattern?

Hi All, My file contain the below data : w_SA_infa1.log:INFO : LM_36620 : () Command task instance : running command , with command value . Binary file w_SA_infa1.log.bin matches w_SA_infa2.log:INFO : LM_36620 : (30377|1427806528) Command task instance : running command , with command value... (1 Reply)
Discussion started by: aliva Dash
1 Replies

7. UNIX for Dummies Questions & Answers

How to Detect Specific Pattern and Print the Specific String after It?

I'm still beginner and maybe someone can help me. I have this input: the great warrior a, b, c and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this : Warrior Type a b c Im still very... (3 Replies)
Discussion started by: radynaraya
3 Replies

8. UNIX for Dummies Questions & Answers

pattern search using grep in specific range of files

Hi, I am trying to do the following: grep -l <pattern> <files to be searched for> In <files to be searched for> , all files should of some specific date like "Apr 8" not all files in current directory. I just to search within files Apr 8 files so that it won't search in entire list of... (2 Replies)
Discussion started by: apjneeraj
2 Replies

9. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies

10. Shell Programming and Scripting

Want to grep for a pattern and display the content above that pattern

Hi, When we have a failure, sometimes we just step restart the job from the next step. Later when we open the log for analysis of the failure, it is becoming difficult to go to the failure part. For eg., if it is a 1000 line log, the failure may be at 500th line. so wat i want to do is, grep... (6 Replies)
Discussion started by: ajayakunuri
6 Replies
Login or Register to Ask a Question