IanPaskin
asked on
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
field1|field2|field3|
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
field1|0
field2|6
field3|12
but the actual source files will be space delimited.
any help is appreciated.
so I have
field1 0 5
field2 6 5
field3 12 10
the result I'm looking for is
field1|field2|field3|
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
field1|0
field2|6
field3|12
but the actual source files will be space delimited.
any help is appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sed 's/ .*//' /path/to/original/file | tr '\n' '|' > /path/to/new/file
ASKER
good work, never thought about replacing '/n' with '|' just done a quick test and works well.
Thanks...
Thanks...
You're welcome.
My second try actually does all in one line :)
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
Thanks.
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
Thanks.
perldoc perlrun
...
Switches include:
-0[octal/hexadecimal]
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.
...
Switches include:
-0[octal/hexadecimal]
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!
field1 0 5
field2 6 5
field3 12 10
END