Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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,748file2
:418:937
:418:969
:418:985
:418:986
:441,506
:709:282
:709:285
:709:478
:709:479
:709:479
:709:748Let me know if I've made this issue more confusing that it really is. Any help with this problem will be greatly appreciated!

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.

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,664File2
: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

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

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:461file2
C:246
C:345
C:418:229
C:418:242
C:418:379
C:418:461Thank you again for the quick responses!

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:292file2
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

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

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

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

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?

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
:210and yes 'Daylight' is in every header.

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

![]() |
![]() |
![]() |

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