China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 08:48
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to add a line at the beginning of a text file View 1,490 Replies 12
Original Poster Posted 2009-01-27 03:00 ·  中国 四川 南充 仪陇县 电信
初级用户
★★
Credits 100
Posts 80
Joined 2008-09-12 15:08
17-year member
UID 125478
Gender Male
Status Offline
I have several hundred text files, and I want to insert one line at the beginning of each file:

For example, one of my files is:
5471,An Degang,"1,100.00",2000-6-18,2001-4-20,2003-9-21,bad debt,loss,,210.87,4.851E+13,8.81301E+13
5471,An Degang,"9,670.00",1997-10-8,1998-9-20,2003-9-21,bad debt,loss,,4659.83,4.851E+13,8.81301E+13
5471,An Degang,"3,120.00",1998-9-23,1999-9-20,2003-9-21,bad debt,loss,,598.11,4.851E+13,8.81301E+13
……

I want to insert this line at the beginning:
Institution,Account Name,Balance,Loan Date,Due Date,Interest Start Date,Level 4,Level 5,On-balance Interest,Off-balance Interest,Customer No.,IOU No.

How should I write the code?
Using sed is fine too.

I've searched online for a long time and still couldn't find suitable code. Please help!
Floor 2 Posted 2009-01-27 11:04 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
┌───────┐
├→学习→实践→┤
└───────┘
Floor 3 Posted 2009-01-28 00:05 ·  中国 四川 南充 仪陇县 电信
初级用户
★★
Credits 100
Posts 80
Joined 2008-09-12 15:08
17-year member
UID 125478
Gender Male
Status Offline
Thanks, brother wxcute.

If I only want to add that line to the original file itself, instead of generating another file with "new_" plus the original filename, how should I write the code?
Floor 4 Posted 2009-01-28 00:14 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
Generate another file with "new_" plus the original filename, then change it back to the original filename, and doesn't that become
the original file? As long as it achieves the goal, that's fine, don't be so rigid about it. If you really can't stand the old one, just delete it.
一个专搜注册信息的链接注册信息搜索--历史是一个旋转的车轮,它的轨迹总是那样的相似回复后可见
Floor 5 Posted 2009-01-28 00:26 ·  中国 四川 南充 仪陇县 电信
初级用户
★★
Credits 100
Posts 80
Joined 2008-09-12 15:08
17-year member
UID 125478
Gender Male
Status Offline
Floor 6 Posted 2009-01-28 11:53 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
That works too, just remove the “new_” string.
┌───────┐
├→学习→实践→┤
└───────┘
Floor 7 Posted 2009-01-28 19:33 ·  中国 广西 南宁 电信
初级用户
Credits 115
Posts 45
Joined 2007-03-03 10:09
19-year member
UID 80655
Gender Male
Status Offline
sed -i "1iInstitution,Account Name,Balance,Loan Date,Due Date,Interest Start Date,Level 4,Level 5,On-balance-sheet Interest,Off-balance-sheet Interest,Customer No.,IOU No." *.txt


[ Last edited by ceii on 2009-1-28 at 19:35 ]
Floor 8 Posted 2009-01-29 13:35 ·  中国 四川 宜宾 联通
初级用户
Credits 56
Posts 40
Joined 2009-01-23 23:49
17-year member
UID 137337
Gender Male
Status Offline
Good, very practical.
Floor 9 Posted 2009-01-31 02:12 ·  中国 四川 南充 阆中市 电信
初级用户
★★
Credits 100
Posts 80
Joined 2008-09-12 15:08
17-year member
UID 125478
Gender Male
Status Offline
Floor 10 Posted 2009-02-01 10:47 ·  中国 广西 南宁 电信
初级用户
Credits 115
Posts 45
Joined 2007-03-03 10:09
19-year member
UID 80655
Gender Male
Status Offline
This is a version issue.
I'm using GNU sed version 4.0.7
Floor 11 Posted 2009-02-01 12:02 ·  新加坡 Microsoft
初级用户
★★
Credits 100
Posts 93
Joined 2009-01-14 15:04
17-year member
UID 136661
Gender Male
From 湖南
Status Offline
Post #2 is pretty good.
But there's one drawback: if the original TXT file contains "branch, account name, balance, loan date, maturity date, interest start date, category four, category five, on-balance-sheet interest, off-balance-sheet interest, customer number, IOU number", and also has other content.
set/p fh=<"%%i"
if not "!fh!"=="%head%" copy/b head.ini+"%%i" "new_%%i"

Should some other check be added as well?
Floor 12 Posted 2009-02-05 21:58 ·  中国 黑龙江 哈尔滨 电信
新手上路
Credits 15
Posts 15
Joined 2009-01-24 10:46
17-year member
UID 137352
Gender Male
Status Offline
Oh, so it can be done like this too.
Learned something.
Floor 13 Posted 2009-07-13 07:00 ·  中国 北京 联通
新手上路
Credits 13
Posts 8
Joined 2007-05-11 19:31
19-year member
UID 88211
Gender Male
Status Offline
@echo institution,account name,balance,loan date,maturity date,interest start date,4th category,5th category,on-balance-sheet interest,off-balance-sheet interest,customer number,loan note number >>header.txt
copy/b header.txt+detal.txt
del detal.txt
ren header.txt detal.txt
Forum Jump: