The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[C++] File I/O (Reading from a Random-Access File) VersEtreOuNe High Level Programming 0 02-12-2008 12:34 PM
reading from a file and pass as variables and ignore # in the file konark Shell Programming and Scripting 4 11-07-2007 11:55 PM
Reading a file and writing the file name to a param file. thebeginer UNIX for Advanced & Expert Users 1 10-05-2007 01:38 PM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 01:25 AM
Reading specific contents from a file and appending it to another file dnicky Shell Programming and Scripting 5 10-04-2005 02:45 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-07-2004
Registered User
 

Join Date: Dec 2004
Posts: 3
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
reading file

Hi
I ‘m trying to write a simple script that will be able to extract some useful info from a file of this format:

+ 1.84375 0 2 cbr 210 ------- 0 0.0 3.1 225 610
- 1.84375 0 2 cbr 210 ------- 0 0.0 3.1 225 610
r 1.84471 2 1 cbr 210 ------- 1 3.0 1.0 195 600
r 1.84566 2 0 ack 40 ------- 2 3.2 0.1 82 602
+ 1.84566 0 2 tcp 1000 ------- 2 0.1 3.2 102 611
- 1.84566 0 2 tcp 1000 ------- 2 0.1 3.2 102 611
r 1.84609 0 2 cbr 210 ------- 0 0.0 3.1 225 610
+ 1.84609 2 3 cbr 210 ------- 0 0.0 3.1 225 610
d 1.84609 2 3 cbr 210 ------- 0 0.0 3.1 225 610
- 1.8461 2 3 cbr 210 ------- 0 0.0 3.1 192 511
r 1.84612 3 2 cbr 210 ------- 1 3.0 1.0 196 603
+ 1.84612 2 1 cbr 210 ------- 1 3.0 1.0 196 603
- 1.84612 2 1 cbr 210 ------- 1 3.0 1.0 196 603
+ 1.84625 3 2 cbr 210 ------- 1 3.0 1.0 199 612

For example I want to extract the information in column 18-22 but say only the number values (not necessarily all at once).
Using cut utility (i.e. cut –d 18-22 filename) I get

210
210
210
40 -
1000
1000
210
210
210
210
210
210
210
210
But I only need the value without the - character
In other word for each line I need the field value and I cant use [ cut –f ] because there only separated by spaces.
Any ideas or know of a good Unix tutorial for beginners.
Thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-07-2004
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
cut -d" " -f 6 file1 using cut ....

awk -F" " '{ print $6 }' file1 using awk ...
Reply With Quote
  #3 (permalink)  
Old 12-07-2004
Registered User
 

Join Date: Dec 2004
Posts: 3
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
thanks man very useful, but say i wanna add them all together how do i do that ?
Reply With Quote
  #4 (permalink)  
Old 12-07-2004
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
awk '
BEGIN { sum=0 ; } { sum+=$1 } END { print sum }' x


where x contains all these numbers.
Reply With Quote
  #5 (permalink)  
Old 12-07-2004
Registered User
 

Join Date: Dec 2004
Posts: 3
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
many megabits
thx
Reply With Quote
  #6 (permalink)  
Old 12-10-2004
Registered User
 

Join Date: Aug 2004
Posts: 141
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Something more I need

I am new to awk.

Bhargav, how do you get those numbers stored in variable x?
Because I am on track something similar to this, where I am interested in numbers in a line present at even location.

And this array x need to be populated with unique values.

Also, please feel free to suggest me good links to start with learning awk & sed. So far to my observation they are powerful in terms of their usage in shell script.
Reply With Quote
  #7 (permalink)  
Old 12-10-2004
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
x is the file there from previous o/p.
redirect previous awk to file "x" as follows

awk -F" " '{ print $6 }' file1 > x

and continue with next step ....

awk '
BEGIN { sum=0 ; } { sum+=$1 } END { print sum }' x
Reply With Quote
  #8 (permalink)  
Old 12-10-2004
Registered User
 

Join Date: Aug 2004
Posts: 141
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Red face

I have file somewhat like below,

xxxxxxx:1111:ssssss:2222:yyyyyyyy
xxxxxxx:3333:ssssss:4444:yyyyyyyy
xxxxxxx:3333:ssssss:5555:yyyyyyyy:6666:zzzzzzzz

Out of which I am interested in fields in 'even' locations after delimiting
by char ":".
With following command, I know I can get field at 2nd loc.
awk -F":" '{ print $2 }' BReject > rows

How to loop thr the row to get value at 2nd, 4th ... fields till end of line,
for each particular row?
Reply With Quote
  #9 (permalink)  
Old 12-10-2004
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Use NF to know the number of fields for each line
But in yr case following works ...


awk -F":" '{ print $2,$4,$6,$8 }' testfile
Reply With Quote
  #10 (permalink)  
Old 12-10-2004
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
generic code this time ....


awk -F":" '{
for (i=1; i<= NF; i++)
{
if (i%2 == 0) { printf("%s\t", $i) }
if ( i == 1 ) { printf ("\n") }
}
}' testfile
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash exec bash for loop command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix export command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script rsync ftp scp recursive segmentation fault(coredump) sftp script snoop unix solaris change ip address stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix mtime unix simulator unix.com vi substitute while loop within while loop shell script


All times are GMT -7. The time now is 03:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101