Grab characters following grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grab characters following grep
# 1  
Old 07-13-2010
Grab characters following grep

Hi all,

I'm trying to gather data directly following a keyword in a file but I have no guarantee where it will appear in said file so I can't use cut or anything else that assumes it will be located at a certain position. If anyone has any suggestions I'd be grateful. Here is an example of the line:
MODStandard , Building=3A 1234, Syntax ID=3B=

In this example I want to search for the keyword "Building" and then grab the four characters "1234" that appear to the right of it.

Thanks in advance.
# 2  
Old 07-13-2010
Code:
$ echo "MODStandard , Building=3A 1234, Syntax ID=3B=" | sed "s/.*Building[^,]*\([^, ]\{4\}\),.*/Building=\1/"
Building=1234

# 3  
Old 07-13-2010
Quote:
Originally Posted by anbu23
Code:
$ echo "MODStandard , Building=3A 1234, Syntax ID=3B=" | sed "s/.*Building[^,]*\([^, ]\{4\}\),.*/Building=\1/"
Building=1234

Thanks but I can't get it to function in production. Running it as:
grep Building file.dat | sed "s/.*Building[^,]*\([^, ]\{4\}\),.*/Building=\1/"

...just spits out the whole line instead of just the section like your example shows.

Am I not understanding the syntax correctly?
# 4  
Old 07-13-2010
Code:
#!/bin/bash
# bash 3++
while read -r LINE
do
  case "$LINE" in
    *Building*)
        LINE=${LINE##*Building=}
        echo ${LINE%%,*};;
  esac
done <"file"

# 5  
Old 07-13-2010
Code:
awk '/Building/ {sub(/,/,"",$4); print $4}' urfile

# 6  
Old 07-14-2010
Quote:
Originally Posted by rdcwayx
Code:
awk '/Building/ {sub(/,/,"",$4); print $4}' urfile

Can you explain the syntax to me? Thanks.
# 7  
Old 07-14-2010
Quote:
Originally Posted by Korn0474
Can you explain the syntax to me? Thanks.
awk splits up the input line into tokens $1, $2, $3, ... etc.; so for this line

Code:
MODStandard , Building=3A 1234, Syntax ID=3B=

$4 equals the string "1234,".

awk's "sub" function performs a replacement on a target string. So

Code:
sub(/,/,"",$4);

replaces the comma (in the regex pattern /,/) by zero-length string (2nd argument "") in the target string $4 (which is "1234,"). Thus $4 turns into "1234" which is then printed.

The construct

Code:
/Building/ {<action>}

performs the action <action> only for lines that have the literal "Building" in them.

A Perl one-liner for the same problem would be:

Code:
$
$ cat f2
blah
MODStandard , Building=3A 1234, Syntax ID=3B=
blah blah
$
$ perl -lne '/Building.*? (\d+),/ and print $1' f2
1234
$
$

HTH,
tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I use grep to grab prime number output from my factor program?

I have a factor program that runs and outputs to stdout all the prime numbers that are specified in the given paramters, in this case 30000000-31000000. Command: factor/factor 30000000-31000000 Sample output: 30999979 = 30999979 30999980 = 2^2 5 11 140909 30999981 = 3 10333327... (6 Replies)
Discussion started by: steezuschrist96
6 Replies

2. Shell Programming and Scripting

Grep with ... matching more than 3 characters

I am trying to understand what the grep command in ubuntu is trying to do here. The contents of my test file is given below harsha@harsha-H67MA-USB3-B3:~/Documents$ cat data abcd efghi jklmno pqr stuv wxyz When I grep for 3 dots (...) without the parenthesis as follows I would expect the... (4 Replies)
Discussion started by: sreeharshasn
4 Replies

3. Shell Programming and Scripting

grep particular characters help

Hello folks i have file which is below, i want to extract last column of file that contains dm-21 or dm-13 or dm-N it show output like I have tried but i got this (16 Replies)
Discussion started by: learnbash
16 Replies

4. Shell Programming and Scripting

grep grab 19 letters from now or a full line

Hi, I have a file like this >hg19_chr1_123_456_+ asndbansbdahsjdbfsjhfghjdsghjdghjdjhdghjjdkhfsdkjfhdsjkdkjghkjdhgfjkhjfkf hasjgdhjsgfhjdsgfdsgfjhdgjhdjhdhjdfhjdfjgfdfbdghjbfjksdhfjsfdghjgdhjgfdjhgd jhgdfj >hg19_chr1_123_456_-... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

5. Shell Programming and Scripting

Grep with special Characters

Need Help For GREP I have a file say g1.txt and content of file is below REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v NoDrives /t REG_DWORD /d 4 /f , REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v NoClose /t REG_DWORD /d 1 /f ,... (4 Replies)
Discussion started by: jalpasoni
4 Replies

6. UNIX for Dummies Questions & Answers

Is it possible to grep a certain amount of characters?

I have a file similar to the following filler filler filler car 6 mazda filler filler filler filler car civic honda car rav 4 toyota filler filler If i do a "grep -i car file.txt" the output would be car 6 mazda car civic honda car rav 4 toyota however, i want to have the... (4 Replies)
Discussion started by: jl487
4 Replies

7. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

8. UNIX for Dummies Questions & Answers

Using grep to find one or more characters

Hi I am a little stuck using grep. I want to be able to find from a file all lines which have the sequence of characters t, followed by any character, followed by the characters ing. I have tried looking at the man pages for help, but did not understand it correctly (as it is not the most... (9 Replies)
Discussion started by: rushhour
9 Replies

9. UNIX Desktop Questions & Answers

grep with special characters

Hi there I need to grep for a detail from a file. The pattern to search for involves escape sequences in it. This causes for the problem. grep "P\_SOME\_STRING\_SEARCH" filename Note, I have line like below in the file and expect it to grep. select * from my_system_param ... (3 Replies)
Discussion started by: guruparan18
3 Replies

10. UNIX for Dummies Questions & Answers

Grep and extract the characters after

Hi All, I have lines like below in a file A /u/ab/test1.dat A/u/ab/test2.dat A /u/bb/test3.dat A/u/cc/test4.dat I will need /u/ab/test1.dat /u/ab/test2.dat /u/bb/test3.dat /u/cc/test4.dat Pls help Thanks (6 Replies)
Discussion started by: thumsup9
6 Replies
Login or Register to Ask a Question