awk for line number 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk for line number 2
# 1  
Old 05-19-2012
awk for line number 2

This is what I have so far.

Code:
xrandr | grep connected | grep -v disconnected | awk '{print $1}'

This is my output.

Code:
LVDS1
TV1

How can I awk for line number 2. The only output I want is TV1.
# 2  
Old 05-19-2012
Try:
Code:
xrandr | awk '$2=="connected"{s=$1} END{print s}'

( or stick | tail -1 onto the end of your string of commands )
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-19-2012
and
Code:
| sed -n '2p'

works also.
# 4  
Old 05-21-2012
Quote:
Originally Posted by Scrutinizer
Try:
Code:
xrandr | awk '$2=="connected"{s=$1} END{print s}'

( or stick | tail -1 onto the end of your string of commands )
That works great Smilie. Can you please explain how it ignores "LVDS1". I know it checks field 2 for connected with $2=="connected", then it stores field 1 this {s=$1}, then it prints field 1 with this END{print s}. I just can't figure out how it ignores "LVDS1".

Quote:
Originally Posted by Habitual
and
Code:
| sed -n '2p'

works also.
When I run this I get:
Code:
$ xrandr | grep connected | grep -v disconnected | sed -n '2p'
TV1 connected (normal left inverted right x axis y axis)

Did you do something else? I would still have to pass it to awk because I want TV1.
# 5  
Old 05-21-2012
Hi, it does not ignore LVDS1, while read the file it stores field 1 of each "connected" line in variable s. So after all lines have been read, s will contain the last value that was found. Then in the END section this variable will get printed..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 05-22-2012
An aside to save a process. Instead of:
Code:
grep connected | grep -v disconnected

you can do:
Code:
grep '\<connected\>'

# 7  
Old 05-22-2012
Quote:
Originally Posted by gary_w
An aside to save a process. Instead of:
Code:
grep connected | grep -v disconnected

you can do:
Code:
grep '\<connected\>'

Can you please elaborate? I don't understand what you are saying.
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