Splitting a filed into multiple fields using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting a filed into multiple fields using awk
# 1  
Old 01-01-2015
Splitting a filed into multiple fields using awk

Hi,

I have a tab delimited file as below:

Code:
AWA Divi  DD01 None 1 2 Room AC 01-MAY-15 31-OCT-15 OT 01-MAY-15 31-OCT-15 CF 01-MAY-15 31-OCT-15
AW0 Beach DD02 None 1 2 Double AC 01-MAY-15 31-OCT-15 AD 01-MAY-15 31-OCT-15

The number of columns(fields) after 7th field is not fixed and varies in each line.I need to split the data for every three fields from 8th field(column) onwards and append it to the below lines while copying the first seven fields in them. i.e the output should look like:

output:

Code:
AWA Divi  DD01 None 1 2 Room AC 01-MAY-15 31-OCT-15
AWA Divi  DD01 None 1 2 Room OT 01-MAY-15 31-OCT-15
AWA Divi  DD01 None 1 2 Room CF 01-MAY-15 31-OCT-15
AW0 Beach DD02 None 1 2 Double AC 01-MAY-15 31-OCT-15
AW0 Beach DD02 None 1 2 Double AD 01-MAY-15 31-OCT-15

Please let me know how to achive it in awk as I am new to it.
# 2  
Old 01-01-2015
Not the slightest attempt from your side? pity...
Try straightforward
Code:
awk '{for (i=8; i<=NF; i+=3) print $1, $2, $3, $4, $5, $6, $7, $i, $(i+1), $(i+2)}' file
AWA Divi DD01 None 1 2 Room AC 01-MAY-15 31-OCT-15
AWA Divi DD01 None 1 2 Room OT 01-MAY-15 31-OCT-15
AWA Divi DD01 None 1 2 Room CF 01-MAY-15 31-OCT-15
AW0 Beach DD02 None 1 2 Double AC 01-MAY-15 31-OCT-15
AW0 Beach DD02 None 1 2 Double AD 01-MAY-15 31-OCT-15

This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-01-2015
Is the output TAB delimited only until field 7 and the rest with spaces or are all fields separated by TABs ?
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-01-2015
Quote:
Originally Posted by Bobby_2000
Hi,

I have a tab delimited file as below:

Code:
AWA Divi  DD01 None 1 2 Room AC 01-MAY-15 31-OCT-15 OT 01-MAY-15 31-OCT-15 CF 01-MAY-15 31-OCT-15
AW0 Beach DD02 None 1 2 Double AC 01-MAY-15 31-OCT-15 AD 01-MAY-15 31-OCT-15

The number of columns(fields) after 7th field is not fixed and varies in each line.I need to split the data for every three fields from 8th field(column) onwards and append it to the below lines while copying the first seven fields in them. i.e the output should look like:

output:

Code:
AWA Divi  DD01 None 1 2 Room AC 01-MAY-15 31-OCT-15
AWA Divi  DD01 None 1 2 Room OT 01-MAY-15 31-OCT-15
AWA Divi  DD01 None 1 2 Room CF 01-MAY-15 31-OCT-15
AW0 Beach DD02 None 1 2 Double AC 01-MAY-15 31-OCT-15
AW0 Beach DD02 None 1 2 Double AD 01-MAY-15 31-OCT-15

Please let me know how to achive it in awk as I am new to it.
Hello Bobby_2000,
Following may also help you in same.
Code:
awk '{X[++q]=$1 FS $2 FS $3 FS $4 FS $5 FS $6 FS $7;k=1} {for(i=8;i<=NF;i++){j=(i==8)?++j:j;if(k%3==0){S=S FS $i;print X[j] FS S;S=""} else{S=S?S FS $i:$i};k++}}'   Input_file

Output will be as follows.
Code:
AWA Divi DD01 None 1 2 Room AC 01-MAY-15 31-OCT-15
AWA Divi DD01 None 1 2 Room OT 01-MAY-15 31-OCT-15
AWA Divi DD01 None 1 2 Room CF 01-MAY-15 31-OCT-15
AW0 Beach DD02 None 1 2 Double AC 01-MAY-15 31-OCT-15
AW0 Beach DD02 None 1 2 Double AD 01-MAY-15 31-OCT-15

