How can I reorganize the text file content for DB import?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I reorganize the text file content for DB import?
# 1  
Old 04-10-2013
How can I reorganize the text file content for DB import?

Dear Madam / Sir,

My Boss need to reorganize Smilie the text file ready for DB import, here show you the requirment and seems not difficult but how to make it by shell script or other programming language effectively.

FILE1 :
Code:
user1,location1,location2,locatoin3,seat1,seat2,seat3
user2,location1,seat1
user3
user4,location1,location2,seat1,seat2

After FILE1 conversion, it become as FILE2

FILE2 :
Code:
user1,location1,seat1
user1,location2,seat2
user1,location3,seat3
user2,location1,seat1
user3
user4,location1,seat1
user4,locatoin2,seat2


Last edited by Franklin52; 04-11-2013 at 03:23 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 04-11-2013
Code:
awk -F, ' {
                if ( NF > 3 )
                {
                        n = (NF - 1) / 2
                        for ( i = 2; i <= (n + 1); i++ )
                        {
                                print $1, $i, $(i + n)
                        }
                }
                else
                {
                        print $0
                }
} ' OFS="," file

# 3  
Old 04-11-2013
Hello Yoda Master,

Nice to try your solution obtain the result quick and accurate.Smilie

However, my actual source file is much complex to fix, here I upload the file for your reference. If possbile, please help me solve this problem for DB import.

before_v2.txt

after_LHS_v2.txt

after_RHS_v2.txt
# 4  
Old 04-11-2013
Try this
Code:
awk -F, '
function prt(start,end,inc,user,FN)
{
  for(i=start;i<end;i++)
    print user,$i,$(i+inc) >> FN
}
{
  for(i=1;i<=NF;i++)if(match($i,"user")){pos=i;break}
  x=((NF-pos)/2)
  prt(pos+1,pos+1+x,x,$pos,"RH")
  y=(pos-1)/2
  prt(1,y+1,y,$pos,"LH")
}' OFS=, infile

LH and RH files will be created

--ahamed
# 5  
Old 04-11-2013
Hello Ahamed,

Smilie Great job Smilie

You completely clear my question, thanks too much !

CK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to sort a content of a text file using a shell script?

I am new to shell scripting. I am interested how to know how to sort a content of a file using shell scripting. I've attached the 'Input file' and the 'expected output' to this thread. Details provided in the expected output file will provide details on how the sort needs to be done. ... (16 Replies)
Discussion started by: nkarthik_mnnit
16 Replies

2. Shell Programming and Scripting

Import ASCII 28 delimited text file

I have an ASCII 28 delimited text file(non-printable delimiter) with 4 columns along with the header.I need to open this text file in Excel or any other tool to view each column content. Please help.. Thanks (6 Replies)
Discussion started by: aneeta13
6 Replies

3. Shell Programming and Scripting

Script to create a text file whose content is the text of another files

Hello everyone, I work under Ubuntu 11.10 (c-shell) I need a script to create a new text file whose content is the text of another text files that are in the directory $DIRMAIL at this moment. I will show you an example: - On the one hand, there is a directory $DIRMAIL where there are... (1 Reply)
Discussion started by: tenteyu
1 Replies

4. UNIX for Dummies Questions & Answers

creating text file with content from script

hi, can somebody tell me how I can create a text file with content from Bash script. The file should be prefilled with information such as current date and time then leaving the user ability to input more data right below those prefilled content. thank you :) (0 Replies)
Discussion started by: s3270226
0 Replies

5. Shell Programming and Scripting

Commands to reorganize a text file

Hi! I am trying to create a script to reorder the contents of a text file. Below is the text file initially, followed by how I would like it reordered: File initially: --- Initial lines with text and/or numbers Initial lines with text and/or numbers Initial lines with text and/or numbers... (11 Replies)
Discussion started by: gwr
11 Replies

6. Shell Programming and Scripting

Sort content of text file based on date?

I now have a 230,000+ lines long text file formatted in segments like this: Is there a way to sort this file to have everything in chronological order, based on the date and time in the text? In this example, I would like the result to be: (19 Replies)
Discussion started by: KidCactus
19 Replies

7. Shell Programming and Scripting

Read a text file and print the content..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... ... (7 Replies)
Discussion started by: samupnl
7 Replies

8. Shell Programming and Scripting

replace block of text with content of another file

Hello, file1: not to be changed not to be changed <start> old stuff old stuff old stuff <end> not to be changed not to be changed file2: new text new text desired output: (3 Replies)
Discussion started by: ripat
3 Replies

9. Shell Programming and Scripting

How to get content of a variable into text file (sed)?

Hello, Im working on this problem for 3 days now and i just cant get it to work.. I tried with alot of different sed methods but didnt find any solution. Its proberly verry simple but i just started bash scripting for a month or so.. i have a file called: file.nfo and file.txt the content... (4 Replies)
Discussion started by: atmosroll
4 Replies

10. UNIX for Dummies Questions & Answers

Content format in a text file

Hi, I need to format the content in a text file as below format. Can some one help me how to approach? Also whether is it possible to convert the output to excel in column wise? Present: ============================================================================= Name: vinodh Status:... (1 Reply)
Discussion started by: vino_hymi
1 Replies
Login or Register to Ask a Question