Split records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split records
# 1  
Old 05-08-2014
Split records

Hi

I have a file

Code:
$cat test
a,1;2;3
b,4;5;6;7
c,8;9

I want to split each record to multiple based on semicolon in 2nd field.
i.e

Code:
a,1
a,2
a,3
b,4
b,5
b,6
b,7
c,8
c,9

Can someone assist ?
# 2  
Old 05-08-2014
Try,

Code:
awk -F"[,;]" '{for (i=2;i<=NF;i++) {print $1 "," $i}}' file
a,1
a,2
a,3
b,4
b,5
b,6
b,7
c,8
c,9

This User Gave Thanks to clx For This Post:
# 3  
Old 05-08-2014
Thanks,that helps.
Can you explain what does the below do ?

Code:
awk -F"[,;]"

# 4  
Old 05-09-2014
Quote:
Originally Posted by Shivdatta
Thanks,that helps.
Can you explain what does the below do ?

Code:
awk -F"[,;]"

May I try to explain it?

awk -F"[,;]" -> You are telling to awk what you want awk to consider what is going to be the Field Separator (FS). You have to tell awk what is going to be the Field Separator in order to know to consider what strings are going to be considered as Fields in a Record (Record=Line).

As in /etc/passwd you have ":" you could tell awk just to consider each Field Separator ":" that would be fine.

In this case you have two different characters "," and ";" to consider either as a FS. How to tell this to awk? Fortunately awk supports Regex even when defining internal variables.

In order to tell awk to evaluate a regex you have to use [ ] and inside define your regex.

Here is a good place to see that your regex is working: RegExr: Learn, Build, & Test RegEx eventhough there are many of them. Give it a try with: /[,;]/g and see it finds either "," or ";" in the text.

You can tell which is going to be the FS either using the -F option or telling awk in the BEGIN block which is used when you want awk to do things before starting to process the file.

Code:
awk 'BEGIN{FS="[,;]"} {for (i=2;i<=NF;i++) {print $1 "," $i}}' test

Then if you use a for loop and you use to limit it with the NF (Number of Fields), awk is so smart that understands that you are going through each Field without even telling.

Code:
for (i=2;i<=NF;i++)

The action to perform for each time it is in a field is that you want to print the first field $1 and each field that takes its value in the $i variable.

Code:
for (i=2;i<=NF;i++) {
  print $1 "," $i
}

Notice that eventhough you are telling awk to consider which is going to be your FS, it will not use this information to print your output. That is, if you do this

Code:
awk 'BEGIN{FS="[,;]"} {for (i=2;i<=NF;i++) {print $1,$i}}' test

It will output:

Code:
a 1
a 2
a 3
b 4
b 5
b 6
b 7
c 8
c 9

because awk will be using the default OFS if not telling the opposite (Output FS).

So as you see, awk makes a difference with FS and OFS. It won't print the output using the defined FS eventhough you stated a specific one. FS variable is defined to tell awk how you want awk to evaluate your strings but it has nothing to do with the way you want them to be printed.

Another way of telling awk to print the same thing:

Code:
awk 'BEGIN{FS="[,;]";OFS=","} {for (i=2;i<=NF;i++) {print $1,$i}}' test

These 2 Users Gave Thanks to Kibou For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to split one record to multiple records?

Hi, I have one tab delimited file which is having multiple store_ids in first column seprated by pipe.I want to split the file on the basis of store_id(separating 1st record in to 2 records ). I tried some more options like below with using split,awk etc ,But not able to get proper output. can... (1 Reply)
Discussion started by: jaggy
1 Replies

2. Shell Programming and Scripting

Split file based on records

I have to split a file based on number of lines and the below command works fine: split -l 2 Inputfile -d OutputfileMy input file contains header, detail and trailor info as below: H D D D D TMy split files for the above command contains: First File: H DSecond File: ... (11 Replies)
Discussion started by: Ajay Venkatesan
11 Replies

3. Shell Programming and Scripting

Split Records

I have a flat file with 2 columns Id,loc 1,nj:ny:pa 2,pa 3,ca:tx:fl:nj Second colum data is seperated by semi colon and can i have many locations for one id Output i need is 1,nj 1,ny 1,pa 1,pa 3,ca 3,tx 3,fl (1 Reply)
Discussion started by: traininfa
1 Replies

4. Shell Programming and Scripting

Split the Master and Child Records

Hi, I receive a file that has Master record followed by one/more Child Records as shown below & also as attached in the file. Now , The key for the child record is from pos 4 to position 80 in the parent record, now the requirement is to create two files 1. Parent file --> has all the parent... (1 Reply)
Discussion started by: KNaveen
1 Replies

5. Shell Programming and Scripting

Split records into multiple records

Hi All, I am trying to split a record into multiple records based on a value. Input.txt "A",1,0,10 "B",2,0,10,15,20 "C",3,11,14,16,19,21,23 "D",1,0,5 My desired output is: "A",1,0,10 "B",2,0,10 "B",2,15,20 "C",3,11,14 "C",3,16,19 "C",3,21,23 (4 Replies)
Discussion started by: kmsekhar
4 Replies

6. UNIX for Dummies Questions & Answers

Split single record to multiple records

Hi Friends, source .... col1,col2,col3 a,b,1;2;3 here colom delimeter is comma(,). here we dont know what is the max length of col3 means now we have 1;2;3 next time i will receive 1;2;3;4;5;etc... required output .............. col1,col2,col3 a,b,1 a,b,2 a,b,3 please give me... (5 Replies)
Discussion started by: bab.galary
5 Replies

7. Shell Programming and Scripting

split records into different files

Hi All, I want my file to be split based on value of 'N' (passed as argument). If value of 'N' is '2' then 4 new files will be generated from the below source file and the o/p file shoud look like File_$num , where num will be incremental. Source file: 1 2 3 4 5 O/p Files: ... (6 Replies)
Discussion started by: HemaV
6 Replies

8. UNIX for Advanced & Expert Users

Split records based on '-'

HI, I have a pipe delimiter file , I have to search for second field pattern, if the second field does not contain a '-' , I need to start capturing the record from this line till I find another second field with '-' value. Below is the sample data SOURCE DATA ABC|ABC_702148-PARAM... (3 Replies)
Discussion started by: mora
3 Replies

9. UNIX for Dummies Questions & Answers

How to split multiple records file in n files

Hello, Each record has a lenght of 7 characters I have 2 types of records 010 and 011 There is no character of end of line. For example my file is like that : 010hello 010bonjour011both 011sisters I would like to have 2 files 010.txt (2 records) hello bonjour and ... (1 Reply)
Discussion started by: jeuffeu
1 Replies

10. Shell Programming and Scripting

dynamically split the records

Hi, I was wandering would it be possible to split the record dynamically based on the certain values, for an instance i have a file with record with predefined split value i.e 10 col1 col2 col3 col4 ------------------------ aaaa bbbb 2 44aaaabbbb55cccddd1110 mmn xnmn 3... (6 Replies)
Discussion started by: braindrain
6 Replies
Login or Register to Ask a Question