Computing.Net > Forums > Unix > Loop using SED

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Loop using SED

Reply to Message Icon

Name: hp_admin20
Date: July 10, 2006 at 08:43:42 Pacific
OS: HP-UX
CPU/Ram: 1GB
Product: HP/B2600
Comment:

I want to scan a file using sed and where a condition meets a certain requirement grab the first four characters and insert a new ling plus those four characters I grabbed.

Example:
file1
:418:937,969,985,986
:441,506
:709:282,285,478,479,497,748

file2
:418:937
:418:969
:418:985
:418:986
:441,506
:709:282
:709:285
:709:478
:709:479
:709:479
:709:748

Let me know if I've made this issue more confusing that it really is. Any help with this problem will be greatly appreciated!



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: July 10, 2006 at 09:49:38 Pacific
Reply:

If it can be done with sed, it'll take a better sed programmer than I am. Awk seems to work:

awk ' BEGIN { FS="," }
{
if(NF > 2)
{
print $1
split($1,arr,":")
for(i=2; i<=NF; i++)
printf(":%s:%s\n", arr[2], $i)
}
else
print $0

} ' file1

Let me know if you need any further explanation.


0

Response Number 2
Name: hp_admin20
Date: July 10, 2006 at 10:22:41 Pacific
Reply:

It works great except one part. When the line doesn't begin with :...: I don't want it to touch that line. Does that make sense? I feel like I'm not explaining myself very well and if that's the case I apologize. :)

File1
:246,264,268,284,340,345
:418:229,242,379,461,773,787,795,947
:473,649,664

File2
:246,264,268,284,340,345
:418:229
:418:242
:418:379
:418:461
:418:773
:418:787
:418:795
:418:947
:473,649,664


0

Response Number 3
Name: nails
Date: July 10, 2006 at 10:52:44 Pacific
Reply:

I made an assumption that the lines you want to parse have 2 colons, and those you don't have only 1 colon:

nawk ' BEGIN { FS="," }
{
t=$0
cnt=gsub(":", ".", t)
if(cnt == 2)
{
print $1
split($1,arr,":")
for(i=2; i<=NF; i++)
printf(":%s:%s\n", arr[2], $i)
}
else
print $0

} ' file1
# end script

I get a count of the number of colons by using the internal function gsub. gsub returns the number of substitutions made.

Note my use of nawk since I'm using Solaris. You might need to change that back to awk.



0

Response Number 4
Name: hp_admin20
Date: July 10, 2006 at 11:12:41 Pacific
Reply:

Thank you!!!

That worked like a champ!

Well if you don't mind I'd like to pick your brain on one more question.

With that same file there is a header for each group of numbers. I'd like to take the first character from that header and place it before each number.

fiale1
C:Atlantic No Daylight
:246
:345
:418:229
:418:242
:418:379
:418:461

file2
C:246
C:345
C:418:229
C:418:242
C:418:379
C:418:461

Thank you again for the quick responses!


0

Response Number 5
Name: hp_admin20
Date: July 10, 2006 at 11:22:20 Pacific
Reply:

Here is a better example of the data I'm working with and what I'm trying to accomplish.

C:Atlantic No Daylight
:246
:345
:418:229
:418:242
:418:379
:418:461
D:Eastern Daylight
:215
:216
:219:276
:219:292

file2
C:Atlantic No Daylight
C:246
C:345
C:418:229
C:418:242
C:418:379
C:418:461
D:Eastern Daylight
D:215
D:216
D:219:276
D:219:292


0

Related Posts

See More



Response Number 6
Name: nails
Date: July 10, 2006 at 11:36:44 Pacific
Reply:

You're welcome. Some people do crossword puzzles; I do this:

# assumes a one-line header starting with C:
nawk ' BEGIN { FS="," }
{
if(NR == 1)
{
split($1,brr,":")
continue
}

t=$0
cnt=gsub(":", ".", t)
if(cnt == 2)
{
printf("%s%s\n", brr[1], $1)
split($1,arr,":")
for(i=2; i<=NF; i++)
printf("\n%s%s:%s", brr[1], arr[2], $i)
}
else
printf("%s%s\n", brr[1], $0)

} ' file1


0

Response Number 7
Name: hp_admin20
Date: July 10, 2006 at 11:57:21 Pacific
Reply:

Does this go after the first set of coding you provided or did you mix the two together? Thanks again.


0

Response Number 8
Name: hp_admin20
Date: July 10, 2006 at 12:02:16 Pacific
Reply:

When I tried it the way you have listed above it did the following to my out put...

A:902
AC:Atlantic No Daylight
A:246
A:264
A:268
A:418:229
A:418:242
AD:Eastern Daylight
A:201
A:202


0

Response Number 9
Name: nails
Date: July 10, 2006 at 12:17:55 Pacific
Reply:

I posted my solution before you posted your last data file. My solution assumes only one header.

How are you suppose to know when the header changes? Can you always assume that the string 'Daylight' is always in the header?



0

Response Number 10
Name: hp_admin20
Date: July 10, 2006 at 12:25:07 Pacific
Reply:

The begining letter changes for each header.

A:Newfoundland Daylight
:709
B:Atlantic Daylight
:418:937
:418:969
:418:985
:418:986
D:Eastern Daylight
:201
:202
:203
:207
:809:999
:812
:819:925
G:Central Daylight
:204
:205
:210

and yes 'Daylight' is in every header.


0

Response Number 11
Name: nails
Date: July 10, 2006 at 13:02:24 Pacific
Reply:


nawk ' BEGIN { FS="," }
{
cnt=index($0, "Daylight")
if(cnt > 1)
{
split($1,brr,":")
continue
}

t=$0
cnt=gsub(":", ".", t)
if(cnt == 2)
{
printf("%s%s\n", brr[1], $1)
split($1,arr,":")
for(i=2; i<=NF; i++)
printf("\n%s%s:%s", brr[1], arr[2], $i)
}
else
printf("%s%s\n", brr[1], $0)

} ' file1


0

Response Number 12
Name: hp_admin20
Date: July 10, 2006 at 13:11:30 Pacific
Reply:

Thank you for all your help today. It works perfectly!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Loop using SED

using sed www.computing.net/answers/unix/using-sed/4164.html

Extract text using sed www.computing.net/answers/unix/extract-text-using-sed/5169.html

How to use 'sed' on windows XP. www.computing.net/answers/unix/how-to-use-sed-on-windows-xp/6605.html