Need spaces in line after few charcacters


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Need spaces in line after few charcacters
# 1  
Old 02-20-2013
Need spaces in line after few charcacters

This is my headerHJKLName00014200012000000600424618201after head 201 I am looking for 382 space.total wc will be 421 then
can you tell me how can I accomplish this

we are performing some more actions in script, which is working fine..but this part I need help.please suggest where changes need to be made. I think in printf statement


Code:
nawk ' NR == 1 {
                HF = substr($0,1,15);
                HC = substr($0,16,6);
                HR = substr($0,22,14);
                HE = substr($0,36,3);
                HS = substr($0,39,382);
} /COOLTV/ {
                R  = $3;
                sub(/0+$/,x,R);
                HC -= 2;
                HR -= R;
                getline; next;
} !/COOLTV/ {
                print $0 > "newfile";
} END {
                printf "%s%06d%014d%d", HF, HC, HR, HE, Hs > "header";
} ' $1


Last edited by mirwasim; 02-20-2013 at 11:35 PM..
# 2  
Old 02-20-2013
My bad, the solution I suggested for extra spaces will not work.

You can apply below modification:
Code:
nawk ' NR == 1 {
                HF = substr($0,1,15);
                HC = substr($0,16,6);
                HR = substr($0,22,14);
                HE = substr($0,36,3);
                HS = substr($0,39);
} /COOLTV/ {
                R  = $3;
                sub(/0+$/,x,R);
                HC -= 2;
                HR -= R;
                getline; next;
} !/COOLTV/ {
                print $0 > "newfile";
} END {
                printf "%s%06d%014d%-382d", HF, HC, HR, HE, HS > "header";
} ' $1

I hope this helps.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-20-2013
[QUOTE=bipinajith;302771493]My bad, the solution I suggested for extra spaces will not work.

Last edited by mirwasim; 02-21-2013 at 12:06 AM..
# 4  
Old 02-20-2013
Quote:
Originally Posted by mirwasim
its printing 0 at last of space
That is weird! I have no idea from where that extra 0 is coming from! Smilie

---------- Post updated at 21:25 ---------- Previous update was at 21:16 ----------

Can you check your input file again? I guess there is an extra 0 after 201
This User Gave Thanks to Yoda For This Post:
# 5  
Old 02-20-2013
Quote:
Originally Posted by bipinajith
That is weird! I have no idea from where that extra 0 is coming from! Smilie

---------- Post updated at 21:25 ---------- Previous update was at 21:16 ----------

Can you check your input file again? I guess there is an extra 0 after 201
I checked there is no 0 after 201 and why its not printing newfile as well
# 6  
Old 02-20-2013
Quote:
Originally Posted by mirwasim
I checked there is no 0 after 201 and why its not printing newfile as well
I'm not sure about the extra 0

BTW the code does not print newfile It is just writing or redirecting the output to file: newfile. Check the directory you are running this code and you will find a file: newfile
This User Gave Thanks to Yoda For This Post:
# 7  
Old 02-21-2013
I current directory no file is generating
see i am putting output here
Code:
[wasim]$ ksh -x try.ksh file_new
+ nawk  NR == 1 {
                HF = substr($0,1,15);
                HC = substr($0,16,6);
                HR = substr($0,22,14);
                HE = substr($0,36,3);
                HS = substr($0,39);
                sub(/0+$/,x,R);
                HC -= 2;
                HR -= R;
                getline; next;
} END {
                printf "%s%06d%014d%d%-382d", HF, HC, HR, HE, HS > "header";
}  cpm1201301021209
+ + cat header
HDR=HJkl1Name000142000118000006004246182010                                                                                                                                                                                                                                                                               
+ nawk -v H=HJkl1Name000142000118000006004246182010                                                                                                                                                                                                                                                                                                                                                                                              NR==1{ sub($0,H) }1 newfile
+ 1> temp
nawk: can't open file newfile
 source line number 1
+ mv temp file_new
+ rm newfile
newfile: No such file or directory

---------- Post updated at 09:24 AM ---------- Previous update was at 09:17 AM ----------

My Bad..oops!! i was missing a line..got rid of Zero n see new file.

checking if I am getting correct spaces

---------- Post updated at 12:03 PM ---------- Previous update was at 09:24 AM ----------

Quote:
Originally Posted by bipinajith
My bad, the solution I suggested for extra spaces will not work.

