Add a column at the end of all the lines in a file

Hi Guys, :smiley:

I am very much new to UNIX. I dont have much basics of coding in UNIX, so please help me out of thi ssituation.

I have a file say for ex: ABC.dtd and it contains "|" delimited data as

test1|testing|test3|moving
past1|runing|test4|going

I need to add a column at the end of each line as shown below.

test1|testing|test3|moving|D
past1|runing|test4|going|D

for this purpose I am using the command :searches the end of the line i.e "$" and then replaces with "D".

%s/$/D/g

The problem is before the search begins, first I need to implement a function that picks up the file from the specified location and performs search and replace and pushes back thhe file to the original location.
This overall function I need to implement in a ksh script.

Could you please help me out of this?????????

Thanks in advance.

ruthless
:cool:

Do you have a fixed location where you will take the file for modification,

Below is an example, which takes the file name with full path as the command line argument and modify that file.

You can modify the script, to call the function to know the file name...

#!/bin/ksh
sed 's/^\(.*\)$/&\\D/' $1 > $1_temp
mv "$1_temp" "$1"

Note: This is not tested.

try
perl -pi -e "s/$/|D/g" yourfilename

in this case the -i flag of perl actually overwrites the same file, there is no involment of creating a temp file and moving it back to the same name again

Thank you very much for helping me.

Mona[COLOR=Blue] , I have couple of questions for you . When I append "|D" at the end of the line, does it replaces the "$" which is the end of the line or just appends without affecting the "$" the end of the line indicator.
for ex:
incoming file:
a|S|D|F|G

output
a|S|D|F|G|D$

Does it works this way? I dont knw whether its a relevant question ro not. Please Please dont mind if its not!!!!!!!!!!

But do tell me how it works.

secondly, the location of the files is fixed, then how do I incorporate(define) the location of the file in the script?

Thanks a lot.

Hi,

  1. It will append \D without affecting the end of line character $. See the below example.
/export/home/test/mons>cat -vet samp
test1|testing|test3|moving$
past1|runing|test4|going$
/export/home/test/mons>sed 's/^\(.*\)$/&\\D/' samp | cat -vet
test1|testing|test3|moving\D$
past1|runing|test4|going\D$

If you see the regular expression in the sed you can understand better.

s/^\(.*\)$/&\\D/ - Takes the entire line and append \D in the last.

  1. If the location of the file is fixed then write a script to get the file name as a command line argument. Create a variable which contains the path(which u can hardcode) and the file name passed.

see an example below

#!/bin/ksh
file="/export/home/test/mons/"$1
sed 's/^\(.*\)$/&\\D/' $file > $file_temp
mv "$file_temp" "$file"

Note: Not tested

The exact script what I tested is
#!/bin/ksh
$file="/A/B/C/D/E/"mat.dtd
sed 's/^\(.*\)$/&\|D/' mat.dtd > $file_temp
mv "$file_temp" "xdm.dtd"

and the output I am getting is(as on the screen)

./test1.ksh[2]: =/A/B/C/D/E/mat.dtd: not found
a|b|s|C|r|q|r|D
b|s|r|e|o|s|k|D
p|i|e|w|m|e|t|D
1|2|3|4|5|6|g|D
Z|Q|R|T|S|L|j|D
1|d|x|a|p|y|m|D
mv: : cannot access: No such file or directory

When I am using ur first solution ,its works fine, but for that I need to work in the same shell.
For the above code,it is not able to access the source file(mat.dtd) from the location mentioned.It is taking the contents of the file only becos I am running code in the same dir and also not able to move back to the original file.

Could u pls find the mistake I am doing???

Thanks again.

Actually I did mistake while posting the code I was using. This is the code I am using. Only diff is there's no $ sign before "file".

#!/bin/ksh
file="/A/B/C/D/E/"mat.dtd
sed 's/^\(.*\)$/&\|D/' mat.dtd > $file_temp
mv "$file_temp" "mat.dtd"

Best regards,