Transform two columns to columnar data using ksh script

54 views Asked by At

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.

1

There are 1 answers

1
dodrg On

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#*|}