Break on record with multiple with userid


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Break on record with multiple with userid
# 1  
Old 08-31-2015
Question Break on record with multiple with userid

I'm using the unix terminal in Mac osx yosemite.

I have a file
Code:
1;2015p;2014r;2013r;2013p
2;2013p;2013r;2012g
3;2013g
4;2015g;2014g;2013r;2012s;2011s

The first column is the userid, the second column is each event.

I'd like a separate record for each event.
Code:
1   2015p
1   2014r
1   2013r
1   2013p
2   2013p
2   2013r
2   2012g
3   2013g
4   2015g
4   2014g

Any help would be marvelous!

Last edited by Scrutinizer; 08-31-2015 at 03:19 PM.. Reason: code tags
# 2  
Old 08-31-2015
Hello Nataliemf,

Welcome to forums, please use code tags in your posts while using commands/codes/inputs as per forum rule.
Following may help you in same.
Code:
awk -F";" '{for(i=2;i<=NF;i++){print NR OFS $i}}' Input_file

Output will be as follows.
Code:
1 2015p
1 2014r
1 2013r
1 2013p
2 2013p
2 2013r
2 2012g
3 2013g
4 2015g
4 2014g
4 2013r
4 2012s
4 2011s

EDIT: Considering if $1 is NOT a line number which I assumed in previous 1st solution, then you can use following for same.
Code:
 awk -F";" '{for(i=2;i<=NF;i++){print $1 OFS $i}}' Input_file

EDIT2: One more solution for same considering that you need line number before each word as like same 1st solution of mine.
Code:
 awk '{sub(/\;/,OFS,$0);gsub(/\;/,ORS NR OFS,$0);print}' Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-31-2015 at 03:07 PM.. Reason: Added one more solution, added one more solution for same
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 08-31-2015
Thank you for your quick response. It looks like I need to give more info. The first field is alphanumeric 15 digits, then the following fields are years with a letter tag.
Code:
AB0004897;2014g;2014r;2013g;2012r;
CX00003241;2015g;2013r
LA000023984;2013r

I tried your second solution and it repeated the first column of the first record in each of the records.
Code:
AB0004897   2014g
AB0004897   2014r
AB0004897   2013g
AB0004897   2012r
CX00003241
AB0004897   2015g
AB0004897   2013r
LA000023984
AB0004897   2013r

I will not need any line numbering in my solution. Thank you!

Last edited by Nataliemf; 08-31-2015 at 06:35 PM..
# 4  
Old 08-31-2015
Maybe you're looking for something more like:
Code:
awk -F ';' '{for(i = 2; i <= NF; i++) printf("%-15s %s\n", $1, $i)}' file

which, if file contains your two sample input files concatenated:
Code:
1;2015p;2014r;2013r;2013p
2;2013p;2013r;2012g
3;2013g
4;2015g;2014g;2013r;2012s;2011s
AB0004897;2014g;2014r;2013g;2012r;
CX00003241;2015g;2013r

produces the output:
Code:
1               2015p
1               2014r
1               2013r
1               2013p
2               2013p
2               2013r
2               2012g
3               2013g
4               2015g
4               2014g
4               2013r
4               2012s
4               2011s
AB0004897       2014g
AB0004897       2014r
AB0004897       2013g
AB0004897       2012r
AB0004897       
CX00003241      2015g
CX00003241      2013r

(Note that the red output line is there because you have a trailing semicolon at the end of the 1st line in your 2nd sample input file.)

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 08-31-2015
Thanks!

Turns out your answers work as designed. My file is encoded wrong. I have to delete and re insert the returns at the end of the line in text edit, for you answers to work. Since the master file is only 10 million records, I'll have to try exporting it another way!

Thank you again!
# 6  
Old 08-31-2015
If you mean that you have a DOS format file with carriage-return linefeed character pairs as line terminators and you want the same format in your output file, try:
Code:
awk -F ';' '{gsub("\r", "")}{for(i = 2; i <= NF; i++) printf("%-15s %s\r\n", $1, $i)}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking File record equal to multiple of 70 or nearest number to multiple of 70

Hello, I have a file with below content - Example 3 6 69 139 210 345 395 418 490 492 I would like the result as - Multiple of 70 or nearest number in the file less than the multiple of 70 69 139 (5 Replies)
Discussion started by: Mannu2525
5 Replies

2. Shell Programming and Scripting

Break one long string into multiple fixed length lines

This is actually a KSH under Unix System Services (Z/OS), but hoping I can get a standard AIX/KSH solution to work... I have a very large, single line file in Windows, that we download via FTP, with the "SITE WRAP" option, into a Z/OS file with an LRECL of 200. This essentially breaks the single... (4 Replies)
Discussion started by: bubbawuzhere
4 Replies

3. Shell Programming and Scripting

Break line content into multiple lines using delimiter

I need to break the line after every 3rd semi colon(;) using Unix shell scripting Input.txt ABC;DEF;JHY;LKU;QWE;BVF;RGHY; Output.txt ABC;DEF;JHY; LKU;QWE;BVF; RGHY; (1 Reply)
Discussion started by: meet_calramz
1 Replies

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

5. Shell Programming and Scripting

Multiple Records from 1 Record

I need to make one record to multiple records based on occurence column in the record and change the date.For example below first record has 5 ,so need to create 5 records from one and change the date to 5 months.Occurence can be any number. I am unable to come with a script.Can some one help ... (5 Replies)
Discussion started by: traininfa
5 Replies

6. Shell Programming and Scripting

break from a single list into multiple columns

Hi Guys, I am prety new to the hell scripting world. I am running some grep/cut commands and extracting from a csv file into a list. But the final product I need is that the whole list that I now have has to be broken and separated into columns. Say what I now have extracted is a list of... (6 Replies)
Discussion started by: h_rishi
6 Replies

7. Shell Programming and Scripting

Multiple line break

Hi, We had an issue with one file. Each line in the file is a record in which there will be 6 fields each separated by ; Problem is some lines are broken into pieces. like a;b; c; d; e;f instead of a;b;c;d;e;f I have filtered out all the broken lines from the original file and wrote to... (6 Replies)
Discussion started by: anjuvr
6 Replies

8. Shell Programming and Scripting

Multiple records based on ';' in the record

Hi All, I have a *.csv files in a die /pro/lif/dow, (pipe delimiter file), these files are having 8 columns and 6 column(CDR_LOGIC) records are populated as below, I need to incorporate the below logic in all the *.csv files. 11||:ColumnA||:ColumnB 123||:ColumnA IIF(:ColumnA = :ColumnC then... (6 Replies)
Discussion started by: shruthidwh
6 Replies

9. Shell Programming and Scripting

How to break record

I have this script lines #!/usr/bin/bash my_path=`dirname $0` I_table=$1 echo $I_table in I_table entry going is ccc_con,cc_gui I want to break content of I_table in S_Table and T_table on basis of comma as separtor (2 Replies)
Discussion started by: scorp_rahul23
2 Replies

10. Shell Programming and Scripting

Help using IFS to break up a record (ksh)

I have a program that produces output similar to this: 16010001pe3m_313101.ver 16010001pe3m_313101.ver 16010001pe4m_0 16010001pe4m_0 16010001pe4m_1 16010001pe4m_1 16010001pe4m_313101.ver 16010001pe4m_313101.ver group_defs.txt Group Definition File I have a ksh... (2 Replies)
Discussion started by: lyonsd
2 Replies
Login or Register to Ask a Question