You can apply below modification:
Code:
nawk ' NR == 1 {
                HF = substr($0,1,15);
                HC = substr($0,16,6);
                HR = substr($0,22,14);
                HE = substr($0,36,3);
                HS = substr($0,39);
} /COOLTV/ {
                R  = $3;
                sub(/0+$/,x,R);
                HC -= 2;
                HR -= R;
                getline; next;
} !/COOLTV/ {
                print $0 > "newfile";
} END {
                printf "%s%06d%014d%-382d", HF, HC, HR, HE, HS > "header";
} ' $1

I hope this helps.
Thanks a TON..
got it correct..

what I was wondering is, say if I have many Entries with name COOLTV to delete with its immediate line..
then how can we use for loop in awk. and it should ask subtract that value from header.

say we are deleting 4 record with COOLTV. the each will have different value , how we can add all values and subtract from header
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove spaces on a line?

Hi, suppose I have the following data: albert music=top40 age=20 bob music=punk rock age=25 candy music=r n b age=22 dave music=mozart or bach only age=30 I want to extract and manipulate the music column but it's got spaces in it. How can I substitute the space with an underscore... (2 Replies)
Discussion started by: almonds
2 Replies

2. Shell Programming and Scripting

append end of line with 8 spaces

child_amt=$amount prev_line="$prev_line $child_amt" i am getting the result like this 21234567890001343 000001004OLFXXX029100020091112 0000060 but i want 8 spaces between the eg: 21234567890001343 000001004OLFXXX029100020091112 0000060 how can i do this in .ksh (1 Reply)
Discussion started by: kshuser
1 Replies

3. Shell Programming and Scripting

Removing spaces in a line

Hi All, I have a line like this " field1;field2;field3 " (single space after and before double quotes). Now i have to remove these single space . Kindly help me. Thanks in advance (2 Replies)
Discussion started by: krishna_gnv
2 Replies

4. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

5. Shell Programming and Scripting

remove trailing spaces from a line

I want to remove the trailing spaces from any line of file. line ending does not follow any pattern. plz help (3 Replies)
Discussion started by: vikas_kesarwani
3 Replies

6. Shell Programming and Scripting

Remove extra spaces in a line

Hi, I need a help in deleting extra spaces in a text. I have a huge file, a part of it is :- 3 09/21/08 03:32:07 started undef mino Oracle nmx004.wwdc.numonyx.com Message Text : The Oracle session with the PID 1103 has a CPU time ... (6 Replies)
Discussion started by: vikas027
6 Replies

7. Shell Programming and Scripting

remove leading spaces from a line

Hi friends I need some help, I have a file which looks as follows TEMP 014637065 014637065 517502 517502 RTE 517502 517502 RTE AWATER_TEST 12325 23563 588323 2323 5656 32385 23235635 ANOTHER_TEST 12 5433 FTHH 5653 833 TEST 123 123 3235 5353 353 53 35 353 535 3 YTERS GJK JKLS ... (6 Replies)
Discussion started by: lijojoseph
6 Replies

8. Shell Programming and Scripting

adding spaces to a line

Is there any command to add spaces to a lline....say i need 50 spaces between the data like "aaabbbccc dddeeefff" or may be like this "aaaabbbbbbcccccdddddeeeffff " your help is appreciated. (4 Replies)
Discussion started by: mgirinath
4 Replies

9. Shell Programming and Scripting

To Trim spaces at the end of line

Hi Friends, Can any one help with this issue: How to trim spaces for each line at the end, Like I have a file in this format. EMP1 SMITH 46373 5 STREET HOWARD 74636 EMP2 JONES 5454 { these are spaces ........} EMP3 SMITH 46373 5 STREET HOWARD 74636 EMP4 JON 2554 { these are... (1 Reply)
Discussion started by: sbasetty
1 Replies

10. UNIX for Dummies Questions & Answers

line containig spaces

hi i have file having following contain 2~%%~2~%%~7~%%~1~%%~none~%%~XYZ_MOV_HDR_REC~%%~1~%%~17_REC_ID == "H" 2~%%~2~%%~8~%%~2~%%~none~%%~XYZ_MOV_EXCL~%%~2~%%~20_UPC<=99999999 see the last column of first line having spaces i have a script cat rec_iden_file for i_line in `cat... (2 Replies)
Discussion started by: mahabunta
2 Replies
Login or Register to Ask a Question