Bash detecting number of digits in line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash detecting number of digits in line
# 1  
Old 07-21-2015
Bash detecting number of digits in line

Hi I have a problem, I am attempting to write a bash script that goes through a file and can determine how many characters are at a set point in a line starting with QTY+113:100:PCE, If it detects 3 digits (number in bold) then pad it out with 12 zero's

If there are only two digits then pad it out with 13 zero's, also removing the + and : and adding the correct spacing if possible.

These numbers change, hence why I need it to detect how many characters

Code:
QTY+113:100:PCE

I need it to return
Code:
QTY   113000000000000100PCE

and if there are two numbers like
Code:
QTY+113:50:PCE

I need it to return
Code:
QTY   113000000000000050PCE

Any help would be greatly appreciated.

Last edited by firefox2k2; 07-21-2015 at 08:43 AM..
# 2  
Old 07-21-2015
Hello firefox2k2,

Could you please try following and let me know if this helps.
Code:
 echo "QTY+113:100:PCE" | awk -F":" '{if(length($2)==3||length($2)==2){sub(/\+/,X,$1);printf "%s%015d%s\n", $1, $2, $3}}'

Output will be as follows.
Code:
 QTY113000000000000100PCE

For input with 2 digits as per your input it will be as follows.
Code:
 echo "QTY+113:50:PCE" | awk -F":" '{if(length($2)==3||length($2)==2){sub(/\+/,X,$1);printf "%s%015d%s\n", $1, $2, $3}}'

Output will be as follows.
Code:
 QTY113000000000000050PCE


Thanks,
R. Singh
# 3  
Old 07-21-2015
Hi that works great, how do I include this line in the small script below so it makes the changes?

I am very new to this

Code:
for file in *.TEST
do
sed -i 's/DTM+/DTM07 /' $file
done

Thank you for the help
# 4  
Old 07-21-2015
Hello,

You haven't showed us the input file in first post of this thread, I have answered according to your question. Nevertheless you can try following. If following doesn't help you then kindly provide details as follows:

I- Your O.S name please.
II- Your input file how it looks an example with few data sample.
III- If above solutions are not working then show us errors which you are getting please.

Following code you can use may be helpful for you, also to appreciate anyone in this forum you can always push THANKS button present in below of any thread please.
Code:
 awk -F":" '{if(length($2)==3||length($2)==2){sub(/\+/,X,$1);printf "%s%015d%s\n", $1, $2, $3}}' chumma.txt

You can give one file as for above code for testing purposes, if All is Well with above code then you can give as you are giving in your sample code shown.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 07-21-2015
How is the important number generated? If it is in a shell script, you can use typeset -Z15 variable to automatically pad it, if that helps.

Can you tell us more about the overall process? There may be a better solution.


Robin
This User Gave Thanks to rbatte1 For This Post:
# 6  
Old 07-21-2015
Quote:
Originally Posted by rbatte1
How is the important number generated? If it is in a shell script, you can use typeset -Z15 variable to automatically pad it, if that helps.

Can you tell us more about the overall process? There may be a better solution.


Robin

Hi Robin, below is an example of the original text file I am starting with, the numbers are an order quantity, they can be anything from 0 to 100's:

Code:
DTM+64:20160601:102'
DTM+63:20160630:102'
QTY+113:50:PCE'
SCC+4'
DTM+64:20160701:102'
DTM+63:20160731:102'
QTY+113:50:PCE'
SCC+4'
DTM+64:20160801:102'
DTM+63:20160831:102'
QTY+113:0:PCE'
SCC+4'
DTM+64:20160901:102'
DTM+63:20160930:102'
QTY+113:50:PCE'
SCC+4'
DTM+64:20161001:102'
DTM+63:20161031:102'
QTY+113:0:PCE'
SCC+4'
DTM+64:20161101:102'
DTM+63:20161130:102'

I need it to look like this :

Code:
DTM07 64 20160601                           102
DTM07 63 20160630                           102
QTY   113000000000000050PCE4  
DTM07 64 20160701                           102
DTM07 63 20160731                           102
QTY   113000000000000050PCE4  
DTM07 64 20160801                           102
DTM07 63 20160831                           102
QTY   113000000000000000PCE4  
DTM07 64 20160901                           102
DTM07 63 20160930                           102
QTY   113000000000000050PCE4  
DTM07 64 20161001                           102
DTM07 63 20161031                           102
QTY   113000000000000000PCE4  
DTM07 64 20161101                           102
DTM07 63 20161130                           102

