Need to Add date as on first position.

Dfd / Dfdf
October 6, 2010 at 05:41:10
Specs: unix, 2gb
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


See More: Need to Add date as on first position.

Report •

#1
October 6, 2010 at 07:08:43

sed '1d' source.txt|sed 's/^/20100928/' > output.txt

Report •

#2
October 6, 2010 at 08:07:17
Hi Sir thanks a lot....for ur repliy 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 ..header and tailor can have spacess also i need to delete that also
Source file:
H20100928
D04041201609360677PC790010384I
D04041203326357853PC790010645R
D04041203602983265PC790010740R
D04041208423493600PC790011258I
T000000004

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


Report •

#3
October 6, 2010 at 12:17:24
You are welcome.

First, calling me Sir is like putting a ribbon on a pig. -) You can call me Nails.

Second, this script deletes the first and last line, no matter what they are:

sed '1d' source.txt|sed '$d'|sed 's/^/20100928/' > output.txt

Third, another way is to use sed's -e option to allow multipe edits on a file:

sed -e '1d' -e '$d' -e 's/^/20100928/' source.txt > output.txt

This 2nd method is more efficient and would be faster on a large file.


Report •
Related Solutions


Ask Question