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
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 2 04-11-2008 11:32 AM
combining fields in two text fields shocker Shell Programming and Scripting 3 01-16-2008 08:27 AM
Extracting a string from one file and searching the same string in other files mohancrr Shell Programming and Scripting 1 09-19-2007 12:17 AM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 05:53 AM
sed problem - replacement string should be same length as matching string. amangeles Shell Programming and Scripting 4 01-11-2006 03:11 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-29-2001
Registered User
 

Join Date: Nov 2001
Location: Australia
Posts: 6
How do I get the fields after a string

I have a file with long lines of comma separated data and I want the 2 fields following the occurence of a string,
eg1
Input: A, B, C, D, E
Search String: B
Output: C, D

eg2
Input: ABC, DEF, GHI, JKL, MNO, PQR
Search String: GH
Output: JKL, MNO

Any suggestions on how I can do this will be appreciated.
Forum Sponsor
  #2  
Old 11-30-2001
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
Code:
#! /usr/bin/ksh
SEARCH=$1

OSTRING="A, B, C, D, E, F, G, H"
STRING=$(echo $OSTRING | sed s/,//g)
set $STRING

integer found
found=0

while (($# && found<3)) ; do
        if ((!found)) ; then
                [[ $1 = $SEARCH ]] && found=1
        else
                RESULT[found]=$1
                ((found=found+1))
        fi
        shift
done

case $found in
0)      echo $SEARCH not found
        ;;
1)      echo $SEARCH was last item in $OSTRING
        ;;
2)      echo $SEARCH was followed only by ${RESULT[1]}
        ;;
3)      echo ${RESULT[1]}, ${RESULT[2]}
        ;;
esac

exit 0
  #3  
Old 12-04-2001
Neo's Avatar
Neo Neo is offline
Administrator
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 4,493
Looks kinda like one of our gurus just did an homework problem for someone because they are such kind, wonderful forumists ...... we need to be careful not to let students post homework problems to the boards, right?

Simple... A, B, Cs........ are a clue to H/W problems to me......
  #4  
Old 12-04-2001
Registered User
 

Join Date: Nov 2001
Location: Australia
Posts: 6
Sorry Neo not always true, in this case this query is a business related problem I was stuck with, and my other contacts could not help me.

I am polite/cautious because this is my first forum and still getting used to netiquette(?).

I describe the problem a simply as possible to help those who could help me and to leave out the business confidental data.

Sorry if my behaviour has the appearance of a student. I will get into the swing of this with practise.
  #5  
Old 12-04-2001
Registered User
 

Join Date: Nov 2001
Location: Australia
Posts: 6
PS. Here is how I eventually solved this problem after researching the awk on the Internet.

echo "A,B,C,D,E" |awk ' \
{print substr( $0, index($0, "B"), 5)}' |awk ' \
BEGIN {FS=","} \
{print $2, $3}'

However I now have new problem with this solution: The data is sometimes too long for awk , am getting the error,

awk: record `search string' too long
record number 506

Does anyone know how long a string can be?

Also is there any way to split the line into lengths that awk can handle. I'm sure the data I want would be in the first part of the input string.
  #6  
Old 12-04-2001
Neo's Avatar
Neo Neo is offline
Administrator
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 4,493
Still smells like homework.....

What is the practical business problem you are solving?
  #7  
Old 12-05-2001
Registered User
 

Join Date: Nov 2001
Location: Australia
Posts: 6
OK .. OK. Here is some background.
We have an old application for selling Insurance policies. The new replacement application has got to be as quick as the old, which I doubt. I am looking at the old applications log file. I've been asked how many Insurance policies we sell as an indication of the load the new application must handle? I've worked that out (average 5/minute). Now they want to know how much money this represents per minute. So I need the two fields after the word "policy" occurs in the log file so I can caluculate how much each policy is worth.

PS. I've found the command, fold so I am piping the log file through "fold -w 2000" first before using the awk
Google The UNIX and Linux Forums
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:00 AM.


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

Content Relevant URLs by vBSEO 3.2.0