On 02/14/2012 03:59 AM, Brian K. White wrote:
On 2/12/2012 6:05 PM, lynn wrote:
I'm regex'd out of it at the moment. Given a string like this:
lynn:*:3000002some other stuff100hellolynn:
Is there a way to get the 3000002 into a variable v1 and the 100 into a variable v2? bash? Any recommended starting point? Thanks, L x
You haven't defined the problem clearly enough for a correct answer.
Is there really no other field delimiters (":" in this case) between the * and the end of the line?
Is "some other stuff" always the same length? _always_?
Can "some other stuff" contain numbers?
Basically I have to doubt that the file you are reading really looks like this, or if it does, what is generating it? The only way such a file would be useful is if the fields were all fixed length, because there is no delimiters for most of the line, but then it's odd that there are any delimiters at all in that case.
Please describe more about what is creating this file, and provide some more sample records, and don't feel free to modify the line to hide sensitive data, rather create some junk records in the generating application that aren't sensitive in the first place, then supply those without changing them at all.
There are a few different ways to do what you asked, but no way to say if they will work on any other input except that specific line above. That isn't very useful.
Best guess until you say otherwise is that the fields tat are delimited by :'s are variable length, but the 3rd field
DONE=false until $DONE ;do IFS=: read F1 F2 F3 junk|| DONE=true case "$F1" in ""|\#*) continue ;; esac F3_1=${F3:0:7} F3_2=${F3:7:16} F3_3=${F3:23:3} echo -e "Name:\t\"${F1}\"" echo -e " Number A:\t\"${F3_1}\"" echo -e " Description:\t\"${F3_2}\"" echo -e " Number B:\t\"${F3_3}\"" done < file.txt
Given this line of input: lynn:*:3000002some other stuff100hellolynn:
the "IFS=: read F1 F2 F3 junk" command will read the line and treat ":" as the word separator, so, F1 will be "lynn", F2 will be "*", F3 will everything up to the 3rd ":", and junk will be anything after the 3rd colon.
Then inside the "until loop", it counts bytes to extract chunks of $F3 into $F3_1 $F3_2 etc.. ${F3:0:7} means to output 7 bytes starting at byte 0 of $F3 ${F3:7:16} means output 16 bytes starting at byte 7 (counting from 0) of $F3 ${F3:23:3} means output 3 bytes starting at byte 24 (counting from 0) of $F3
Then I stuck in stuff to ignore empty lines and lines that begin with #, and the use of the $DONE variable ensures that the last line is processed even if the file ends right at the end of the line with no trailing newline.
So given this sample input: ---- lynn:*:3000002some other stuff100hellolynn: foob:*:6100013some stuff......201hellofoob: snac:*:3000562stuff goes here.111hellosnac: higb:*:5000001gibberish.......007hellohigb:
# a comment blah:*:7500022blah blah blah..911helloblah: ----
You get:
Name: "lynn" Number A: "3000002" Description: "some other stuff" Number B: "100" Name: "foob" Number A: "6100013" Description: "some stuff......" Number B: "201" Name: "snac" Number A: "3000562" Description: "stuff goes here." Number B: "111" Name: "higb" Number A: "5000001" Description: "gibberish......." Number B: "007" Name: "blah" Number A: "7500022" Description: "blah blah blah.." Number B: "911"
Which is nice and all, but only works if the lengths of the number and comment fields are always exactly the same length on every record.
Hi Brian Thank you so much for your effort. This thread has given me insight into the hidden power of Linux. You are right. I had not provided enough info. The strings are from the output of wbinfo -i user and wbinfo --group-info=group and so yes, there are : delimiters and they come at the same place for both user and group. This must have been designed with people like me in mind! cut did it, e.g. for groups: strgid=$(wbinfo --group-info=$1) gid=$(echo $strgid | cut -d ":" -f 3) echo $gid I had marked the thread as [solved] previously and I hope that you don't feel you've wasted your time with your (excellent) explanation here. On the contrary. I have learned a load of new stuff from it. More, I have learned how to ask smarter questions. L x -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org