-1

I have a file with static format like below.

column|Data

Example:

Name|Joe
Surname|Robinson
Country|US
Gender|Male

I am expecting below output using a ksh script

Name|Surname|Country|Gender
Joe|Robinson|US|Male

Please suggest what approach I can use.

Arnaud Valmary
  • 2,039
  • 9
  • 14
Shri_0303
  • 1
  • 1
  • If you have `rs` available (or can get your sysadmin to install it), then that's the tool for this job. Which then means it's not a programming problem. – Toby Speight Aug 17 '23 at 18:36
  • 1
    Does [this](https://stackoverflow.com/questions/1729824/an-efficient-way-to-transpose-a-file-in-bash) answer your question? – tink Aug 17 '23 at 18:42
  • Does this answer your question? [An efficient way to transpose a file in Bash](https://stackoverflow.com/questions/1729824/an-efficient-way-to-transpose-a-file-in-bash) – shellter Aug 17 '23 at 18:50
  • hanks for your help!! This did not work for me. however we figured out a workaround where we loading the data as is into the table and then writing SQL to pivot it – Shri_0303 Aug 18 '23 at 07:29

1 Answers1

1

You expect a ksh solution.
As ksh is sh-compatible a sh script is fitting too. Additionally it maximizes portability:

#!/bin/sh
# Expects the data file as first parameter.

for input in $(cat $1); do
    header="${header}|${input%|*}"
    data="${data}|${input#*|}"
done
echo ${header#*|}
echo ${data#*|}

If you prefer to use a pipe the suggestion of @Fravadona is the script for you:

#!/bin/sh
# Accepts the data from pipe

while IFS='|' read -r key value; do
    header="${header}|${key}"
    data="${data}|${value}"
done
echo ${header#*|}
echo ${data#*|}
dodrg
  • 1,142
  • 2
  • 18
  • Thanks for your help!! This did not work for me. however we figured out a workaround where we loading the data as is into the table and then writing SQL to pivot it – Shri_0303 Aug 18 '23 at 07:29