awk to find number in a field then print the line and the number


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to find number in a field then print the line and the number
# 1  
awk to find number in a field then print the line and the number

Hi

I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field.
The source file is pipe delimited and looks something like
Code:
1|net|ABC Letr1|1530|||
1|net|EXP_1040 ABC|1121|||
1|net|EXP_TG1224|1122|||
1|net|R_North|1123|||
1|net|RExp 123 456 X|1234|||

What I want as an output is:
Code:
1|net|ABC Letr1|1530|||1|
1|net|EXP_1040 ABC|1121|||1040|
1|net|EXP_TG1224|1122|||1224|
1|net|RExp 123 456 X|1234|||123 456|

i.e. field $7 has just the number from field $3
I got as far as matching where field 3 contains a number:
where the input file is called 'x'
Code:
cat x|gawk -F"|" '$3 ~ /[0-9]/'

but various attempts with substr have so far failed.
any help appreciated . .

thanks
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input and output in addition to code segments.

Last edited by Don Cragun; 07-31-2015 at 06:48 PM.. Reason: Add CODE tags for sample input and output segments.
# 2  
Hello Mudshark,

Following may help you in same.
Code:
awk -F"|" '{Q=$3;gsub(/[[:alpha:]]|[[:punct:]]/,X,Q);sub(/^[[:space:]]/,X,Q);sub(/[[:space:]]+$/,X,Q);$(NF)=Q;if(Q){print $0 OFS}}' OFS="|" Input_file

Output will be as follows.
Code:
1|net|ABC Letr1|1530|||1|
1|net|EXP_1040 ABC|1121|||1040|
1|net|EXP_TG1224|1122|||1224|
1|net|RExp 123 456 X|1234|||123 456|

EDIT: Adding a non-one liner solution for same.
Code:
 awk -F"|" '{
                Q=$3;
                gsub(/[[:alpha:]]|[[:punct:]]/,X,Q);
                sub(/^[[:space:]]/,X,Q);
                sub(/[[:space:]]+$/,X,Q);
                $(NF)=Q;
                                                        if(Q){
                                                                print $0 OFS
                                                             }
            }
          ' OFS="|" Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-31-2015 at 11:56 AM..
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 3  
Try also
Code:
awk -F\| '{$7=$3; $8=""; gsub(/^[^0-9]*|[^0-9]*$/, "",$7)} $7' OFS=\| file
1|net|ABC Letr1|1530|||1|
1|net|EXP_1040 ABC|1121|||1040|
1|net|EXP_TG1224|1122|||1224|
1|net|RExp 123 456 X|1234|||123 456|

This User Gave Thanks to RudiC For This Post:
# 4  
Ravinder, you worked around
Code:
gsub(/[^[:digit:]]/,X,Q)

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Quote:
Originally Posted by MadeInGermany
Ravinder, you worked around
Code:
gsub(/[^[:digit:]]/,X,Q)

This works with the English description of the desired output, but doesn't preserve the space between strings of digits as shown in the last line of the desired output. To get the desired output, one could use:
Code:
gsub(/[^[:digit:] ]/,X,Q)

if only <space> characters are to be allowed between strings of digits, or:
Code:
gsub(/[^[:digit:][:space:]]/,X,Q)

if any combinations of <space> and <tab> characters are to be allowed (as assumed by Ravinder's following sub() statements to get rid of leading and trailing space class characters.

Note that Ravinder's suggestion to use:
Code:
                sub(/^[[:space:]]/,X,Q);
                sub(/[[:space:]]+$/,X,Q);

after getting rid of alphabetic and punctuation class characters only removes one leading whitespace character (when it seems that there could be more than one). And, both of these could be replaced by a single gsub() call:
Code:
                gsub(/^[[:space:]]+|[[:space:]]+$/,X,Q);

or, if only space characters are to be allowed between digit strings:
Code:
                gsub(/^ +| +$/,X,Q);

These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Many thanks all for your help on this.
Suggestions both worked perfectly on my example data.

appreciate the help.

Thanks Don for the explanations.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #153
Difficulty: Easy
Novell sold their Unix business operations to Microsoft in 1995.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

2. Shell Programming and Scripting

Replace a field with line number in file

I am working on a script to convert bank data to a csv file. I have the format done - columns etc. The final piece of the puzzle is to change the second field (after the R) of every line to reflect its' line number in the file. I am stumped. I can use awk on each line but need help looping through... (9 Replies)
Discussion started by: Melah Gindi
9 Replies

3. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

4. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

5. Programming

Find and print number after string in C

I'm trying find and print a number after a specific user passed string in each line of a text file using C (as requested by the powers that be). I've pieced together enough to read the file, find the string and print the line it was found on but I’m not sure where to even start in terms of finding... (3 Replies)
Discussion started by: cgol
3 Replies

6. Shell Programming and Scripting

find the field number

######################## SOLVED ################## Hi I have a header file like the following and the field "IDENTIFIER" can be at any possition on this line, The line can containt a variable number of field, not alway the same depending of the header file i use ... (6 Replies)
Discussion started by: kykyboss023
6 Replies

7. Shell Programming and Scripting

awk script: print line number n of another file

Hi, I wrote an awk script to analyse file A. I call the script with files A and B. File A has lines like: 000000033100001 000000036100001 000000039100001 The first 9 characters are interpreted as a line number; for each line number found I want to output this line number of file B. ... (13 Replies)
Discussion started by: kpg
13 Replies

8. Shell Programming and Scripting

awk to print exact field number

Hello there. I want some help with awk I have this script that reads a file from awk and you can insert from keyboard any list from the fields that exist so to be printed on monitor echo "give a choice" read -a ans array=${ans} awk -F: -v k="$array" '{ ... (4 Replies)
Discussion started by: covis
4 Replies

9. Shell Programming and Scripting

fourth field is number in a line

hi i hav a file like 121212 asdd d 7 dfsdffdffsdfsdfsdfdf rrretrtrtre 121212 asdd d 5 dfsdffdffsdfsdfsdfdf rrretrtrtre 121212 asdd d 5 dfsdffdffsdfsdfsdfdf rrretrtrtre 121212 asdd d 4 dfsdffdffsdfsdfsdfdf rrretrtrtre 121212 asdd d 6 dfsdffdffsdfsdfsdfdf rrretrtrtre i need to... (4 Replies)
Discussion started by: Satyak
4 Replies

10. Shell Programming and Scripting

use awk to aggregrate the field number

Hi, I have a various files;each filled with hundreds of line with similar number of fields. I would like to extract out field $5 from each of this file and aggregate them before printing out to a file. I tried to :- #!/usr/bin/awk -f file="file1.txt file2.txt file3.txt file4.txt" ... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

Featured Tech Videos