I can do a lot of it with sed and its easier because you can edit files in place with sed, not used awk much so a little lost

Thank you again for taking the time to help.
# 7  
Old 07-21-2015
So can I just clarify the rules:-
  • You only want DTM & QTY records
  • Input is delimited by + or :
  • Trailing ' is ignored
  • DTM records are output as DTM07 with space separated fields of length 5, 2, 8, 29 (right justified)
  • QTY records are output with fields of length 6 (left justified), 3, 15 (zero padded), 5 (left justified)
How would you like to have the code written? What are you most comfortable in? Options include awk, sed, perl, ksh script, C, others?

Think about what you would like to support in the future.


Sorry for so many questions,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find number of digits in a word

HI, Can you tell me how to find the number of digits in a word. $cat data.txt +123456ad 87645768 Output should be 6 8 (5 Replies)
Discussion started by: ashwin3086
5 Replies

2. Shell Programming and Scripting

awk changes to cut number of digits

HCPM1ONDB00014800011800000589009211201 L201307022013070228AUD 00000000031. 000965105800000000000000000000000 MOBITV KEYA ... (4 Replies)
Discussion started by: mirwasim
4 Replies

3. Shell Programming and Scripting

extracting Number variable and the following digits.

HI all, I have output of something like this: crab: ExitCodes Summary >>>>>>>>> 12 Jobs with Wrapper Exit Code : 50117 List of jobs: 1-12 See https:///twiki/something/ for Exit Code meaning crab: ExitCodes Summary >>>>>>>>> 5 Jobs with Wrapper Exit Code : 8001 List of... (20 Replies)
Discussion started by: emily
20 Replies

4. Shell Programming and Scripting

summing the digits of a binary nuMBER

please help me write a perl program to find the difference of 1 and zeros of a 6 digit binary number. eg If input is 111100 expected output +2 if input is 000011 expected output -2 input is 000111 expected output 0 (2 Replies)
Discussion started by: dll_fpga
2 Replies

5. UNIX for Dummies Questions & Answers

How to delete lines above a certin line number in bash shell

Hi, I have written a script that returns the line number of the pattern i want and i stored the line number in a variable.Now i want to delete all the lines in a file above this line number which is stored in a variable. i am using sed '1,$getlinenumberd' > file1.txt which is not working(wrog... (5 Replies)
Discussion started by: learninguser235
5 Replies

6. UNIX for Dummies Questions & Answers

How to delete lines above a certin line number in bash shell

Hi, I have written a script that returns the line number of the pattern i want and i stored the line number in a variable(getlinenumber).Now i want to delete all the lines in a file above this line number which is stored in a variable. i am using sed '1,$getlinenumberd' > file1.txt which is... (2 Replies)
Discussion started by: learninguser235
2 Replies

7. Shell Programming and Scripting

Detecting key combination in bash

I need to grab key combination from a bash script and store it in a variable. Is there any way to do this? Thanks in advance! (1 Reply)
Discussion started by: prism1
1 Replies

8. Shell Programming and Scripting

number of digits after decimal

Hi All, I have a file of decimal numbers, cat file1.txt 1.1382666907 1.2603107334 1.6118799297 24.4995857056 494.7632588468 560.7633734425 ..... I want to see the output as only 7 digits after decimal (5 Replies)
Discussion started by: senayasma
5 Replies

9. Shell Programming and Scripting

Printing the line number in bash script

Hi, I would like to know how do I print the line # in a script. My requirement is, I have a script which is about ~5000 lines long. If there are any errors happen I just exit. And I would like to add the line # of the script where the error happened. Thanks, (6 Replies)
Discussion started by: suryaemlinux
6 Replies

10. Shell Programming and Scripting

Count number of digits in a word

Hi all Can anybody suggest me, how to get the count of digits in a word I tried WORD=abcd1234 echo $WORD | grep -oE ] | wc -l 4 It works in bash command line, but not in scripts :mad: (12 Replies)
Discussion started by: ./hari.sh
12 Replies
Login or Register to Ask a Question