PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > How to substr this?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

How to substr this?

Réponse
 
LinkBack Outils de la discussion
Vieux 02/12/2006, 21h43   #1
amerar@iwc.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to substr this?

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!

  Réponse avec citation
Vieux 02/12/2006, 21h51   #2
Michael Heiming
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to substr this?

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
  Réponse avec citation
Vieux 02/12/2006, 21h53   #3
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to substr this?


<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



  Réponse avec citation
Vieux 02/12/2006, 22h29   #4
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to substr this?

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
  Réponse avec citation
Vieux 02/12/2006, 23h18   #5
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to substr this?


"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


  Réponse avec citation
Vieux 02/12/2006, 23h54   #6
amerar@iwc.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to substr this?


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????

  Réponse avec citation
Vieux 03/12/2006, 01h36   #7
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to substr this?

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
  Réponse avec citation
Vieux 03/12/2006, 09h50   #8
amerar@iwc.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to substr this?


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......

  Réponse avec citation
Vieux 03/12/2006, 11h02   #9
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to substr this?


<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


  Réponse avec citation
Vieux 03/12/2006, 15h08   #10
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to substr this?

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
  Réponse avec citation
Vieux 04/12/2006, 10h58   #11
kenneth kahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to substr this?

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)
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h56.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20638 seconds with 19 queries