LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 09: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 03:08 AM.
 
Old 06-28-2012, 09:35 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,329

Rep: Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745
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, 09: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, 10: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 10:46 PM.
 
Old 06-30-2012, 07:50 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,309

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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 06:54 PM
[SOLVED] Find a field and print its companion jaymzter Programming 2 04-05-2010 06:05 PM
awk: how to find expression in a certain field? Micro420 Programming 5 08-08-2007 06:59 PM
Find text and replace another field jaysin_aus Linux - Server 9 07-26-2007 03:16 PM
Find a field number for a value in text value - BASH severian23 Programming 3 01-10-2007 10:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:34 PM.

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
Open Source Consulting | Domain Registration