awk length of digit and print at most right digit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk length of digit and print at most right digit
# 1  
Old 11-23-2011
awk length of digit and print at most right digit

Have columns with digits and strings like:

input.txt
Code:
3840 3841 3842 Dav Thun Tax
Cahn 146; Dav.
3855 3853 3861 3862 Dav Thun Tax
2780 Karl VI.,
3873 3872 3872  Dav Thun Tax
3894 3893 3897 3899 Dav Thun Tax
403; Thun 282.
3958 3959 3960  Dav Thun Tax
3972 3972 3972 3975 Dav Thun Tax
Rom. Dav. 145;
4006 4005 4007 Dav Thun Tax

output.txt
Code:
3842 Dav Thun Tax
Cahn 146; Dav.
3862 Dav Thun Tax
2780 Karl VI.,
3872  Dav Thun Tax
3899 Dav Thun Tax
403; Thun 282.
3960  Dav Thun Tax
3975 Dav Thun Tax
Rom. Dav. 145;
4007 Dav Thun Tax

Can anybody help me on the code

Last edited by pludi; 11-23-2011 at 08:02 AM..
# 2  
Old 11-23-2011
if you have gnu sed maybe u can try this
Code:
$ sed '1~2s/.* \([^ ]* [^ ]* [^ ]* [^ ]*\)/\1/'

This User Gave Thanks to ygemici For This Post:
# 3  
Old 11-23-2011
Quote:
Originally Posted by ygemici
if you have gnu sed maybe u can try this
Code:
$ sed '1~2s/.* \([^ ]* [^ ]* [^ ]* [^ ]*\)/\1/'

Thanks, though i use gawk on windows i can't use sed.

Last edited by sdf; 11-23-2011 at 08:24 AM..
# 4  
Old 11-23-2011
Hi ygemici,

Nice Smilie

Code:
$ sed '1~2s/.* \([^ ]* [^ ]* [^ ]* [^ ]*\)/\1/'

Can you please explain me how it works/run.
# 5  
Old 11-23-2011
Quote:
Originally Posted by ygemici
if you have gnu sed maybe u can try this
Code:
$ sed '1~2s/.* \([^ ]* [^ ]* [^ ]* [^ ]*\)/\1/'

OK got sed to run on gnu it erases a lot of strings. So i will want to run part on code and the rest i will do by hand. I came up with this code

Code:
awk '{if(length($1)==4 && $1=="[0-9]" && length($2)==4 && $2=="[0-9]"  && length($3)==4 && $3=="[0-9]" ) print $1,$2,$3}' input.txt to_correct_ouput.txt

The code won't work can anybody help on correcting.
# 6  
Old 11-23-2011
Quote:
Originally Posted by sdf
Thanks, though i use gawk on windows i can't use sed.
you can use sed on windows..Smilie
sed for Windows
# 7  
Old 11-23-2011
Quote:
Originally Posted by forroughuse
Can you please explain me how it works/run.
1~2s for line 1 and every 2nd line after that, substitute
/.* any number of any characters, followed by a space and
\([^ ]* [^ ]* [^ ]* [^ ]*\)(stored sub-pattern) any number of non-space characters followed by a space (*3), followed by any number of non-space characters (i.e. the last 4 fields in the line, space-separated).
/\1/ replace with the text that matched stored sub-pattern 1
This User Gave Thanks to CarloM For This Post:
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 print only the digit in between a line in UNIX?

Below is my content in a file. How to print only the number in between CURDEPTH( ) ?? Please help me 5724-H72 (C) Copyright IBM Corp. 1994, 2011. ALL RIGHTS RESERVED. Starting MQSC for queue manager 1 : dis ql(DUMMY.QUEUE) curdepth AMQ8409: Display Queue details. ... (1 Reply)
Discussion started by: prince1987
1 Replies

2. Shell Programming and Scripting

Print a number up to last significant digit after decimal point

I want to write/print a number through a shell script up to its last significant digit (LSD) after the decimal point. Say, x=10.00056000000000000 I want to print x as x=10.00056. Note that x can be any thing so I cannot know the position of the LSD always. Thanks. (16 Replies)
Discussion started by: hbar
16 Replies

3. Shell Programming and Scripting

convert two digit in to single digit...

Hi Guys. My Input: ABCD 12 00 KL ABCD 12 08 DL ABCD 12 10 KK ABCD 12 04 LL ABCD 13 00 LP ABCD 13 1O LS Output: ABCD 12 0 KL ABCD 12 8 DL ABCD 12 10 KK ABCD 12 4 LL ABCD 13 0 LP (2 Replies)
Discussion started by: pareshkp
2 Replies

4. Shell Programming and Scripting

awk Loop 4 digit fields

Can somebody help me on this script with a loop? gawk --re-interval '{if(($1 ~ /]{4}/) && ($2 ~ /]{4}/) && ($3 ~ /]{4}/)) print $0}' I would like to loop until NF and while $i does not match ($i ~ /]{4}/) anymore then print all matching $i and exit. (5 Replies)
Discussion started by: sdf
5 Replies

5. Shell Programming and Scripting

Awk numeric range match only one digit?

Hello, I have a text file with lines that look like this: 1974 12 27 -0.72743 -1.0169 2 1.25029 1974 12 28 -0.4958 -0.72926 2 0.881839 1974 12 29 -0.26331 -0.53426 2 0.595623 1974 12 30 7.71432E-02 -0.71887 3 0.723001 1974 12 31 0.187789 -1.07114 3 1.08748 1975 1 1 0.349933 -1.02217... (2 Replies)
Discussion started by: meridionaljet
2 Replies

6. Shell Programming and Scripting

Single digit date to double digit date.

I have a var storing date var=`date` Now the date is returned as Mon Feb 2 00:25:48 PST 2009 Is there any way to check the date field alone ("2" in above case) and if its a single digit then add a prefix 0 to it and store the result in same variable "var" My intention in above case is... (3 Replies)
Discussion started by: villain41
3 Replies

7. Shell Programming and Scripting

using awk make an field as 5 digit and display

using awk convert 3 rd fileld of file as 5 digit and then display changed file. like 1 2 23445 3452 3343 3 5 6 6 ================ o/p:- 1 2 23445 3452 03343 3 5 00006 6 (1 Reply)
Discussion started by: RahulJoshi
1 Replies

8. Shell Programming and Scripting

How to convert a 2 digit to 4 digit

Hi All, How can i convert a number 24 to 0024 In the same way how can i convert 123 to 0123? All this has to be done inside a script Thanks in advance JS (6 Replies)
Discussion started by: jisha
6 Replies

9. Shell Programming and Scripting

Replace one digit by two digit using sed

Folks, Is there a simple way to replace one digit by two digit using sed. Example, mydigit1918_2006_8_8_lag1.csv should be mydigit1918_2006_08_08_lag01.csv. I tried this way, but doesn't work. echo mydigit1989_2006_8_8_lag1.csv|sed 's/]/0]/' Thank you, (5 Replies)
Discussion started by: Jae
5 Replies

10. Shell Programming and Scripting

How to print all lines that has exactly 3 occurences of digit 4 using grep?

How to print all lines that has exactly 3 occurences of digit 4 using grep? I am able to find the pattern if it is for atleast 3 occurences but with exactly 3 occurences? Can anybody help me on this. (3 Replies)
Discussion started by: sivasai83
3 Replies
Login or Register to Ask a Question