×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

split row by group of ten in each row splited

split row by group of ten in each row splited

split row by group of ten in each row splited

(OP)
hi all:

I have a thousand of variable rows in texte file , I took an examlpe of one row :

CODE --> awk

ADD=ARGX,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX&100-262-PAX&
100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX100-28-PAX; 

I have used this piece of awk code to parse the text file but I didn't get what I want :

CODE --> awk

awk -F'[,]'  -v RS='[&\n]' 'NF>1{k=$1} {gsub(/;/, "", $NF);print k,",PUT,Uid=",$NF,";"}' 

I want to get this :

CODE --> awk

ADD=ARGX,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX;    
ADD=ARGX,FID,Uid=100-262-PAX&100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX&100-28-PAX; 

thanks for your help and support

RE: split row by group of ten in each row splited

and what is the rule which are you using to split the row

RE: split row by group of ten in each row splited

(OP)
hi again:

I want to split my line after (10th of &).

my awk code create 20 lines , I want only two which conatin 10 of & and so on .

CODE --> awk

awk -F'[,]'  -v RS='[&\n]' 'NF>1{k=$1} {gsub(/.{10}/,"& ");gsub(/;/, "", $NF);print k,",PUT,Uid=",$NF,";"}'  tek 

I have an extra Uid= in first line after split i don't want it .

CODE --> awk

ADD=ARGX ,PUT,Uid= Uid=100-31-PAX ;
ADD=ARGX ,PUT,Uid= 100-41-PAX ;
ADD=ARGX ,PUT,Uid= 100-57-PAX ;
ADD=ARGX ,PUT,Uid= 100-65-PAX ;
ADD=ARGX ,PUT,Uid= 100-77-PAX ;
ADD=ARGX ,PUT,Uid= 100-119-PAX ;
ADD=ARGX ,PUT,Uid= 100-175-PAX ;
ADD=ARGX ,PUT,Uid= 100-194-PAX ;
ADD=ARGX ,PUT,Uid= 100-225-PAX ;
ADD=ARGX ,PUT,Uid= 100-257-PAX ;
ADD=ARGX ,PUT,Uid= 100-262-PAX ;
ADD=ARGX ,PUT,Uid= 100-271-PAX ;
ADD=ARGX ,PUT,Uid= 100-287-PAX ;
ADD=ARGX ,PUT,Uid= 100-289-PAX ;
ADD=ARGX ,PUT,Uid= 100-345-PAX ;
ADD=ARGX ,PUT,Uid= 100-361-PAX ;
ADD=ARGX ,PUT,Uid= 100-415-PAX ;
ADD=ARGX ,PUT,Uid= 100-436-PAX ;
ADD=ARGX ,PUT,Uid= 100-447-PAX ; 

this what I want :

CODE --> awk

ADD=ARGX,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX;    
ADD=ARGX,FID,Uid=100-262-PAX&100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX&100-28-PAX; 

RE: split row by group of ten in each row splited

Quote (seifou45)


I want only two which contain 10 of & and so on
.. but I don't see in your code that you test the 10 & on any place

RE: split row by group of ten in each row splited

I took Record Separator ";" and Field Separator "&" and got this result:

CODE

$ awk -f seifou45_01.awk seifou45_01.txt
ADD=ARGX,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX;
ADD=ARGX,FID,Uid=100-262-PAX&100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX100-28-PAX; 
It seems to be what you want

RE: split row by group of ten in each row splited

(OP)
Hi mikrom:

I have changed awk code as your recomandation but I got only two lines :

CODE --> awk

awk -F'[&]'  -v RS='[;\n]' 'NF>1{k=$1} {gsub(/;/, "", $NF);print k,",PUT,Uid=",$NF,";"}'  tek.txt 

result after execution :

CODE --> awk

ADD=ARGX,FID,Uid=100-31-PAX ,PUT,Uid= 100-447-PAX ;
ADD=ARGX,FID,Uid=100-31-PAX ,PUT,Uid=  ; 

I 'm sorry to ask you this can you post your awk code ?
thanks

RE: split row by group of ten in each row splited

Of course, I was in a big hurry before and forgot to post my source code. Here it is:

seifou45_01.awk

CODE

# run:
# awk -f seifou45_01.awk seifou45_01.txt

BEGIN {
 RS = ";"
 FS = "&" 
}

{
  # chomp 
  sub(/\r+/, "", $0)
  sub(/\n+/, "", $0)
  sub(/\s+/, "", $0)
}

$0 == "" {
  # skip empty lines
  next
}

