Help with grep at specific field of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with grep at specific field of a file
# 1  
Old 10-20-2010
Help with grep at specific field of a file

hi,

I would like to search for a specific string at a specific line in a file and would like to know the count of it.

eg: samp.txt
Code:
ABC 1234 BELL
HNZ 4567 RING
NNN 5673 BELL

Please help with the command to find the count of BELL in the above file samp.txt at the specific line 10 with the length 4.

Thanks

Moderator's Comments:
Mod Comment Use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. You should be familiar with them having so many posts.

Last edited by techmoris; 10-20-2010 at 12:21 PM..
# 2  
Old 10-20-2010
Code:
cut -c10-13 samp.txt | grep BELL | wc -l


Last edited by ctsgnb; 10-20-2010 at 12:26 PM.. Reason: typo
# 3  
Old 10-20-2010
Using Perl:-


Code:
perl -wlane '
if (/\bBELL\b/ && $.==10 ) {
            map {$_ =~ /\bBELL\b/ and $i++ } @F ;
            print $i ;
            $i=0
}

'  sample.txt

SmilieSmilieSmilie

---------- Post updated at 18:30 ---------- Previous update was at 18:28 ----------

Quote:
Originally Posted by ctsgnb
Code:
cut -c10-13 samp.txt | grep BELL | wc -l


will not work if you have the word "BELLLLLLL" on the lines of the infile.

he specify that the word total length is 4.
# 4  
Old 10-20-2010
Code:
awk ' substr($0,10,4) ~ /BELL/ { ++cnt } END { print cnt } ' file

# 5  
Old 10-21-2010
@ahmad.diab
will not work if you have the word "BELLLLLLL" on the lines of the infile.

he specify that the word total length is 4.


Where did he mention that the 3rd field could exceed the 4 length ?

I based my code on the sample he gave , so if this sample does not represent all the cases he can get, then he has to give more clue.

The problem was not "i want to check that my 3rd field has a 4 character length"
# 6  
Old 10-21-2010
Quote:
Originally Posted by techmoris
hi,

I would like to search for a specific string at a specific line in a file and would like to know the count of it.

eg: samp.txt
Code:
ABC 1234 BELL
HNZ 4567 RING
NNN 5673 BELL

Please help with the command to find the count of BELL in the above file samp.txt at the specific line 10 with the length 4.

Thanks

Moderator's Comments:
Mod Comment Use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. You should be familiar with them having so many posts.


see above BOLD line.
# 7  
Old 10-21-2010
Maybe it's my english which is too bad ... i think i just don't understand the sentence in the same way than you:

when he wrote "Line 10", i thought it was a typo error and that we should read "char 10" instead (indeed, that was matching the sample he gave)

By the way, i read the bold line and he explicitly talk about 4 length, he never specified it could be larger .

Or am I missunderstanding something ?

I understood he wanted to count the number of BELL occurence appearing in field starting at char 10 and have a length of 4
(just as it appear in his example).
I didn't figure his problem was about having BELLLLLLLLLLLLL occurrence (if so, i suppose he would have put such an entry in the sample)

You are true that my code does not handle with BELLLLLLLLLLLLL : it count them as if it were a BELL one.
I just assumed that his input file was a file formatted the same way than the sample he gave.

Last edited by ctsgnb; 10-21-2010 at 12:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add tab after digit in specific field in file

I am trying to add a tab after the last digit in $3 in the input. The grep below is all I can think off. Thank you :) sed -n 's/:/&/p' input input chr1 955542 955763AGRN-6|gc=75 chr1 957570 957852AGRN-7|gc=61.2 chr1 976034 976270AGRN-9|gc=74.5 desired output chr1... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

awk to remove lines in file if specific field matches

I am trying to remove lines in the target.txt file if $5 before the - in that file matches sorted_list. I have tried grep and awk. Thank you :). grep grep -v -F -f targets.bed sort_list grep -vFf sort_list targets awk awk -F, ' > FILENAME == ARGV {to_remove=1; next} > ! ($5 in... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

File Parsing based on a character in a specific field

Hi All, I'm having a hard time finding a starting point for my issue. I have a 30k line file (fspsec.txt) that I would like to parse into smaller files based on any character existing in field 1. ACCOUNTANT LEVEL 1 (ACCT.ACCOUNTANT) OPERATORS: DOEJO (418) TOOLS: Branch Maintenance ... (2 Replies)
Discussion started by: aahlrich
2 Replies

4. Shell Programming and Scripting

Update specific field in a line of text file

I have a text file like this: subject1:LecturerA:10 subject2:LecturerA:40 if I was given string in column 1 and 2 (which are subject 1 and LecturerA) , i need to update 3rd field of that line containing that given string , which is, number 10 need to be updated to 100 ,for example. The... (6 Replies)
Discussion started by: bmtoan
6 Replies

5. Shell Programming and Scripting

convert specific field to upper case in file

Hi Friends, I have a file1.txt as below I want to convert the date fields in to upper case field3 and field 6 output.txt Plz help. (2 Replies)
Discussion started by: i150371485
2 Replies

6. Shell Programming and Scripting

Combine multiple lines in file based on specific field

Hi, I have an issue to combine multiple lines of a file. I have records as below. Fields are delimited by TAB. Each lines are ending with a new line char (\n) Input -------- ABC 123456 abcde 987 890456 7890 xyz ght gtuv ABC 5tyin 1234 789 ghty kuio ABC ghty jind 1234 678 ght ... (8 Replies)
Discussion started by: ratheesh2011
8 Replies

7. Shell Programming and Scripting

replacing field in specific line in a file

Hi, I know there are lots of threads on replacing text within files, usually using sed or awk. However, I find it hard to adapt examples that I found to my specific case. I am kind of new to UNIX and have hard times learning the syntax either for sed or awk so I would appreciate any help. Here's... (5 Replies)
Discussion started by: vytenis
5 Replies

8. Shell Programming and Scripting

Extract data into file with specific field specs

:confused: I have a tab delimited file that I need to extract data from and into a file with specific field specs. Each field has to be a certain amount of characters. So, the name field (from delimited file) might have only 15 characters but needs to be 25 (in new file) so I need to insert spaces... (5 Replies)
Discussion started by: criddel
5 Replies

9. UNIX for Dummies Questions & Answers

how to grep from specific field

the file has several date fields.for example, #2, #3,and #5 are date fields. I want grep year '2002' just within #2 field and get all the rows which content year '2002' in the #2 field. is it possible to do this? how to do it? Thanks (2 Replies)
Discussion started by: tiff-matt
2 Replies

10. UNIX for Dummies Questions & Answers

Grep for NULL for a specific field in a unix file

hii.. I have a pipe delimited file For ex: TEST_1-8J6TZN|ASLANK|BHR-09||TRAM I want to grep for NULL in the 4th field only from the above file Please suggest Also let me know if there is any limitation in the suggested command Thanks all in advance..!! (5 Replies)
Discussion started by: sureshg_sampat
5 Replies
Login or Register to Ask a Question