awk for line number 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk for line number 2
# 8  
Old 05-23-2012
He wants you to look for the WORD connected rather than the substring connected which is part of dis"connected". You can declare word boundaries in a regexp.
# 9  
Old 05-23-2012
Well said. :-)
# 10  
Old 05-23-2012
Word boundaries are not specified in POSIX and are not universally available in grep. Most greps do have a -w switch:
Code:
$ echo bla bla connected | grep '\<connected\>'   
$ echo bla bla connected | grep -w 'connected' 
bla bla connected
$

Although -w is not specified in POSIX either.

Last edited by Scrutinizer; 05-23-2012 at 02:26 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 06-01-2012
Quote:
Originally Posted by Scrutinizer
Word boundaries are not specified in POSIX and are not universally available in grep. Most greps do have a -w switch:
Code:
$ echo bla bla connected | grep '\<connected\>'   
$ echo bla bla connected | grep -w 'connected' 
bla bla connected
$

Although -w is not specified in POSIX either.
How do I check if mine has word boundaries? I searched my man page for the word boundaries with no luck. When I ran the command it seemed to do what gary said. Here is my version info.

Code:
grep -V
grep (GNU grep) 2.9
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.

Here is my finished product if anyone is curious Smilie. I use different types of screens for a dual monitor on a regular basis and this makes life a lot easier. Next step is to get this to work in an alias Smilie.

Code:
VAR=$(xrandr | awk '$2=="connected"{s=$1} END{print s}'); xrandr --output $VAR --mode 1024x768 --rate 60; xrandr --output LVDS1 --left-of $VAR; xrandr --output LVDS1 --primary; unset VAR;

# 12  
Old 06-01-2012
From man (GNU) grep (2.5.4):
Code:
   The Backslash Character and Special Expressions
       The  symbols  \< and \> respectively match the empty string at the beginning and end of a word.  The symbol \b matches the
       empty string at the edge of a word, and \B matches the empty string provided it's not at the edge of a word.   The  symbol
       \w is a synonym for [[:alnum:]] and \W is a synonym for [^[:alnum:]].

So yes, GNU grep has word boundaries, so you can use them, unless you want your code to be universal and/or want it to run on other platforms that do not have GNU grep.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

3. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

4. Shell Programming and Scripting

help: Awk to control number of characters per line

Hello all, I have the following problem: My input is two sorted files: file1 >1_19_130_F3 T01220131330230213311013000000110000 >1_23_69_F3 T01200211300200200010000001000000 >1_24_124_F3 T010203113002002111111200002010 file2 >1_19_130_F3 24 18 9 18 23 4 11 4 5 9 5 8 15 20 4 4 7 4... (9 Replies)
Discussion started by: DerSeb
9 Replies

5. UNIX for Dummies Questions & Answers

awk - display from line number to regex

Hi. Is there a way in awk to show all lines between a line number and the next line containing a particular regex? We can do these, of course: awk '/regex1/,/regex2/' filename awk 'FNR > X && FNR < Y' filename But can they be combined? Thanks. (3 Replies)
Discussion started by: treesloth
3 Replies

6. Shell Programming and Scripting

AWK: generate new line number

Hi. I have a script wich reads 1 file and generates 4. If the original file has 10 lines the the sum of the 4 generated files must have the 10 original lines. So far this works. Now what I need is to numerate the lines wtithin each generated file. I tried with NR but it prints the line... (2 Replies)
Discussion started by: mrodrig
2 Replies

7. Shell Programming and Scripting

awk help,line number

I am grep-ing the word "this" in all the files in my dir. $ awk '/this/' * this is this this I want the output as: 1)this is 2)this 3)this How can I achieve this ? Please help. HTH, jkl_jkl (4 Replies)
Discussion started by: jkl_jkl
4 Replies

8. Shell Programming and Scripting

printing a line number using awk

Hi Chaps, I'm trying to print the line number of a comma delimited file where the second field in the line is blank using AWK. Here is the code I have so far where am I going wrong. It is the last column in the file. nawk -v x==0 'BEGIN {FS=",";OFS=","} x++ if ($2 == " ") print $x' bob.tst ... (3 Replies)
Discussion started by: rjsha1
3 Replies

9. Shell Programming and Scripting

awk to select a column from particular line number

The awk command awk -F: '{print $1}' test1 gives the first columns of all the lines in file ,is there some command to get a particular column from particular line . Any help is appreciated. thanks arif (4 Replies)
Discussion started by: mab_arif16
4 Replies

10. Shell Programming and Scripting

Getting line number while using AWK

Using AWK, while I am reading the file, I am separating fields based on the ':' & using NF. I also would like to mention line numbers from the file they are originally from. How would I take out the line number for them? I am trying something like following , awk -F":" '{ j=1 for (i=1;... (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question