{ 
  # 1.line
  # print $1 .. $9
  for (i=1; i < 10; i++) {
    printf("%s&", $i)
  }
  # print $10
  printf("%s;\n", $10)

  # 2.line
  printf("%s", "ADD=ARGX,FID,Uid=")
  # print $11 .. $NF-1 
  for (i=11; i < NF; i++) {
    printf("%s&", $i)
  }
  # print $NF
  printf("%s;\n", $NF)
} 

Try it!

RE: split row by group of ten in each row splited

I tried the program on this data file with 2 very long lines:

seifou45_01.txt

CODE

ADD=ARGX,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX&100-262-PAX&
100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX100-28-PAX;
ADD=ARGX,FID,Uid=110-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX&100-272-PAX&
100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX100-28-PAX; 

and the result of this command:

CODE

$ awk -f seifou45_01.awk seifou45_01.txt > seifou45_out.txt 

produced this output file with 4 shorter lines:

seifou45_out.txt

CODE

ADD=ARGX,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX;
ADD=ARGX,FID,Uid=100-262-PAX&100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX100-28-PAX;
ADD=ARGX,FID,Uid=110-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX;
ADD=ARGX,FID,Uid=100-272-PAX&100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX100-28-PAX; 

RE: split row by group of ten in each row splited

Did that help you?

RE: split row by group of ten in each row splited

(OP)
Hi mikrom:

Sorry for my delay I'll test it and Get back To you.

Thanks a lot of.

RE: split row by group of ten in each row splited

(OP)
hi mikrom:

it works fine but I did a big mistakes in my description of my texte file .

sorry I didn't pay attention .

have a look on the newest data .

CODE --> awk

ADD=ARGX,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX&100-262-PAX&100-271-PAX&100-287-PAX&
100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX;
ADD=VRRP,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX&100-262-PAX&100-271-PAX&100-287-PAX&
100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX;
ADD=CLMTP,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX&100-262-PAX&100-271-PAX&100-287-PAX&
100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX; 

the first field change after ADD= and I need it in the second line .

I need bellow output:

CODE --> awk

ADD=ARGX,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX;
ADD=ARGX,FID,Uid=100-262-PAX&100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX;
ADD=VRRP,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX;
ADD=VRRP,FID,Uid=100-262-PAX&100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX;
ADD=CLMTP,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX;
ADD=CLMTP,FID,Uid=100-262-PAX&100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX; 

sorry again and thanks a lot of

RE: split row by group of ten in each row splited

It's no problem to take the first part of $1, e.g.:
ADD=ARGX,FID,Uid=
ADD=VRRP,FID,Uid=
.. etc


RE: split row by group of ten in each row splited

(OP)
Hi

The problem is located in second Line
They must follow the first one for All lines splitted after 10th .

RE: split row by group of ten in each row splited

It is only necessary to adjust the program a bit

RE: split row by group of ten in each row splited

CODE

# run:
# awk -f seifou45_01.awk seifou45_01.txt

BEGIN {
 RS = ";"
 FS = "&" 
}

{
  # chomp 
  sub(/\r+/, "", $0)
  sub(/\n+/, "", $0)
  sub(/\s+/, "", $0)
}

$0 == "" {
  # skip empty lines
  next
}

{
  split($1, my_array, "Uid=")
  first_part = my_array[1]"Uid="
  # 1.line
  # print $1 .. $9
  for (i=1; i < 10; i++) {
    printf("%s&", $i)
  }
  # print $10
  printf("%s;\n", $10)

  # 2.line
  printf("%s", first_part)
  # print $11 .. $NF-1 
  for (i=11; i < NF; i++) {
    printf("%s&", $i)
  }
  # print $NF
  printf("%s;\n", $NF)
} 

Output:

CODE

$ awk -f seifou45_01.awk seifou45_01.txt
ADD=ARGX,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX;
ADD=ARGX,FID,Uid=100-262-PAX&100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX;
ADD=VRRP,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX;
ADD=VRRP,FID,Uid=100-262-PAX&100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX;
ADD=CLMTP,FID,Uid=100-31-PAX&100-41-PAX&100-57-PAX&100-65-PAX&100-77-PAX&100-119-PAX&100-175-PAX&100-194-PAX&100-225-PAX&100-257-PAX;
ADD=CLMTP,FID,Uid=100-262-PAX&100-271-PAX&100-287-PAX&100-289-PAX&100-345-PAX&100-361-PAX&100-415-PAX&100-436-PAX&100-447-PAX; 

RE: split row by group of ten in each row splited

Is this now, what you needed?

RE: split row by group of ten in each row splited

(OP)
hi :

100000000000000000000 thanks .

it works fine

RE: split row by group of ten in each row splited

you're welcome

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close