Need to append a text in a specific Position

June 14, 2011 at 06:04:16
Specs: Unix
Hi,

I want to add the text "INS" at the 234-236 position of all the lines available in Files. I have some 10Lac records in my file.


See More: Need to append a text in a specific Position

Report •

#1
June 14, 2011 at 13:10:30
This link describes something similar to what you want to do, only the lines are not as large. One cuts the string in 3 pieces and one uses a regular expression in a perl script:

http://www.computing.net/answers/un...


Report •

#2
June 14, 2011 at 23:35:20
Hi Nails,

Thanks for the reply..My case here is..

I have some multiple records in a file.. So i have used "For Loop" to get the work done.

My record length of each record is 233.. However i have blank spaces from its last field(Which varies from 175-178). So i dont have any values inbetween 175 to 233..

I have used the below script:

for line in `cat hids.txt`
do
l1=$(echo "$line"|cut -c1-233)
l2="INS"
newline="${l1}${l2}"
echo $newline >> hids1.txt
done


Here is the output of a single record for your observation:

0000022e200504131528180000022e79949586847181Z00000019998Z16A0000555581802BOYERINS
NATIVIDADINS
2002010120020101200201010000000000000000ortegapx,8025PPO,CSR2INS.

As you could see.. the value "INS" is not corrected appended only at the end of the line. I could see the value "INS" three times in a single line..

I am not sure,what is wrong with my script..

Thanks


Report •

#3
June 15, 2011 at 09:29:56
I thought you wanted to add the text somewhere in the line because your line length was greater than 233? Are you saying that you want to append the string to the end of each line in the file? If that is the case why not use a sed script to append to the end of the line. sed doesn't care about the spaces:

sed 's/$/INS/g' hids.txt > newhids.txt

Regarding your script, I wouldn't use a for loop like that because the for statement parses the white space. I would use a while loop:


#!/bin/ksh

while read line
do
.
.
.
done < hids.txt



Report •

Related Solutions

#4
June 15, 2011 at 22:56:21
Nails..

Yes..I want to add the value at the end of each line. My last field length is 50 Characters(183 to 233). sometimes i have no value in the last field. So if i use the above sed command or while command, i am getting the value "INS" appended at the end of the last field.

For example.. If i have only 10 characters in the last field, i have "INS" appended from 193 position.. I dont want this case.

I am looking for the value "INS' only in 234-236, after the end of the last field,irrespective of spaces/value in last field..

I hope you got my point...

Thanks again...


Report •

#5
June 16, 2011 at 12:23:49
If I am interpreting what you are saying is that you expect to have fixed length records of 233 characters unless your last field isn't defined; in that case you have less than 233.

I suggest that before you start trying to insert INS at the end of the line, that you make sure that each line is 233 characters. This stub program reads each line and prints out 233 characters. Any line with less than 233 characters pads with spaces so each line is guaranteed to be 233 spaces:

#!/bin/ksh

# UNTESTED
while read line
do
   printf "%-233s\n" "$line"
done <  hids1.txt > newhids1.txt
# end script

Now, with this newfile, the sed command I previously posted should work.


Report •

#6
June 16, 2011 at 20:40:42
Great Nails..It worked like a charm..

Have a good day..

Thanks
Gobi


Report •

Ask Question