Unix Shell - How to substr this?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2006 > How to substr this?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to substr this?
amerar@iwc.net

2006-12-02, 7:21 pm

Hi All,

I have a string that looks like this:

TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.12312006: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 help would be appreciated!

Thanks!

Radoulov, Dimitre

2006-12-02, 7:21 pm


<amerar@iwc.net> wrote in message
news:1165095815.447075.44590@j72g2000cwa.googlegroups.com...
> Hi All,
>
> I have a string that looks like this:
>
> TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.12312006: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



Michael Heiming

2006-12-02, 7:21 pm

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.12312006: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
Chris F.A. Johnson

2006-12-02, 7:21 pm

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.12312006: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.12312006: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
Radoulov, Dimitre

2006-12-02, 7:21 pm


"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.googlegroups.com...
>
> 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


amerar@iwc.net

2006-12-02, 7:21 pm


Radoulov, Dimitre wrote:
> "Radoulov, Dimitre" <cichomitiko@gmail.com> wrote in message
> news:4571f580$0$49204$14726298@news.sunsite.dk...
>
> 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????

Janis Papanagnou

2006-12-03, 1:28 am

amerar@iwc.net wrote:
> Radoulov, Dimitre wrote:
>
>
>
> What is NR==2????
>


The second record. Equivalent to second line as long as the record
separator RS has not been changed.

Janis
amerar@iwc.net

2006-12-03, 7:22 am


Janis Papanagnou wrote:
> amerar@iwc.net wrote:
>
> 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......

Radoulov, Dimitre

2006-12-03, 7:22 am


<amerar@iwc.net> wrote in message
news:1165139419.334533.214150@73g2000cwn.googlegroups.com...
>
> Janis Papanagnou wrote:
[...][vbcol=seagreen]
[...]
[vbcol=seagreen]
> 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


Janis Papanagnou

2006-12-03, 1:16 pm

amerar@iwc.net wrote:
> Janis Papanagnou wrote:
>
> 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
kenneth kahn

2006-12-04, 7:20 am

you could also do this:

line='TRSUN5.12312006:N,TRSAT2.12092006:Y,TRSUN6.12312006:N'
field1=$(echo $line|cut -d, -f:1)
field2=$(echo $line|cut -d, -f:2)
...
fieldn=$(echo $line|cut -d, -f:n)
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com