Need to Add date on first position.Plz help

We have a file as shown below with Header and details below Header.Here Header indicates date field.
i Want to add date i.e 20100928 as a first position as shown below without header.Please help us using unix script
Source file:
H20100928
D04041201609360677PC790010384I
D04041203326357853PC790010645R
D04041203602983265PC790010740R
D04041208423493600PC790011258I

Output File should be as :

20100928D04041201609360677PC790010384I
20100928D04041203326357853PC790010645R
20100928D04041203602983265PC790010740R
20100928D04041208423493600PC790011258I

awk 'NR==1{sub("H","");d=$0;next}{$0=d$0}1' source > output
1 Like

may be:

awk 'NR==1 { date= substr($0,2,8)} NR> 1 { print date$0 }' source_file
1 Like
awk 'NR==1{s=substr($0,2);next}$0=s $0' file
1 Like
$
$
$ cat f29
H20100928
D04041201609360677PC790010384I
D04041203326357853PC790010645R
D04041203602983265PC790010740R
D04041208423493600PC790011258I
$
$ awk 'NR==1{x=$0; sub(/^H/,"",x)} NR>1{print x$0}' f29
20100928D04041201609360677PC790010384I
20100928D04041203326357853PC790010645R
20100928D04041203602983265PC790010740R
20100928D04041208423493600PC790011258I
$
$

tyler_durden

within shell,

n=1
while read line
do
 [ $n -eq 1 ] && dt=$(expr $line : '.\(........\)') || echo ${dt}${line}
 n=$(( n+1 ))
done < source_file
ruby -ne 'h=$_[1..-1].chomp! if $.==1; print "#{h}#{$_}" if $.>1 ' file

And Perl -

$
$ perl -lne '$.==1 ? do{/.(.*)/ and $x=$1} : print "$x$_"' f29
20100928D04041201609360677PC790010384I
20100928D04041203326357853PC790010645R
20100928D04041203602983265PC790010740R
20100928D04041208423493600PC790011258I
$
$ perl -lne '$.==1 ? $x=substr($_,1) : print "$x$_"' f29
20100928D04041201609360677PC790010384I
20100928D04041203326357853PC790010645R
20100928D04041203602983265PC790010740R
20100928D04041208423493600PC790011258I
$

tyler_durden

Hi Friends thanks a lot....for ur replies its worked fine...i have one more question..
if i need to remove trailor (T000000004 ) as shown below file also with header.Plz help thanks
Source file:
H20100928
D04041201609360677PC790010384I
D04041203326357853PC790010645R
D04041203602983265PC790010740R
D04041208423493600PC790011258I
T000000004

Output File should be as :
20100928D04041201609360677PC790010384I
20100928D04041203326357853PC790010645R
20100928D04041203602983265PC790010740R
20100928D04041208423493600PC790011258I

---------- Post updated at 08:25 AM ---------- Previous update was at 08:25 AM ----------


Hi Friends thanks a lot....for ur replies its worked fine...i have one more question..
if i need to remove trailor (T000000004 ) as shown below file also with header.Plz help thanks
Source file:
H20100928
D04041201609360677PC790010384I
D04041203326357853PC790010645R
D04041203602983265PC790010740R
D04041208423493600PC790011258I
T000000004

Output File should be as :
20100928D04041201609360677PC790010384I
20100928D04041203326357853PC790010645R
20100928D04041203602983265PC790010740R
20100928D04041208423493600PC790011258I

awk 'NR==1{s=substr($0,2);next}p{print p}{p=s $0}' file
1 Like
$
$ cat f29
H20100928
D04041201609360677PC790010384I
D04041203326357853PC790010645R
D04041203602983265PC790010740R
D04041208423493600PC790011258I
T000000004
$
$
$ awk 'NR==1{x=$0; sub(/^H/,"",x)} NR>1 && /^[^T]/{print x$0}' f29
20100928D04041201609360677PC790010384I
20100928D04041203326357853PC790010645R
20100928D04041203602983265PC790010740R
20100928D04041208423493600PC790011258I
$
$ perl -lne '$.==1 ? do{/.(.*)/ and $x=$1} : print "$x$_" if /^[^T]/' f29
20100928D04041201609360677PC790010384I
20100928D04041203326357853PC790010645R
20100928D04041203602983265PC790010740R
20100928D04041208423493600PC790011258I
$
$ perl -lne '$.==1 ? $x=substr($_,1) : print "$x$_" if /^[^T]/' f29
20100928D04041201609360677PC790010384I
20100928D04041203326357853PC790010645R
20100928D04041203602983265PC790010740R
20100928D04041208423493600PC790011258I
$
$

Note that these one-liners prefix the date only for those lines that are other than the first as well as do not start with "T". They aren't specifically looking for (in order to avoid) the last line that starts with "T".

tyler_durden

$ ruby -ne 'h=$_[1..-1].chomp! if $.==1; print "#{h}#{$_}" if $.>1 and not $_[/^T/]' file

Hi thanks its working perfectly..hi can you also plz modify this command:
awk 'NR==1 { date= substr($0,2,8)} NR> 1 { print date$0 }' source_file
to remove trailor record as shown below:

H20100928
D04041201609360677PC790010384I
D04041203326357853PC790010645R
D04041203602983265PC790010740R
D04041208423493600PC790011258I
T000000004

Output File should be as :
20100928D04041201609360677PC790010384I
20100928D04041203326357853PC790010645R
20100928D04041203602983265PC790010740R
20100928D04041208423493600PC790011258I

---------- Post updated at 09:57 AM ---------- Previous update was at 09:53 AM ----------

if i have to remove the spaces also after header with spacess and tailor with spacess.

---------- Post updated at 09:57 AM ---------- Previous update was at 09:57 AM ----------

plz sugest command..
thanks
in advance

awk '/^H/{s=substr($0,2)}/^D/{print s $0}' file

Why not figure out all your problems and jot down all your questions in a single post, instead of asking them in installments ?

tyler_durden

1 Like

I totally agree with that!

sed "s/^/$(date +%Y%m%d)/" infile | tail -n+2 | head -n-1
awk 'NR==1 { date= substr($0,2,8)} NR>1&&!/^[H|T]/ { print date$0 }' source_file

Guess you have several sessions with H2010XXXX to T0000xxxxx block, such as:

H20100928
D04041201609360677PC790010384I 
D04041203326357853PC790010645R 
D04041203602983265PC790010740R 
D04041208423493600PC790011258I 
T000000004 
H20100928
D04041201609360677PC790010384I 
D04041203326357853PC790010645R 
D04041203602983265PC790010740R 
D04041208423493600PC790011258I 
T000000004 

then your code can be changed to:

awk '/^H/ { date=substr($0,2,8);next} !/^T/ { print date $0 }' source_file