Visit Our UNIX and Linux User Community


How do I get the fields after a string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How do I get the fields after a string
# 1  
Old 11-29-2001
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.
# 2  
Old 11-30-2001
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
Looks kinda like one of our gurus just did an homework problem for someone because they are such kind, wonderful forumists Smilie ...... 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
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
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-05-2001
Still smells like homework..... Smilie

What is the practical business problem you are solving?
# 7  
Old 12-05-2001
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
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #610
Difficulty: Medium
Compared to Python, Perl has a relatively small Comprehensive Perl Archive Network (CPAN) library.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

awk partial string match and add specific fields

Trying to combine strings that are a partial match to another in $1 (usually below it). If a match is found than the $2 value is added to the $2 value of the match and the $3 value is added to the $3 value of the match. I am not sure how to do this and need some expert help. Thank you :). file ... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

4. Shell Programming and Scripting

Need a separator string between fields in cut -c command

Hi All, I'm trying to view data using cut command for a fixed length file using the below command: cut -c 1-3,4-5 FALCON_PIS_00000000.dat I want to mention a separator say | (pipe) in between 1-3 and 4-5. Please let me know how to achieve this. Thanks in Advance, (3 Replies)
Discussion started by: HemaV
3 Replies

5. Shell Programming and Scripting

Identifying entries based on 2 fields in a string.

Hi Guys, I’m struggling to use two fields to do a duplicate/ unique by output. I want to look IP addresses assigned to more than one account during a given period in the logs. So duplicate IP and account > 1 then print all the logs for that IP. I have been Using AWK (just as its installed... (3 Replies)
Discussion started by: wabbit02
3 Replies

6. Shell Programming and Scripting

How to read a delimited string and assign fields to incremented variables?

How can I read a string delimited on spaces and assign the fields to incremented variables. For example: Given $exts= txt dat mov I want to read in $exts and have "txt" "dat" and "mov" assigned to incremented variables like $ext1, $ext2, etc. I would like to do this in a loop so that I can... (4 Replies)
Discussion started by: runit
4 Replies

7. Shell Programming and Scripting

mappin strings of two different file and finding the mapped string and then map other fields.

As i am new to unix so facing some problems in scripting: here is my question: i m having two files. 1st file say a.txt contain 3 column like SPECIALITY|UMP_CODE|SPECIALTY_CODE Addictive Diseases|25ADD|ADD Addictive Diseases/Family Practice|25ADD|ADD/FP Aerospace Medicine|1.041666667|AM... (4 Replies)
Discussion started by: dsh007
4 Replies

8. Shell Programming and Scripting

AWK break string into fields + pattern match

I am trying to break a string into separate fields and print the field that matches a pattern. I am using awk at the moment and have gotten this far: awk '{for(i=1;i<=NF;++i)print "\t" $i}' longstring This breaks the string into fields and prints each field on a separate line. I want to add... (2 Replies)
Discussion started by: Moxy
2 Replies

9. Shell Programming and Scripting

script to sort a string of numerical data in set fields

So, I will be working with someone and basically we are trying to build a form that is submitted most likely via the web and the data is just a string of numbers. like: 19383882872201110929282821818182827349190102837364718191001932873711 Now, each number is part of a numerical value of... (4 Replies)
Discussion started by: tlarkin
4 Replies

10. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

Featured Tech Videos