awk : search last index in specific column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk : search last index in specific column
# 1  
Old 10-29-2013
awk : search last index in specific column

I am trying to search a given text in a file and find its last occurrence index. The task is to append the searched index in the same file but in a separate column. I am able to accomplish the task partially and looking for a solution.

Following is the detailed description:

names_file.txt
Code:
111|lkasjdfaaa|555|tarun trehan
111|aaa|55765|vikram batra
111|aaa|555|allzhere blog
65876111|aaa|555|allzhere android apps
111|hhhaaa|555|allzhere on facebook
111|aaa|555|contact updater utility
111|aaa|444555|help me

Assuming, i am trying to search last occurrence index of "n" in the name i.e. my last column of file. So, the output file should be as following where the last column is representing the last index of "n" in the name column:

Output File :
Code:
111|lkasjdfaaa|555|tarun trehan|12
65876111|aaa|555|allzhere android apps|11
111|hhhaaa|555|allzhere on facebook|11
111|aaa|555|contact updater utility|3

I tried the following command:

Code:
cat names_file.txt | awk -F"|" '{print $4}' | awk -F"n" 'NF>1{print $0"|"length($0) - length($NF)}'

It gives me the following results:
Code:
tarun trehan|12
allzhere android apps|11
allzhere on facebook|11
contact updater utility|3

How can i fetch the rest of the columns first awk and still scan the 4th column only. I want the desired output described above.

Last edited by vbe; 10-29-2013 at 11:28 AM.. Reason: code tags... also for data!
# 2  
Old 10-29-2013
Using awk match function:
Code:
awk -F\| '
        {
                for ( i = 1; i <= NF; i++ )
                {
                        n = match ( $i, /n[^n]*$/ )
                }
                if ( n )
                        print $0, n
        }
' OFS=\| names_file.txt

This User Gave Thanks to Yoda For This Post:
# 3  
Old 10-29-2013
Excellent Yoda, I am big fan of you. Could you please explain me the code with the
Code:
match

please.


Thanks,
R. Singh
# 4  
Old 10-29-2013
Syntax:
Code:
match(string, regexp)

The match function searches the string, for the longest, leftmost substring matched by the regexp. It returns the character position, or index

Since match function searches the leftmost, I used regexp /n[^n]*$/ to match the last character n

Refer: GNU awk string functions
# 5  
Old 10-29-2013
Yoda,

Thanks for your reply. Appreciate your inputs.
I have the following queries:

1. i am getting a syntax error around the
Code:
n = match ( $i, /n[^n]*$/ )

..may be i am not pasting the character correctly.

2. This code is scanning all variables and i would like to search only the 4th column. I think i can do that by adding a if clause for i==4.

3. I am onto text mining a huge file set i.e. 10 million records. Is line by line scanning a viable option ??
# 6  
Old 10-29-2013
1. i am getting a syntax error around the n = match ( $i, /n[^n]*$/ )..may be i am not pasting the character correctly.
Use nawk instead if you are using SunOS or Solaris

2. This code is scanning all variables and i would like to search only the 4th column. I think i can do that by adding a if clause for i==4.
Yes, remove the for loop and put $4 instead: n = match ( $4, /n[^n]*$/ )

3. I am onto text mining a huge file set i.e. 10 million records. Is line by line scanning a viable option ??
Yes.
# 7  
Old 10-29-2013
Quote:
Using awk match function:

Code:
awk -F\| ' { for ( i = 1; i <= NF; i++ ) { n = match ( $i, /n[^n]*$/ ) } if ( n ) print $0, n }' OFS=\| names_file.txt


Hi Yoda,

Sorry to bother you, I have tried searching the match expression given by you in code but was not able to under stand it could you please exaplain it.


Code:
n = match ( $i, /n[^n]*$/ )

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

2. What is on Your Mind?

Updated Forum Search Index Min Word Length to 2 Chars and Added Quick Search Bar

Today I changed the forum mysql database to permit 2 letter searches: ft_min_word_len=2 I rebuilt the mysql search indexes as well. Then, I added a "quick search bar" at the top of each page. I have tested this and two letter searches are working; but it's not perfect,... (1 Reply)
Discussion started by: Neo
1 Replies

3. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

4. Shell Programming and Scripting

Search Replace Specific Column using RegEx

Have Pipe Delimited File: > BRYAN BAKER|4/4/2015|518 VIRGINIA AVE|TEST > JOE BAXTER|3/30/2015|2233 MockingBird RD|ROW2On 3rd column where the address is located, I want to add a space after every numeric value - basically doing a "s//&\ / ": > BRYAN BAKER|4/4/2015|5 1 8 VIRGINIA AVE|TEST > JOE... (5 Replies)
Discussion started by: svn
5 Replies

5. Shell Programming and Scripting

awk Search Array Element Return Index

Can you search AWK array elements and return each index value for that element. For example an array named car would have index make and element engine. I want to return all makes with engine size 1.6. Array woulld look like this: BMW 1.6 BMW 2.0 BMW 2.5 AUDI 1.8 AUDI 1.6 ... (11 Replies)
Discussion started by: u20sr
11 Replies

6. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

7. UNIX for Dummies Questions & Answers

Average by specific column value, awk

Hi, I am searching for an awk-script that computes the mean values for the $2 column, but addicted to the values in the $1 column. It also should delete the unnecessary lines after computing... An example (for some reason I cant use the code tag button): cat list.txt 1 10 1 30 1 20... (2 Replies)
Discussion started by: bjoern456
2 Replies

8. Shell Programming and Scripting

awk uniq and longest string of a column as index

I met a challenge to filter ~70 millions of sequence rows and I want using awk with conditions: 1) longest string of each pattern in column 2, ignore any sub-string, as the index; 2) all the unique patterns after 1); 3) print the whole row; input: 1 ABCDEFGHI longest_sequence1 2 ABCDEFGH... (12 Replies)
Discussion started by: yifangt
12 Replies

9. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

10. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies
Login or Register to Ask a Question