complicated row to rows conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting complicated row to rows conversion
# 1  
Old 07-25-2011
complicated row to rows conversion

I have a file containing rows with the following format.

Field1|Field2|Field3|data1:data data2:data data3:"dataA:data dataB:data" data4:data:data (and so on)

I need to format the above row into multiple rows that look like this:

Field1|Field2|Field3|data1|data

Field1|Field2|Field3|data2|data

Field1|Field2|Field3|data3|dataA:data dataB:data

Field1|Field2|Field3|data4|data:data

Here are the steps in my mind it would take to accomplish this.
1. Store everything before the last | on each row into a string variable, lets call this $header
2. use sed or awk to replace every space that does not appear between quotations " " with \n$header
3. Use sed to turn the first : on every row into a | in and output into the results file.

I performed a similar task with the help of some people here using awk to deliniate a row into rows, but the originating row was purely space delimited. With the added criteria of not delimiting if the space appears between " " I am a bit stumped.

I believe a workaround to the problem with the spaces between " " is to deliniate by spaces, and then if the string has a " then it needs to be concatinated with all subsequent strings until another " is found. I plan on trying this solution tonight, but I believe that there is likely much more elegant ways to do this.

Additionally, I still need a hand in creating the header string. I could possibly check to see if the deliniated string for | and if they exist, write the values before the last | into $header.

Any help here would be appreciated. Thanks!
# 2  
Old 07-25-2011
See if this works for you:
Code:
#!/bin/ksh
typeset -i mCnt=0
typeset -i mTot
IFS='|'
while read mFld1 mFld2 mFld3 mFld4; do
  mFldOut=${mFld1}'|'${mFld2}'|'${mFld3}'|'
  IFS=' '
  for mEach in ${mFld4}; do
    mTot=$(echo ${mEach} | grep -c '"')
    if [[ ${mTot} -eq 0 ]]; then
      mOut=$(echo ${mEach} | sed 's/:/|/')
      echo "${mFldOut}${mOut}"
    else
      if [[ ${mCnt} -eq 1 ]]; then
        mOut=${mOut}' '${mEach}
        mOut=$(echo ${mOut} | sed -e 's/"//'g -e 's/:/|/')
        echo "${mFldOut}${mOut}"
        mCnt=0
      else
        mOut=${mEach}
        mCnt=${mCnt}+1
      fi
    fi
  done
done < Input_File

# 3  
Old 07-26-2011
POSIX shell:
Code:
while read line; do
  start=${line%|*}
  eval set -- ${line##*|}
  for data; do
    echo "$start|$data" | sed 's/:/|/'
  done
done  <INPUTFILE

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. UNIX for Advanced & Expert Users

Conversion of rows to columns using awk based om column value

HI, My Input file data is dn:adcfgeneral id:13343 Name:xxxxxx Password:iutyerwuitywue wpuwt tuiytruityrutyrwtyrwp dn:cdferwjyyyy id:3875 Name:yyyy Password :hgfdsjkfhdsfkdlshf dshfkldshfdklsfh interset:uiuiufj My output should be ... (6 Replies)
Discussion started by: dineshaila
6 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Uneven column to row conversion

Hi Unix Forum, I have a relatively easy question i suppose for which, however, until now i could not find a solution. I am working with a program that will give me an output file similar to the following: A 1 2 3 4 B 1 2 3 4 C 1 (9 Replies)
Discussion started by: Leander
9 Replies

4. Solaris

Rows to column conversion in Solaris

i am using the command pkginfo -l | /usr/xpg4/bin/grep -e 'VENDOR' -e 'NAME' -e 'VERSION' >> /tmp/test1.txt in order to get name,vendor and version of the applications installed on Solaris machine.The output stored in test1.txt in as shown below: NAME: Solaris Zones (Usr) VERSION: ... (4 Replies)
Discussion started by: omkar.jadhav
4 Replies

5. Shell Programming and Scripting

Row to Column conversion?

I have a text file with the geneIds separated by space in each line. The number Ids in lines are different. The file is like: abc qwe tyu ghj jkl dfg sdf cvb sdk fgh tyu uio iop tyu rty eru wer rty iop asd sdf dfg fgh zxc I want to format the file like: abc qwe tyu ghj jkl ... (7 Replies)
Discussion started by: sammy777
7 Replies

6. Shell Programming and Scripting

Awk: group rows by id and simple conversion

Hi all, I am a newbie to awk and trying to learn by doing examples. I got stuck at this relatively simple conversion. The start file looks like: 1 2 "t1" 1 3 "h1" 2 1 "h1" 2 2 "h2" and I want to convert it into 1 t1:2, h1:3; 2 h1:1, h2:2; Thanks. (9 Replies)
Discussion started by: eagle_fly
9 Replies

7. UNIX for Dummies Questions & Answers

Columns to rows conversion in unix

Hi All, My requirement is to convert the rows to columns, please help me how to do in one command. Ex: source file is having data like ABC,XYZ,123,987,KKK,XXX,666 Need output like ABC XYZ 987 KKK XXX 666 Regards, Pavan. (3 Replies)
Discussion started by: madsongtel
3 Replies

8. Shell Programming and Scripting

Date conversion in row while preserving rest of fields

Hi Forum. I searched the forum for a solution but could not find an exact one to my problem. I have some records in the file where I would like to convert the last date field to another format while preserving the rest of the other fields. For example: Found:... (6 Replies)
Discussion started by: pchang
6 Replies

9. UNIX for Dummies Questions & Answers

conversion and transpositions of columns and rows

Hi, I am wondering how to do this more efficiently than I am currently doing it: I have these 3 columns 3315 E14 1 397 E14 2 321 E14 3 27 E14 4 7 E14 5 2 E14 6 But I like them to be displayed like this: 1 2 ... (5 Replies)
Discussion started by: danieladna
5 Replies

10. Shell Programming and Scripting

column to row conversion with additional pattern

Hi there, I've an input file1 as follows: 1001 1002 1003 1004 1005 I would like to have an output file2 as follows: Numbers are 1001/ 1002/ 1003/ 1004/ 1005/ Any help is appreciated. (2 Replies)
Discussion started by: kbirde
2 Replies
Login or Register to Ask a Question