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

Lots of ways:

awk -F":" '{print $1}' file
cut -d':' -f1 file

Just pick the field you'd like to print ( i.e. $1, $2, or -f1 -f2).

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

You can do something like that :

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

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

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

Jean-Pierre.

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?

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.

Python alternative:

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

dear All,

I have file name : Report.log

02/10/2006 [08:00:02] report from System
IWF problem , critical, 20 minutes
problem : Server down
02/10/2006 [08:05:12] report from system
Node down, minor, 5 minutes
problem : link down
02/10/2006 [20:15:04] report from system
IWF problem, major, 30 minutes
problem : System reload
02/10/2006 [23:00:42] report from system
IWF problem, critical , 5 minutes
problem : Server down

.........

I just want get all IWF problem from this file, but contain date, and problem reason and make 1 file that name "IWFPROBLEM" for example in this file just contain
....
02/10/2006 [20:15:04] report from system
IWF problem, major, 30 minutes
problem : System reload
02/10/2006 [23:00:42] report from system
IWF problem, critical , 5 minutes
problem : Server down
....

I was try using grep but not work.. :slight_smile:

Thank's for your help

Regrads,

Heru

Python alternative:

#!/usr/bin/python
data = open("report.log").readlines()
o = open("IWFPROBLEM","a") #open file for appending
for i in range(len(data)):
 	if "IWF problem" in data:
 		print >>o, ''.join(data[i-1:i+2])

output:

02/10/2006 [08:00:02] report from System
IWF problem , critical, 20 minutes
problem : Server down

02/10/2006 [20:15:04] report from system
IWF problem, major, 30 minutes
problem : System reload

02/10/2006 [23:00:42] report from system
IWF problem, critical , 5 minutes
problem : Server down

Thank's Ghostdog74,

but I don't have phyton.. :slight_smile:
can you give example script..

Regards,

Heru

#!/bin/bash

state=0
while read line
do
if [ $state -eq 1 ]
then
echo -e "$first\n$second\n$line\n" >>report.log
state=0
continue
fi

    echo $line | grep "IWF problem" 1>/dev/null 2>&1
    if [ $? -eq 0 ]
    then
            first=$last
            second=$line
            state=1
    fi

    last=$line

done<report.log

sed -n "N;/IWF problem/{p;n;p;};"