The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Removing parts of a specific field kieranh Shell Programming and Scripting 9 09-28-2007 01:52 PM
removing parts of a line with SED gammaman UNIX for Dummies Questions & Answers 2 08-05-2005 04:44 AM
filter parts of a big file using awk or sed script apalex Shell Programming and Scripting 1 07-25-2005 05:45 PM
hiding parts of processes dangral UNIX for Dummies Questions & Answers 2 05-29-2003 07:57 PM
cksum parts of a file crazykelso UNIX for Dummies Questions & Answers 6 07-30-2002 12:38 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-07-2006
bebop1111116 bebop1111116 is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 30
getting parts of a file

Hello, I'm trying to retreive certain bits of info from a file.

the file contains a list like this
info1:info2:info3:info4
info1:info2:info3:info4
info1:info2:info3:info4
info1:info2:info3:info4

how do i pick out only info2 or only info3 without the others?

Thanks
  #2 (permalink)  
Old 10-07-2006
petebear petebear is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 23
Lots of ways:


Code:
awk -F":" '{print $1}' file


Code:
cut -d':' -f1 file

Just pick the field you'd like to print ( i.e. $1, $2, or -f1 -f2).
  #3 (permalink)  
Old 10-07-2006
bebop1111116 bebop1111116 is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 30
hey thanks, one more question though. How do i tell it to keep doing this until the end of the file? Im going to make a while loop, but what would i put for the loop.

thanks
  #4 (permalink)  
Old 10-07-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433
You can do something like that :

Code:
cut -d':' -f1 file |
while read field
do
    echo "Input field: $field"
done

If can also do all the work with the shell (KSH) :

Code:
 while FS=: read field1 field2 other_fields
do
   echo "Field1: $field1"
   echo "Field2: $Field2"
done


Jean-Pierre.
  #5 (permalink)  
Old 10-07-2006
bebop1111116 bebop1111116 is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 30
I just want to make sure i understand these correct. Does it take the first line and process it, then go back to the beginning and process line two and then go back and so on and so on? Or does it do it for all the lines at the same time?
  #6 (permalink)  
Old 10-07-2006
petebear petebear is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 23
Well, either of the two commands I gave will will work on the entire file. I don't see any reason why you'd need a loop just to print your chosen field.
  #7 (permalink)  
Old 10-07-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,556
Quote:
Originally Posted by bebop1111116
hey thanks, one more question though. How do i tell it to keep doing this until the end of the file? Im going to make a while loop, but what would i put for the loop.

thanks
Python alternative:

Code:
for lines in open("input.txt"):
 	lines = lines.strip().split(":")
 	print "Info 2 , second column: " , lines[1]
 	print "Info 3 , third column: " , lines[2]

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0