Print out a row of data


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print out a row of data
# 1  
Old 03-17-2008
Print out a row of data

hi

im completely new at unix so dont hate me for asking what is probably the easiest question ever.

below is an extract of some data im processing. the first column is like a counter while the second is an ip address. i need to be able to output the ip address based on which has the largest counter number. as you can see the data is already sorted by the size of the counter. hence the output would be 66.249.66.103.

Code:
     65 138.130.16.56
     67 207.46.98.38
     79 65.214.44.112
     82 66.151.181.10
     94 209.191.65.248
     96 207.46.98.37
     97 207.46.98.39
    104 207.46.98.40
    178 66.249.65.167
    457 66.249.66.103

please help me do this, i have no idea how to do it. ALSO...please keep the answer simple otherwise i will just get lost Smilie
thanks
# 2  
Old 03-17-2008
To print the last line of a file, use the "tail" command.

Code:
tail -n 1 filename

Conventionally, we would probably sort so that the top result is at the top, and use "head".

Don't worry about this part, I'm just giving you hints in case you want to develop your skills in the future.

Code:
sort -rn filename | head -n 1

If you want to split off the counter and just see the IP address, you can post-process the output from tail (or head, or any command) with a command like "cut" or "awk" or ... there's a myriad variations. I'll use awk here because it conveniently handles the space-padded format you are using (cut would have a problem with that);

Code:
tail -n 1 filename | awk '{print $2}'

awk is a full-blown scripting language so you could do all of this in awk, but I guess you are overwhelmed already.
era
# 3  
Old 03-17-2008
thanks a lot. that was a big help. yeah i knew that awk could do it all, but i prefer to work through it step by step using the simple commands.

btw this is not related to my previous question, but when setting out unix code are there any ever new lines between pipelines? coz im writing a script and it has 8 pipelines at the moment which tends to get a bit messy. ie what is the correct setting out for longer unix code.
# 4  
Old 03-17-2008
The shell allows you to split pipelines across multiple lines without any continuation character.

Code:
grep fnord /tmp/file |
nl |
sed -e 's/foo/bar/' |
cat -A |
whatever ...

In the general case, it's probably better to post a new question under a different topic title, because many readers only look at the topic titles and only read the ones they know the answer to (or find interesting, or don't know the answer to, depending on who they are and what they do).
era
# 5  
Old 03-17-2008
great. thanks a lot for ur help Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

2. Shell Programming and Scripting

Get row number from file1 and print that row of file2

Hi. How can we print those rows of file2 which are mentioned in file1. first character of file1 is a row number.. for eg file1 1:abc 3:ghi 6:pqr file2 a abc b def c ghi d jkl e mno f pqr ... (6 Replies)
Discussion started by: Abhiraj Singh
6 Replies

3. Emergency UNIX and Linux Support

[Solved] Mysql - Take data from row and copy it to another row

Sorry if I repost my question in this section, but I'm really in a hurry since I have to finish my work... :( Dear community, I have a table with two rows like: Row1 Row2 ======= ======= 7,3 text 1 1,3 text 2 1,2,3 blabla What i need to do is add/copy... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

4. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

5. Shell Programming and Scripting

Print the row element till the next row element appear in a column

Hi all I have file with columns F3 pathway CPS F2 H2 H4 H5 H6 no pathway CMP H7 H8 H9 H10 My expected output is F3 pathway CPS F2 pathway CPS (10 Replies)
Discussion started by: Priyanka Chopra
10 Replies

6. UNIX for Advanced & Expert Users

Convert column data to row data using shell script

Hi, I want to convert a 3-column data to 3-row data using shell script. Any suggestion in this regard is highly appreciated. Thanks. (4 Replies)
Discussion started by: sktkpl
4 Replies

7. Shell Programming and Scripting

Add value of each row when each row has different data ype

Guys -- I am noob and I am really strugging on this one. I cant even write the pseudo code for it. I have a file that spits values in individual rows as such: file: user date type size joe 2005-25-09 log 1 joe 2005-25-09 snd 10 joe 2005-25-09 rcd 12 joe 2005-24-09 log 2 joe... (1 Reply)
Discussion started by: made2last
1 Replies

8. Shell Programming and Scripting

How to insert data befor some field in a row of data depending up on values in row

Hi I need to do some thing like "find and insert before that " in a file which contains many records. This will be clear with the following example. The original data record should be some thing like this 60119827 RTMS_LOCATION_CDR INSTANT_POSITION_QUERY 1236574686123083rtmssrv7 ... (8 Replies)
Discussion started by: aemunathan
8 Replies

9. Shell Programming and Scripting

Trying to print data row/columnwise

PRDCNT=12 FILS= f1=1 f2=2 f3=3 f4=4 f5=5 CNT=0 CNT=`expr $PRDCNT - 5` MNT=6 VR=1 while do f$VR="$f$VR,$MNT" echo "$f$VR" MNT=`expr $MNT + 1` CNT=`expr $CNT - 1` VR=`expr $VR + 1` if then (8 Replies)
Discussion started by: swaminathanks
8 Replies
Login or Register to Ask a Question