transpose into pipe delemited

I'm trying to transpose a list into a one line pipe delimited header in a bash script that is called from perl so could be done in either,

so I have

field1 0 5
field2 6 5
field3 12 10

the result I'm looking for is


I have around 300 of these to do and some are 300 lines long so I could copy into excel and do manually but would like to build into my scripts

I have been looking at this kind of thing... but not used awk very much so struggling.

awk -vORS= '{ print $1 }' listfile.txt | sed 's/|$//' > headerfile.txt

this was kind of working as a test when my list was pipe delimited, like

but the actual source files will be space delimited.

any help is appreciated.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
So, on each line, you want to delete everything after the first space?

sed 's/ .*//' /path/to/original/file > /path/to/new/file

Then simply replace \n with | and you're set.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
perl -0174lan012e 'print $F[0]' <<END
field1 0 5
field2 6 5
field3 12 10
Dan CraciunIT ConsultantCommented:
sed 's/ .*//' /path/to/original/file | tr '\n' '|' > /path/to/new/file

Open in new window

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

IanPaskinAuthor Commented:
good work, never thought about replacing '/n' with '|' just done a quick test and works well.

Dan CraciunIT ConsultantCommented:
You're welcome.

My second try actually does all in one line :)
Nice work, ozo.  Concise as always.

But could you please help me to understand a couple of things:

1. '-0174' specifies '|' as the record separator, doesn't it?  Is that for both input and output?  There are none in the input, so it will slurp the whole file, right?

2. I know '012' is '\n', but where is that syntax mentioned in:
      perl -h   ?
    The closest I can see is:
        -l[octal]         enable line ending processing, specifies line terminator
    but your 012 is not immediately after the -l, it's after the -n, which takes no arguments:
        -n                assume "while (<>) { ... }" loop around program

perldoc perlrun
       Switches include:

            specifies the input record separator ($/) as an octal or
            hexadecimal number.

So the switch is 0, the argument is 12
The first -0 specifies '|' as the input separator, the -l then uses the input separator as the output separator, then the last -0 re-specifies '\n' as the  input separator.
Ooooh.  Thanks ozo!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.