Return alias with pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Return alias with pattern
# 1  
Old 02-13-2015
Return alias with pattern

hello all,

i have ids repeated with alias names starting with different patterns like 'ab' , 'fg' etc...sometimes the same pattern names are repeated with the same ids like id=1 has names ab1 as well as ab3.

Lookup.txt

Code:
name id
ab1 1 
fg22 1
ab3 1
er2  1
fgh1 1
fg21 2
ab2 2
ab31 2
ab4 3
fgh4 3
fgh5 4
er3 4


Given a list of names

List.txt

Code:
fg22
fg21
er3

and a starting pattern , say 'ab' ... I would like to return the corresponding alias names (of the same id) , if available ..

I am looking for script, lookup.sh or awk or pl which when passed parameters

Code:
./script list.txt pattern

in this case

Code:
./lookup.sh list.txt ab

will return the pattern name , where available. In case where the id has multiple ab names, any one can be returned.

output.txt

Code:
ab1
ab2
er3


Last edited by jalaj841; 02-13-2015 at 03:12 AM..
# 2  
Old 02-13-2015
How big are your files?

What have you tried? Where are you stuck?
# 3  
Old 02-13-2015
Why would er3 show up in your output?
# 4  
Old 02-13-2015
Quote:
Originally Posted by RudiC
Why would er3 show up in your output?
Because there is no name in lookup.txt with the abbreviation ab (ab1, ab3, ab2, or ab4) that has the same id as the id for name er3 (4).

I have a working awk script for this problem, but I'm waiting for the submitter to explain what has been tried (to show that we aren't just being used as an unpaid programming staff).
# 5  
Old 02-13-2015
Wide range for interpretation, no?
Quote:
say 'ab' ... I would like to return the corresponding alias names (of the same id) , if available ..
I'd read this to suppress names not starting with "ab" ... but I'm no native speaker.
# 6  
Old 02-13-2015
Quote:
Originally Posted by RudiC
Wide range for interpretation, no? I'd read this to suppress names not starting with "ab" ... but I'm no native speaker.
Hi Rudi,
There is also the later statement in post #1:
Quote:
In case where the id has multiple ab names, any one can be returned.
that at least reduced the range of interpretations that fit the requirements. The way I interpreted it fit the output that was requested, but I agree that without the sample output I could interpret the requirements differently.

Anyway, i'm going to bed now; it is way past my bedtime...

- Don
# 7  
Old 02-13-2015
I`m sorry if I made this post ambiguous, reading through your replies I think Don understands the problem correctly.

Here is my attempt, which gives me a wrong output
Code:
  awk  '{if(a[$2]){a[$2]=a[$2]","$1} else { a[$2]=$1}} END {for (i in a) {print a[i]}}' lookup.txt > temp
  
  
  awk -F,  'FNR == NR {a[$0];next} {split($0,x,","); for (i=1;i<=length(x);i++) { if ($i~/^ab/) { $1=$i; break } } ; print $1}'  list.txt  temp

---------- Post updated at 10:51 AM ---------- Previous update was at 10:50 AM ----------

Quote:
Originally Posted by RudiC
Wide range for interpretation, no? I'd read this to suppress names not starting with "ab" ... but I'm no native speaker.
You are right, suppress the non "ab" names and return any of the "ab" names, where available.

---------- Post updated at 10:52 AM ---------- Previous update was at 10:51 AM ----------

Quote:
Originally Posted by Don Cragun
How big are your files?

What have you tried? Where are you stuck?
File is not that big, 120k records.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

2. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

3. Shell Programming and Scripting

Suppressing carriage return in bash alias

I'd like to create an alias that displays my string but leaves my cursor at the end. Not seeing any examples of this. One indirect way might be to preload or stuff the history buffer, so I just hit up arrow. (2 Replies)
Discussion started by: tns1
2 Replies

4. Shell Programming and Scripting

Grep command to return all the lines between one matched pattern to another.

14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. 14:50:00- abcdefghijkl. 123456789. 15:30:00-abcdefghijkl. (3 Replies)
Discussion started by: dev_shivv
3 Replies

5. Solaris

Grep command to return all the lines from one matched pattern to another.

For example a log file looks like below- 13:30:00- abcdefghijklhjghjghjhskj. abcdefghijkl. 14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. (0 Replies)
Discussion started by: dev_shivv
0 Replies

6. Shell Programming and Scripting

How to add a return after a special pattern?

Hello I'm new to Solaris I have a file have things like these PATTERN12345678 PATTERN12345678PATTERN87654321 PATTERN12345678 PATTERN87654321 i hope i could change to this format PATTERN12345678 PATTERN12345678 PATTERN87654321 PATTERN12345678 PATTERN87654321 (4 Replies)
Discussion started by: beterhans
4 Replies

7. UNIX for Dummies Questions & Answers

Search specific pattern in file and return number of occurence

Hi I want to search for a specific pattern in file Say ABC;HELLO_UNIX_WORLD;PQR ABC;HELLO_UNIX_WORLD_IS_NOT_ENOUGH;XYZ ABC;HELLO_UNIX_FORUM;LMN Pattern to search is : "HELLO_UNIX_*****" and not "HELLO_UNIX_***_***_" I mean after "HELLO_UNIX" there can only be one word.In this case... (2 Replies)
Discussion started by: dashing201
2 Replies

8. Shell Programming and Scripting

Perl help - Searching for a pattern and return the position

Hi, I need to search a file, in each line I need to check for occurance of '1' from a particular position through the next 32 bytes. If 1 is found, i need to return the position. Here is an example of the file and the output i need. Please help. I'm new to perl and unix. File: ... (1 Reply)
Discussion started by: gpaulose
1 Replies

9. UNIX for Dummies Questions & Answers

return previous line for pattern match

Hi, Need some idea on file processing, I have file like below, Processing al sources ... ...No value found : CHECK. Completed comparing all sources. Comparing schedulers... Processing al targets ... ...No value found : From above I need to extract the line where "No value... (4 Replies)
Discussion started by: braindrain
4 Replies

10. Shell Programming and Scripting

Help with pattern search and return

I would like to write a script which will read a file containing a list of filenames of the format as shown below : /usr/local/packages/runcmdlinetool /home/john.doe/sdfsdf/sdfsdfsd/sdfsdf/sdfsdfTemplates.xml /usr/local/bin/gtar... (4 Replies)
Discussion started by: inditopgun
4 Replies
Login or Register to Ask a Question