Visit Our UNIX and Linux User Community


append existing file with zeroes bsed on position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting append existing file with zeroes bsed on position
# 1  
Old 10-28-2009
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 from position 50-56 & 58-64.
3rd record same like 2nd record.
4th record same like 2nd record
5th record empty space is there only position 58-64.
6th record same like 2nd record.
7th record.......
8th record.........
..........
........etc.
like wise i may have many records in FILE1.

I want to write a program in .KSH where it append the FILE1 with zeroes on the position mention above and want the outfile FILE2. All the records will begin with no 2.

Any help is appreciated.

FILE1:

26666666660000001 B86860003OLFXXX592123320081227 0000002 0000003-
27777777770000653-B94030001OLFXXX000000120081227
28888888880000654-E71060001OLFXXX000000220081227
29999999990000655 E71060002OLFXXX000000320081227
21112223330000101 E71060002OLFXXX000000320081227 0000102
22222222220000201 E71060002OLFXXX000000320081227

FILE2:

26666666660000001 B86860003OLFXXX592123320081227 0000002 0000003-
27777777770000653-B94030001OLFXXX000000120081227 0000000 0000000
28888888880000654-E71060001OLFXXX000000220081227 0000000 0000000
29999999990000655 E71060002OLFXXX000000320081227 0000000 0000000
21112223330000101 E71060002OLFXXX000000320081227 0000102 0000000
22222222220000201 E71060002OLFXXX000000320081227 0000000 0000000

thanks
# 2  
Old 10-28-2009
Code:
awk -F'[ |-]' '{$0=(NF==3)?$0 OFS f:((NF==2)?$0 OFS f OFS f:$0)}1' f="0000000" file

# 3  
Old 10-28-2009
Quote:
Originally Posted by danmero
Code:
awk -F'[ |-]' '{$0=(NF==3)?$0 OFS f:((NF==2)?$0 OFS f OFS f:$0)}1' f="0000000" file

thanks danmero !!

I am getting the following error when i run your code
[CODE]
Code:
>awk -F'[ |-]' '{$0=(NF==3)?$0 OFS f:((NF==2)?$0 OFS f OFS f:$0)}1' f="0000000" wedtest3_appnd_zero.txt

awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

BTW in your code where is the input file parameter and output file parameter.

thnks

Last edited by kshuser; 10-28-2009 at 06:30 PM..
# 4  
Old 10-28-2009
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.
file is the input and you can redirect the output to another file(2).

Previous Thread | Next Thread
Test Your Knowledge in Computers #316
Difficulty: Easy
RAM stands for Registered Access Memory.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching column then append to existing File as new column

Good evening I have the below requirements, as I am not an experts in Linux/Unix and am looking for your ideas how I can do this. I have file called file1 and file2. I need to get the second column which is text1_random_alphabets and find that in file 2, if it's exists then print the 3rd... (4 Replies)
Discussion started by: mychbears
4 Replies

2. Shell Programming and Scripting

Perl - Append data to existing excel cell

Hello All, I have the following code in PERL to write data to excel sheet. Can someone please help me about how to append data to an exisitng cell? For ex in the below given case,Cell 1,1 has Active State PERL Now I want to add a new line like "prorgamming" without overwritting the... (3 Replies)
Discussion started by: prasperl
3 Replies

3. Shell Programming and Scripting

Append to existing line

I have a file which has lines that end with a plus (+) sign. I would like to get the next line appended to the one with the plus. For example bla bla bla bla bla + blip blip blip would become bla bla bla bla bla blip blip blip However not all lines end with a plus sign . I would... (2 Replies)
Discussion started by: bombcan
2 Replies

4. Shell Programming and Scripting

Append files to a existing tar file.

Hi all, I want to check whether tar file exists in the directory or not. If tar file exists in the directory then I want to append the files to it. I am using the below command to tar files if the file does not exist. tar zcvf <tar file name> <Files to append> However, if want to... (4 Replies)
Discussion started by: Nagaraja Akkiva
4 Replies

5. 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

6. Shell Programming and Scripting

append each line on fixed position 31 to 33

I have a .DAT file like below. 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... (5 Replies)
Discussion started by: kshuser
5 Replies

7. Shell Programming and Scripting

Add leading zeroes to numbers in a file

Hello, I am (trying) to write a script that will check to see how many users are logged on to my machine, and if that number is more than 60 I need to kill off all the oldest sessions that are over 60. So far I have been able to check how many users are on and now I am at the part where I have to... (3 Replies)
Discussion started by: raidzero
3 Replies

8. Shell Programming and Scripting

append newline to existing variables

firstly, i check is the variable empty or not, if so vesselNameList=`echo $vesselName` if not vesselNameList="${vesselNameList}""\n"`echo "$vesselName"` and it produces this result BUNGA TERATAI 3 5055\ JADE TRADER 143W\ MOL SPLENDOR 0307A BUNGA TERATAI 3 5055\ JADE... (1 Reply)
Discussion started by: finalight
1 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

10. UNIX for Advanced & Expert Users

Shell script to append a time for the existing error log file

Hi Guys, the requirement is like this, i am having a error log file in this format, 4594.493: parallel nursery GC 2594592K->2544691K (2969600K), 30.848 ms 4605.958: parallel nursery GC 2634887K->2584986K (2969600K), 38.900 ms 4619.079: parallel nursery GC 2822555K->2774812K... (12 Replies)
Discussion started by: gsprasanna
12 Replies

Featured Tech Videos