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
# 8  
Old 07-21-2015
Hello firefox2k2,

Following may help you in same, let me know if you have any queries.
Code:
 awk -F":" -vs1="'" '{sub(/\+/," ",$1);sub(/DTM/,"DTM07",$1);sub(s1,X,$NF);if(length($NF)==2 || length($NF)==3 && $1 ~ /DTM07/){printf "%s %d %15d\n", $1, $2, $3}};{if($1 ~ /QTY/){sub(/\+/,":",$1);printf "%s%015s%s\n", $1, $2, $3}}' Input_file

Output will be as follows.
Code:
DTM07 64 20160601             102
DTM07 63 20160630             102
QTY 113000000000000050PCE
DTM07 64 20160701             102
DTM07 63 20160731             102
QTY 113000000000000050PCE
DTM07 64 20160801             102
DTM07 63 20160831             102
QTY 113000000000000000PCE
DTM07 64 20160901             102
DTM07 63 20160930             102
QTY 113000000000000050PCE
DTM07 64 20161001             102
DTM07 63 20161031             102
QTY 113000000000000000PCE
DTM07 64 20161101             102
DTM07 63 20161130             102

EDIT: Adding a non one-liner form for same.
Code:
 awk -F":" -vs1="'" '{
                        sub(/\+/," ",$1);
                        sub(/DTM/,"DTM07",$1);
                        sub(s1,X,$NF);
                        if(length($NF)==2 || length($NF)==3 && $1 ~ /DTM07/)
                    {
                        printf "%s %d %15d\n", $1, $2, $3
                    }
                    };
                    {
                        if($1 ~ /QTY/)
                    {
                        sub(/\+/,":",$1);
                        printf "%s%015s%s\n", $1, $2, $3
                    }
                    }
                   ' Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-21-2015 at 12:03 PM.. Reason: Added a non one-liner form for solution on same now
# 9  
Old 07-21-2015
Try also
Code:
awk -F: -vsq="\'" '

/^QTY/          {sub (/\+/, "   ")
                 getline X
                 $0 = $0 X
                 sub ("SCC\+", "")
                 OFS=""
                 $2 = sprintf ("%015d", $2)
                 OFS=" "
                }

/^DTM/          {sub (/\+/, "07 ", $1)
                 $NF = sprintf ("%28s%s", " ", $NF)
                }

                {gsub (sq, _)}
 
1
' file
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

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