LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-28-2012, 08:19 PM   #1
lcvs
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Rep: Reputation: Disabled
find one field into another


Hi guys !

Using awk, I try to read a column and see if a string from a field in this column is present into a longer string from another field in the same column.
If it is the case, returns the sub-string length (RLENGTH) in a new field.
input:
Code:
111|abcd1234efgh|zzzzz
222|1234|yyyyyyyyyy
333|ij1234k|xxx
444|ui987654g|ww
I tried using match function:
Code:
awk 'BEGIN{FS=OFS="|"} {match(a[$2],$2); print $4 = RLENGTH}1' input
to obtain:
Code:
111|abcd1234efgh|zzzzz|8
222|1234|yyyyyyyyyy|4
333|ij1234k|xxx|5
444|ui987654g|ww|-1

But I don't manage to write a for loop to scan the entire column by taking every field one by one as a reference for match function. It returns $4 = "-1" for every record.

Last edited by lcvs; 07-03-2012 at 02:08 AM.
 
Old 06-28-2012, 08:35 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
I reckon you can extend this one liner to match first, then sort by length http://unstableme.blogspot.com.au/20...g-awk-and.html & only print last record
 
Old 06-28-2012, 08:45 PM   #3
lcvs
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Hi Chrism01, thanks for your help !

The problem is that it doesn't tell me if it found the shorter string within another field ($4 = -1 for all records).
 
Old 06-28-2012, 09:24 PM   #4
lcvs
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
edited first post

Last edited by lcvs; 06-28-2012 at 09:46 PM.
 
Old 06-30-2012, 06:50 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,106

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
not sure what you are trying to do (shouldnt your 3rd row = 6 ?).

anyways, this is my stab at it:
Code:
[schneidz@hyper ~]$ cat lcvs.txt 
111|abcd1234efgh|zzzzz
222|1234|yyyyyyyyyy
333|ij1234k|xxx
444|ui987654g|ww
[schneidz@hyper ~]$ awk 'BEGIN{FS=OFS="|"}  {print  index($2,"1234")}' lcvs.txt 
5
1
3
0
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I search a file to find if my variable belogs to a field thomai Linux - Newbie 2 05-19-2011 05:54 PM
[SOLVED] Find a field and print its companion jaymzter Programming 2 04-05-2010 05:05 PM
awk: how to find expression in a certain field? Micro420 Programming 5 08-08-2007 05:59 PM
Find text and replace another field jaysin_aus Linux - Server 9 07-26-2007 02:16 PM
Find a field number for a value in text value - BASH severian23 Programming 3 01-10-2007 09:49 AM


All times are GMT -5. The time now is 09:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration