Creating sequence number as per records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating sequence number as per records
# 1  
Old 11-14-2016
Creating sequence number as per records

Hi,

I have a file source as below.
Code:
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
Code:
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.

Last edited by vbe; 11-14-2016 at 08:10 AM.. Reason: code tags
# 2  
Old 11-14-2016
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.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 11-14-2016
Creating sequence number as per records

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.
Code:
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.


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes it far easier to read and preserves multiple spaces for indenting or fixed width data.

Last edited by rbatte1; 11-14-2016 at 10:21 AM.. Reason: Added CODE tags
# 4  
Old 11-14-2016
Hello bhaski2012,

Could you please try following and let me know if this helps you.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 11-14-2016
Hi RavinderSingh13,

Thanks.

I tried this one but it's making source file as zero byte and creating inclusion file as below.
Code:
,,1,
,,2,
,,3,
,,4,
,,5,


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes it far easier to read and preserves multiple spaces for indenting or fixed-width data.

Last edited by bhaski2012; 11-14-2016 at 12:24 PM.. Reason: Added CODE tags
# 6  
Old 11-14-2016
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 ?

Code:
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"

*
# 7  
Old 11-15-2016
Hi,

Code given by Ravinder was fine. But had one issue. It was not printing all the given values.
Code:
awk -F"|" '{for(i=3;i<=NF;i++){print $1 "," $2 "," ++q "," $i >> "inclusion1"};q=""}'   Input_file

The output for the above command:
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating a sequence of numbers in a line for 1000 files

Hi, I try to explain my problem , I have a file like this: aasdsaffsc23 scdsfsddvf46567 mionome0001.pdb asdsdvcxvds dsfdvcvc2324w What I need to do is to create 1000 files in which myname line listing a sequence of numbers from 0001 to 1000. So I want to have : nomefile0001.txt that must... (10 Replies)
Discussion started by: danyz84
10 Replies

2. Shell Programming and Scripting

Need append sequence number

Hi, Need to add sequnce number to one of the csv file and please find below actual requirement. Input file ABC,500 XXQ,700 ADF,400, ART,200 Out put file should be 1,ABC,500 2,XXQ,700 3,ADF,400, 4,ART,200 (3 Replies)
Discussion started by: siva83
3 Replies

3. Shell Programming and Scripting

Hex number sequence help

Need some help doing this ... with awk maybe Input 0DF6 0DF7 0DF8 0DF9 0DFA 0DFB 0DFC 0DFD 0DFF 0E00 0E01 0E02 0E03 0E04 0E05 0E06 (11 Replies)
Discussion started by: greycells
11 Replies

4. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

5. Shell Programming and Scripting

How to take the missing sequence Number?

Am using unix aix KSH... I have the files called MMRR0106.DAT MMRR0206.DAT MMRR0406.DAT MMRR0506.DAT MMRR0806.DAT .... ... MMRR3006.DAT MMRR0207.DAT These files are in one dircetory /venky ? I want the output like this ? Missing files are : MMRR0306.DAT MMRR0606.DAT... (7 Replies)
Discussion started by: Venkatesh1
7 Replies

6. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

7. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

8. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

9. UNIX for Dummies Questions & Answers

creating sequence numbers in unix

Hi, Is there a way to create sequence numbers in unix i have a set of batches(which contain records) and i want to assign a number to every batch. how can i do that? (1 Reply)
Discussion started by: dnat
1 Replies

10. UNIX for Dummies Questions & Answers

sequence number checking

Hi there, I'm wanting to produce a shell script that will check through some file names and identify a skip in sequence (four digit seq num in file name). I have played on the idea of havng a file that has a sorted list of file names which I can read line at a time and cut out the sequence... (1 Reply)
Discussion started by: nhatch
1 Replies
Login or Register to Ask a Question