append each line on fixed position 31 to 33


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting append each line on fixed position 31 to 33
# 1  
Old 10-30-2009
append each line on fixed position 31 to 33

I have a .DAT file like below.

Code:
26666666660001343 000001004OLF 029100020090820
27777777770000060 000001004ODL-CH001000020090820
28888888880000780 000001013OLF 006500020090820

.......
........
and so on.....

I want to append each line in a file in .KSH script with XXX with position starting from 31 to 33 in the above file the first record on the position 31-33 empty space is there.. i want to replace with XXX only on this position.

i want the result should be like this..
Code:
26666666660001343 000001004OLFXXX029100020090820
27777777770000060 000001004ODL-CH001000020090820
28888888880000780 000001013OLFXXX006500020090820

Can anyone please send me the .KSH script to do this..

thanks

Last edited by vgersh99; 10-30-2009 at 07:20 PM.. Reason: code tags, please!
# 2  
Old 10-30-2009
Code:
awk 'BEGIN{FS=OFS=""}$31==" "{$31=$32=$33="X"}1' file

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.
# 3  
Old 10-30-2009
I would try something with cut -d' ' -f3 so that when there is no space there isn't no third field too.
so test the third field and if not empty add 'XXX' at the beginning of that field. Concatenate with the fisrt two fields and it's done.
I can give you an idea of scripting that in bash if you want.
oops ! maybe danmero has found something better with awk !
# 4  
Old 10-30-2009
Well i just used nawk but the outfile remain the same no change..

Code:
nawk 'BEGIN{FS=OFS=""}$31==" "{$31=$32=$33="X"}1' Infile.txt > friday.txt


I am using KSH.

Last edited by vgersh99; 10-30-2009 at 07:22 PM.. Reason: code tags, please
# 5  
Old 10-30-2009
Try this

Code:
$cat insertxxx.sh

#!/bin/ksh

myFile="${1}"
myOutput="${2}"

awk '{foo=substr($0,31,1); if(foo == "\ ") {$2=$2"XXX"; print $1 "\ " $2 $3;} else print $0;}' "${myFile}" > "${myOutput}"

# 6  
Old 10-30-2009
Thanks all
i used nawk and it works...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting based on occurence of a Character at fixed position

I have a requirement where i need to split a file based on occurence of a character which is present at a fixed position. Description is as below: 1. The file will be more than 1 Lakh records. 2. Each line will be of fixed length of 987 characters. 3. At position 28 in each line either 'C' or... (9 Replies)
Discussion started by: Neelkanth
9 Replies

2. Shell Programming and Scripting

Fixed width file search based on position value

Hi, I am unable to find the right option to extract the data in the fixed width file. sample data abcd1234xgyhsyshijfkfk hujk9876 io xgla loki8787eljuwoejroiweo dkfj9098 dja Search based on position 8-9="xg" and print the entire row output ... (4 Replies)
Discussion started by: onesuri
4 Replies

3. Shell Programming and Scripting

Append spaces the rows to make it into a required fixed length file

I want to make a script to read row by row and find its length. If the length is less than my required length then i hav to append spaces to that paritucular row. Each row contains special characters, spaces, etc. For example my file contains , 12345 abcdef 234 abcde 89012 abcdefgh ... (10 Replies)
Discussion started by: Amrutha24
10 Replies

4. UNIX for Dummies Questions & Answers

Using grep to check for character at fixed position

i have a file (test.txt) that contains: 20799510617900000928000000005403020110315V 20799510617900000928000000005403020110316 20799510617900000928000000005403020110317 20799510617900000928000000005403020110318V grep V test.txt > /tmp/void.log if then mail -s "void" < test.txt fi... (2 Replies)
Discussion started by: tjmannonline
2 Replies

5. Shell Programming and Scripting

Remove the spaces at the end of a line starting from a fixed position

I want to remove the trailing spaces at the end of each line starting from a particular position(using ksh script). For example, in the attached file, I want to remove all the spaces starting from the position 430 till the end. The space has to be removed only from the 430th position no matter in... (3 Replies)
Discussion started by: Suryaaravindh
3 Replies

6. Shell Programming and Scripting

append the position 28:33

I have a file FILE1.DAT like below 21111111110001343 000001004OLF-AA029100020091112 21111111110000060 000001004ODL-CH001000020091112 24444444440001416 000001045OLF-AA011800020091112 23333333330001695 000001039OLF-AA030600020091112 23333333330000111 000001039ODL-SP002000020091112... (2 Replies)
Discussion started by: new2ksh
2 Replies

7. Shell Programming and Scripting

append existing file with zeroes bsed on position

Hi i am trying to append value with 0 to an existing file in the position 50-56 & 58-64 only where empty space is there Rule: 1 row already has some value and i do not want to change anything for this row. 2nd record below you see the position 50-64 is empty, i want to replace with 0000000... (3 Replies)
Discussion started by: kshuser
3 Replies

8. UNIX for Dummies Questions & Answers

Append line and variable at fixed postion in file in unix

Hi I have a input file ::: 1583904589034853904 1690234849023849023 159823890238409 1690238490238490238490 . . . The output file should have the record 16 appended to the record 15 and a variable should be added at a FIXED POSTION at 55. The records are been processed inside a loop... (3 Replies)
Discussion started by: akashhello
3 Replies

9. Shell Programming and Scripting

Append line based on fixed position

Hi all; I'm having headache on append one line to another based on the fix position.Hope u guys can help. All i need to do is append the line that start with '3' to a line which start with '1' and the position for line 3 that i need to append is 22. The original file look like this: ... (2 Replies)
Discussion started by: ashikin_8119
2 Replies
Login or Register to Ask a Question