Convert Header into Column in Text file

Hi Gurus,

I have a requirement like this and have to create a UX shell scripts. Thanks in advance.

File-in:
------
Header2007-12-012007-11-21
100|xyz|was
101|wsa|qws
......
.......

Output should be:
-------------------
2007-12-01|100|xyz|was
2007-12-01|101|wsa|qws
......
.......

pls suggest.Thanks again.

#!/bin/ksh

InpFile=$1
OutFile="$HOME/my_output.txt"
pattern=$(head -1 $1 | sed 's/Header\([0-9-]\{10\}\)[0-9-]*/\1/');
sed "/Header/d;s/^/$pattern|/g" $InpFile > $OutFile

Hi vsubbu1000, the "Contact Admins and Mods" forum description says:

"Post Here to Contact Site Administrators and Moderators Make Suggestions About Forums, Features, or Content here. Discuss Rules and Guidelines. Get Forum Support Here. (Registered Users Only)"

Do not post any queries regarding shell scripting/normal unix questions in that forum. For such questions, please use the other subforums available.

Hi Lorcan,

Output comes as:
-------------------
Header2007-12-012007-11-21|100|xyz|was
Header2007-12-012007-11-21|101|wsa|qws

but it should be:
--------------
2007-12-01|100|xyz|was
2007-12-01|101|wsa|qws

any idea?Thanks!

i am not sure as to why that happens, it works fine for me
try the below code

#!/bin/ksh

InpFile=$1
OutFile="$HOME/my_output.txt"
pattern=$(head -1 $1 | sed 's/Header\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)[0-9-]*/\1/' );
sed "/Header/d;s/^/$pattern|/g" $InpFile > $OutFile

When i tried to run the script in the debug mode it was returning the pattern as expected.

$ >ksh -x hd.ksh head.txt
+ InpFile=head.txt
+ OutFile=/home/my_output.txt
+ head -1 head.txt
+ sed 's/Header\([0-9-]\{10\}\)[0-9-]*/\1/'
+ pattern=2007-12-01
+ sed '/Header/d;s/^/2007-12-01|/g' head.txt
+ 1> /home/my_output.txt
awk '{ if ( NR == 1 ) { x = substr($0, length("Header") + 1, 10) } else { $0 = x"|"$0; print } }' filename

Hi Lorcan,

I got it with small changes.Header length is fixed always.Thanks.

thats:

#!/bin/ksh
InpFile=$1
OutFile="my_output.txt"
pattern=$(head -1 $1 | awk '{print substr($1,10,10)}');
sed "/Header/d;s/^/$pattern|/g" $InpFile > $OutFile
exit 1

I think you can try follow:
1>find out the 2007-12-01 in your file
2>add it to the beginner of your input file
3> use temp file 11.txt to store 'Header2007-12-012007-11-21'
as 'Header,2007-12-01,2007-11-21'

sed -n '1s/\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)/,&1,/p' 1.txt > 11.txt
temp=`awk 'BEGIN{FS=","} {print $2}' 11.txt`
awk '{if (NR!=1) printf("%s|%s\n","'"$temp"'",$0)}' 1.txt