EDIT: Adding a non one liner form for code.
Code:
awk '{
        X[++q]=$1 FS $2 FS $3 FS $4 FS $5 FS $6 FS $7;
        k=1
      }
     {for(i=8;i<=NF;i++)
     {
        j=(i==8)?++j:j;
        if(k%3==0)
        {
                S=S FS $i;
                print X[j] FS S;
                S=""
        }
        else
        {
                S=S?S FS $i:$i
        };
        k++
     }
     }
    '  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-01-2015 at 09:25 AM.. Reason: Added a non one liner form of code
This User Gave Thanks to RavinderSingh13 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

Splitting multiple fields of /usr/bin/id

Hi, Iv got the following input $id |grep uid uid=6090(dsiddiq) gid=1(staff) groups=4001(cdgrp) and Im using the below command to split the field to grab the numberical userid as well the alphabetical userid $id|awk -F'=' '{print $2}'|awk -F')' '{print $1}'|awk -F'(' '{print $1" "$2}'... (4 Replies)
Discussion started by: dsid
4 Replies

2. Shell Programming and Scripting

Print multiple fields with awk

so its common knowledge one can print multiple fields with simple commands like this: echo 12 44 45 552 24 | awk '{print $1,$4,$3}' but suppose i want to avoid specifying the "$" symbol. is that possible? can something like this be done: echo 12 44 45 552 24 | awk '{print $(1,4,3)}' ... (9 Replies)
Discussion started by: SkySmart
9 Replies

3. Shell Programming and Scripting

awk multiple filed separators

There is an usual ifconfig output vlan30 Link encap:Ethernet HWaddr inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: 2407:4c00:0:1:aaff::1/64 Scope:Global inet6 addr: fe80::224:e8ff:fe6b:cc4f/64 Scope:Link UP BROADCAST... (1 Reply)
Discussion started by: urello
1 Replies

4. Shell Programming and Scripting

Replace 0 with 1 in multiple fields with awk

Hello, I have the following input file: 1 3 3 2 3 3 4 0 4 0 5 4 5 2 2 0 5 3 4 0 6 0 3 2 I am trying to remove all zeroes in fields 2 and 4 and replace them with "1's" I tried the following, but it's not working awk -F"\t" '{ if (($2==0) || ($4==0) $2=1; $4=1; print $0 ) }' input ... (8 Replies)
Discussion started by: Rabu
8 Replies

5. Shell Programming and Scripting

awk multiple fields separators

Can you please help me with this .... Input File share "FTPTransfer" "/v31_fs01/root/FTP-Transfer" umask=022 maxusr=4294967295 netbios=NJ09FIL530 share "Test" "/v31_fs01/root/Test" umask=022 maxusr=4294967295 netbios=NJ09FIL530 share "ENR California" "/v31_fs01/root/ENR California"... (14 Replies)
Discussion started by: greycells
14 Replies

6. Shell Programming and Scripting

awk gsub multiple fields

Hi, I am trying to execute this line awk -F ";" -v OFS=";" '{gsub(/\./,",",$6); print}' FILE but for multiple fields $6 $7 $8 Do you have a suggstion? Tried: awk -F ";" -v OFS="";"" "function GSUB( F ) {gsub(/\./,\",\",$F); print} { GSUB( 6 ); GSUB( 7 ); GSUB( 8 ) } 1"... (2 Replies)
Discussion started by: nakaedu
2 Replies

7. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

8. Shell Programming and Scripting

Find and Replace in multiple fields using awk

Hi, Say I have a record "1|22| | |". In which the third and fourth fields are <space> alone. I have to replace the <Space> with <null>. Input: "1|22| | |" --> "1|22|<space> |<space> |" Expected output: "1|22|||" --> "1|22|<null> |<null>|" I tried: echo "1|22| | |" | awk -F... (4 Replies)
Discussion started by: machomaddy
4 Replies

9. Shell Programming and Scripting

awk - splitting 1 large file into multiple based on same key records

Hello gurus, I am new to "awk" and trying to break a large file having 4 million records into several output files each having half million but at the same time I want to keep the similar key records in the same output file, not to exist accross the files. e.g. my data is like: Row_Num,... (6 Replies)
Discussion started by: kam66
6 Replies

10. Shell Programming and Scripting

AWK multiple fields separators

I need to print the second field of a file, taking spaces, tab and = as field separators. ; for 16-bit app support MAPI=1 CMC=1 CMCDLLNAME32=mapi32.dll CMCDLLNAME=mapi.dll MAPIX=1 MAPIXVER=1.0.0.1 OLEMessaging=1 asf=MPEGVideo asx=MPEGVideo ivf=MPEGVideo m3u=MPEGVideo (2 Replies)
Discussion started by: PamPam
2 Replies
Login or Register to Ask a Question