Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

aligning the records in a file 1

Status
Not open for further replies.

scottpeter

Programmer
Sep 23, 2003
28
US
I have a file in the following format:

AAA000+0032432
BBB00+0023324
CCC000+0023442
ddd000+0032424
FFF00+0023444
AAA000+0032432
CCC000+0023442
ddd000+0032424

The file should have '+' sign as the 7th character of every line. But you can see the character has shifted left by one place in some lines.

How can I shift this '+' signs to the right where it is misaligned.

Probably I can replace '+' with '0+' whenever I find + as the 6th character. Can somebody help me with how to do this.

Thanks,
Scott
 
A starting point:
awk 'BEGIN{FS=OFS="+"}{$1=substr($1"00000",1,6);print}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I am still struggling to run this successfully.

Otherwise how can I do the following:
If I find '+' in the 6th position, then replace the '+' with a '0+'(The 0+ should be inserted at that position).

Thanks,
Scott
 
I am still struggling to run this successfully
What happens ? Any error message ? Unexpected behaviour ?
Try nawk instead of awk.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
The first solution does not give any error message. It is just shifting the records in a way I cannot determine whether the output file is right or no.

Input File
1230003459G314010610299 +00000000 C69NU00000000000 00000+0000000000000+0000000000000+00000000 000000000000N0000000000+
000000000000332+0000100001009+0000100001036+0000100000000+0000000000000+0000000000000+00000000 1 010610321 MC0195737900011 0000000347N0000
0000000000+00000000 20 NOS98
1230019414T758030970100 +00000000 A00NU00000000000 00000+0000000000000+0000000000000+00000000 0000000000000000000000+0
00000000000000+0000000000000+0000000000000+0000000000000+0000000000000+0000000000000+00000000 1 030972108 MC0188 00000 0309700000 00000
000000000+00000000 20 N0000

Output File generated after running the first script
1230003459G314010610299 00000+00000000 C69NU00000000000 00000+0000000000000+0000000000000+00000000 000000000000N000000
0000+000000000000332+0000100001009+0000100001036+0000100000000+0000000000000+0000000000000+00000000 1 010610321 MC0195737900011 0000000347
N00000000000000+00000000 20 NOS98
1230019414T758030970100 00000+00000000 A00NU00000000000 00000+0000000000000+0000000000000+00000000 0000000000000000000
000+000000000000000+0000000000000+0000000000000+0000000000000+0000000000000+0000000000000+00000000 1 030972108 MC0188 00000 0309700000
00000000000000+00000000 20 N0000

The actual records on the file look like above. Hence it is difficult to track the changes made by the first script.

Hence it would be easier if I replace the '+' at a particular(say 140th)with a '0+'.

I appreciate your help.

 
Your input file has nothing to do with your original post !
Could you please reformulate your EXACT problem ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
The real file I have is 316 bytes long and in the above format. The position I need to check for the '+' sign is the 140th position for every record.

When I posted the question originally I wanted to make it simple. Hence I created some records of my own that were 14 bytes long and said that I need to check the 6th character instead of the 140th position I need to check on the real file.

I am really sorry for all the confusion.

so what I really need is that in a big text file if I find a '+' on the 140th position in any record I need to replace it with a '0+'.

Thanks,
Scott
 
if I find a '+' on the 140th position in any record I need to replace it with a '0+'
Like this ?
awk '{x=$0;if(substr(x,140,1)=="+")x=substr($0,1,139)"0"substr($0,140);print x}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,

The new solution worked perfectly.

Thank you very much.

Sorry for all the confusion.

Scott

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top