|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi All,
I have a string that looks like this: TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.1231200 6:N I want to be able to extract the different substrings using the comma as a delimiter. Extracting the first item is easy: fld=`echo $string | awk '{print substr($string,1,index($string,",")-1)}'` I'm not sure how to get the second and third parts. I'm not sure I can use the comma as a delimiter since there is more than one comma................... The length of the string will not always be the same either, in fact, sometimes an element may not be present. TRSUN5.12312006:N,,TRSUN6.12312006:N TRSUN5.12312006:N,TRSAT2.12092006:Y, Any would be appreciated! Thanks! |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
In comp.unix.shell amerar@iwc.net <amerar@iwc.net>:
> Hi All, > I have a string that looks like this: > TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.1231200 6:N [..] > The length of the string will not always be the same either, in fact, > sometimes an element may not be present. > TRSUN5.12312006:N,,TRSUN6.12312006:N > TRSUN5.12312006:N,TRSAT2.12092006:Y, awk 'BEGIN{FS=","}{print $2}' infile Though it is unclear what you want to do if there is ",," an empty field? awk would regard this as field $2 being empty in your above example. -- Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94) mail: echo zvpunry@urvzvat.qr | perl -pe 'y/a-z/n-za-m/' #bofh excuse 164: root rot |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
<amerar@iwc.net> wrote in message news:1165095815.447075.44590@j72g2000cwa.googlegro ups.com... > Hi All, > > I have a string that looks like this: > > TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.1231200 6:N > > I want to be able to extract the different substrings using the comma > as a delimiter. Extracting the first item is easy: fld=`echo $string > | awk '{print substr($string,1,index($string,",")-1)}'` > > I'm not sure how to get the second and third parts. I'm not sure I can > use the comma as a delimiter since there is more than one > comma................... > > The length of the string will not always be the same either, in fact, > sometimes an element may not be present. > > TRSUN5.12312006:N,,TRSUN6.12312006:N > TRSUN5.12312006:N,TRSAT2.12092006:Y, awk 'NF>0' RS="," infile Regards Dimitre |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On 2006-12-02, amerar@iwc.net wrote:
> Hi All, > > I have a string that looks like this: > > TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.1231200 6:N > > I want to be able to extract the different substrings using the comma > as a delimiter. Extracting the first item is easy: fld=`echo $string >| awk '{print substr($string,1,index($string,",")-1)}'` > > I'm not sure how to get the second and third parts. I'm not sure I can > use the comma as a delimiter since there is more than one > comma................... > > The length of the string will not always be the same either, in fact, > sometimes an element may not be present. > > TRSUN5.12312006:N,,TRSUN6.12312006:N > TRSUN5.12312006:N,TRSAT2.12092006:Y, str=TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.123 12006:N IFS=, set -f set -- $str field1=$1 field2=$2 field3=$3 .... -- Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
"Radoulov, Dimitre" <cichomitiko@gmail.com> wrote in message news:4571f580$0$49204$14726298@news.sunsite.dk... > > <amerar@iwc.net> wrote in message > news:1165095815.447075.44590@j72g2000cwa.googlegro ups.com... >> Hi All, >> >> I have a string that looks like this: >> >> TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.1231200 6:N >> >> I want to be able to extract the different substrings using the comma >> as a delimiter. Extracting the first item is easy: fld=`echo $string >> | awk '{print substr($string,1,index($string,",")-1)}'` >> >> I'm not sure how to get the second and third parts. I'm not sure I can >> use the comma as a delimiter since there is more than one >> comma................... >> >> The length of the string will not always be the same either, in fact, >> sometimes an element may not be present. >> >> TRSUN5.12312006:N,,TRSUN6.12312006:N >> TRSUN5.12312006:N,TRSAT2.12092006:Y, > > awk 'NF>0' RS="," infile The above command outputs all the (not null) fields. To extract a particular filed, for example the third on the second line: awk 'NR==2{print $3}' FS="," file Regards Dimitre |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Radoulov, Dimitre wrote: > "Radoulov, Dimitre" <cichomitiko@gmail.com> wrote in message > news:4571f580$0$49204$14726298@news.sunsite.dk... > > > > <amerar@iwc.net> wrote in message > > news:1165095815.447075.44590@j72g2000cwa.googlegro ups.com... > >> Hi All, > >> > >> I have a string that looks like this: > >> > >> TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.1231200 6:N > >> > >> I want to be able to extract the different substrings using the comma > >> as a delimiter. Extracting the first item is easy: fld=`echo $string > >> | awk '{print substr($string,1,index($string,",")-1)}'` > >> > >> I'm not sure how to get the second and third parts. I'm not sure I can > >> use the comma as a delimiter since there is more than one > >> comma................... > >> > >> The length of the string will not always be the same either, in fact, > >> sometimes an element may not be present. > >> > >> TRSUN5.12312006:N,,TRSUN6.12312006:N > >> TRSUN5.12312006:N,TRSAT2.12092006:Y, > > > > awk 'NF>0' RS="," infile > > The above command outputs all the (not null) fields. > To extract a particular filed, for example the third on the second line: > > awk 'NR==2{print $3}' FS="," file > > > Regards > Dimitre What is NR==2???? |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
amerar@iwc.net wrote:
> Radoulov, Dimitre wrote: > >>"Radoulov, Dimitre" <cichomitiko@gmail.com> wrote in message >>news:4571f580$0$49204$14726298@news.sunsite.dk.. . >> >>><amerar@iwc.net> wrote in message >>>news:1165095815.447075.44590@j72g2000cwa.google groups.com... >>> >>>>Hi All, >>>> >>>>I have a string that looks like this: >>>> >>>>TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.123 12006:N >>>> >>>>I want to be able to extract the different substrings using the comma >>>>as a delimiter. Extracting the first item is easy: fld=`echo $string >>>>| awk '{print substr($string,1,index($string,",")-1)}'` >>>> >>>>I'm not sure how to get the second and third parts. I'm not sure I can >>>>use the comma as a delimiter since there is more than one >>>>comma................... >>>> >>>>The length of the string will not always be the same either, in fact, >>>>sometimes an element may not be present. >>>> >>>>TRSUN5.12312006:N,,TRSUN6.12312006:N >>>>TRSUN5.12312006:N,TRSAT2.12092006:Y, >>> >>>awk 'NF>0' RS="," infile >> >>The above command outputs all the (not null) fields. >>To extract a particular filed, for example the third on the second line: >> >>awk 'NR==2{print $3}' FS="," file >> >> >>Regards >>Dimitre > > > What is NR==2???? > The second record. Equivalent to second line as long as the record separator RS has not been changed. Janis |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Janis Papanagnou wrote: > amerar@iwc.net wrote: > > Radoulov, Dimitre wrote: > > > >>"Radoulov, Dimitre" <cichomitiko@gmail.com> wrote in message > >>news:4571f580$0$49204$14726298@news.sunsite.dk.. . > >> > >>><amerar@iwc.net> wrote in message > >>>news:1165095815.447075.44590@j72g2000cwa.google groups.com... > >>> > >>>>Hi All, > >>>> > >>>>I have a string that looks like this: > >>>> > >>>>TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.123 12006:N > >>>> > >>>>I want to be able to extract the different substrings using the comma > >>>>as a delimiter. Extracting the first item is easy: fld=`echo $string > >>>>| awk '{print substr($string,1,index($string,",")-1)}'` > >>>> > >>>>I'm not sure how to get the second and third parts. I'm not sure I can > >>>>use the comma as a delimiter since there is more than one > >>>>comma................... > >>>> > >>>>The length of the string will not always be the same either, in fact, > >>>>sometimes an element may not be present. > >>>> > >>>>TRSUN5.12312006:N,,TRSUN6.12312006:N > >>>>TRSUN5.12312006:N,TRSAT2.12092006:Y, > >>> > >>>awk 'NF>0' RS="," infile > >> > >>The above command outputs all the (not null) fields. > >>To extract a particular filed, for example the third on the second line: > >> > >>awk 'NR==2{print $3}' FS="," file > >> > >> > >>Regards > >>Dimitre > > > > > > What is NR==2???? > > > > The second record. Equivalent to second line as long as the record > separator RS has not been changed. > > Janis I do not understand the concept of "second line"........it is the first line...... |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
<amerar@iwc.net> wrote in message news:1165139419.334533.214150@73g2000cwn.googlegro ups.com... > > Janis Papanagnou wrote: >> amerar@iwc.net wrote: >> > Radoulov, Dimitre wrote: >> > >> >>"Radoulov, Dimitre" <cichomitiko@gmail.com> wrote in message >> >>news:4571f580$0$49204$14726298@news.sunsite.dk.. . >> >> >> >>><amerar@iwc.net> wrote in message [...] >> >>>>I have a string that looks like this: >> >>>> >> >>>>TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.123 12006:N >> >>>> >> >>>>I want to be able to extract the different substrings using the comma >> >>>>as a delimiter. Extracting the first item is easy: fld=`echo >> >>>>$string >> >>>>| awk '{print substr($string,1,index($string,",")-1)}'` >> >>>> >> >>>>I'm not sure how to get the second and third parts. I'm not sure I >> >>>>can >> >>>>use the comma as a delimiter since there is more than one >> >>>>comma................... >> >>>> >> >>>>The length of the string will not always be the same either, in fact, >> >>>>sometimes an element may not be present. >> >>>> >> >>>>TRSUN5.12312006:N,,TRSUN6.12312006:N >> >>>>TRSUN5.12312006:N,TRSAT2.12092006:Y, [...] > I do not understand the concept of "second line"........it is the first > line...... [...] OK, it's because of the line wrapping that I assumed multiple lines/records. If it's one-line input, you can access all your fields as $1, $2 ... $n (assuming the comma delimiter). Regards Dimitre |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
amerar@iwc.net wrote:
> Janis Papanagnou wrote: >>amerar@iwc.net wrote: >>>Radoulov, Dimitre wrote: >>> >>>>To extract a particular filed, for example the third on the second line: >>>> >>>>awk 'NR==2{print $3}' FS="," file >>> >>>What is NR==2???? >> >>The second record. Equivalent to second line as long as the record >>separator RS has not been changed. > > I do not understand the concept of "second line"........it is the first > line...... What "concept"? What 'it' "is the first line"? You asked: "What is NR==2????". I trimmed the posting to the essential parts so that you can re-read my answer in the specific context. Janis |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
you could also do this:
line='TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.1 2312006:N' field1=$(echo $line|cut -d, -f:1) field2=$(echo $line|cut -d, -f:2) ... fieldn=$(echo $line|cut -d, -f:n) |
|
![]() |
| Outils de la discussion | |
|
|