![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
Looks kinda like one of our gurus just did an homework problem for someone because they are such kind, wonderful forumists
Simple... A, B, Cs........ are a clue to H/W problems to me...... |
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Still smells like homework.....
What is the practical business problem you are solving? |
|
#7
|
|||
|
|||
|
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 |