Creating sequence number as per records

Hi,

I have a file source as below.

OL|10031|Day|Black|Midi|Good|Value|P01|P07
OL|10031|Day|Black|Short|Good|Value|P01|P07

I need to create a file form the above data as below logic

  1. take the first line
  2. create a file say inclusion1 as below from the first line
OL,10031,1,Day
OL,10031,2,Black
OL,10031,3,Midi
OL,10031,4,Good
OL,10031,5,Value

after doing some other process again recreate the file inclusion1 with second line with the format as below.

So this process will repeat for all records present in source file.

Request you please help me in doing this.
Thanks in advance.

Hello bhaski2012,

Please use code tags for your commands/codes/Inputs which you are using into your posts as per forum rules. If your Input_file is same as sample you have shown in post then following may help you in same.

awk -F"|" '{for(i=3;i<=7;i++){print $1 "," $2 "," ++q "," $i >> "inclusion1"};q=""}'   Input_file

It will create file named inclusion1 as per your request and it will append all the lines in it from first line to last line of your Input_file. Also it is looking for fields from 3rd to 7th fields from Input_file as per your expected output shown.

Thanks,
R. Singh

1 Like

Hi R. Singh,

Thanks for the reply.
It has worked for me but one change I require here.

I want to create records for first line only
Then do some internal process
then overwrite the same file with second record and so on

So at a time this inclusion1 file will have one set of data like 5 records form first line.

OL,10031,1,Day
OL,10031,2,Black
OL,10031,3,Midi
OL,10031,4,Good
OL,10031,5,Value

So this inclusion 1 file creation I need to put in a loop one by one as per source file lines.

Hello bhaski2012,

Could you please try following and let me know if this helps you.

while read line
do
        VALUE=$(echo $line | awk -F"|" '{for(i=3;i<=7;i++){print $1 "," $2 "," ++q "," $i > "inclusion1"};q=""}')
        #### Do your process here with value of above variable named VALUE.
done < "Input_file"

Above code will take care of overwriting the file named inclusion1

Thanks,
R. Singh

1 Like

Hi RavinderSingh13,

Thanks.

I tried this one but it's making source file as zero byte and creating inclusion file as below.

,,1,
,,2,
,,3,
,,4,
,,5,

Hi,

"I want to create records for first line only
Then do some internal process
then overwrite the same file with second record and so on"

Modified a bit of Ravinder solution . Can you try below one ?

while read line
do
 rm -f inclusion1
        VALUE=$(echo $line | awk -F"|" '{for(i=3;i<=7;i++){print $1 "," $2 "," ++q "," $i >> "inclusion1"};q=""}')
        #### Do your process here with value of above variable named VALUE.
done < "Input_file"

*

Hi,

Code given by Ravinder was fine. But had one issue. It was not printing all the given values.

awk -F"|" '{for(i=3;i<=NF;i++){print $1 "," $2 "," ++q "," $i >> "inclusion1"};q=""}'   Input_file

The output for the above command:

OL,10031,1,Day
OL,10031,2,Black
OL,10031,3,Midi
OL,10031,4,Good
OL,10031,5,Value
OL,10031,6,P01
OL,10031,7,P07
OL,10031,1,Day
OL,10031,2,Black
OL,10031,3,Short
OL,10031,4,Good
OL,10031,5,Value
OL,10031,6,P01
OL,10031,7,P07

Hello rahulsk,

I am not sure I got your point here, following is the output mentioned by user(for a single line):

OL,10031,1,Day
OL,10031,2,Black
OL,10031,3,Midi
OL,10031,4,Good
OL,10031,5,Value
 

After running my code as follows is the output for all lines:

while read line; do echo $line | awk -F"|" '{for(i=3;i<=7;i++){print $1 "," $2 "," ++q "," $i};q=""}'; done < "Input_file"
 

Output of above code is as follows:

OL,10031,1,Day
OL,10031,2,Black
OL,10031,3,Midi
OL,10031,4,Good
OL,10031,5,Value
OL,10031,1,Day
OL,10031,2,Black
OL,10031,3,Short
OL,10031,4,Good
OL,10031,5,Value

I think code is providing the output similar to what OP has requested. Your opinions are welcomed in case I missed anything here, thank you.

Thanks,
R. Singh

Hi All,

Many thanks for this quick help.
This is working and previous one was my mistake.