awk to search similar strings and arrange in a specified pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to search similar strings and arrange in a specified pattern
# 8  
Old 02-18-2012
Quote:
Originally Posted by prashu_g
Code:
awk -F"; " '{for (i=1;i<=NF;i++) a[$i]=1}END{for (i in a) print i}' file

The above code does work but the output is not in the format that am looking for. The output is coming
Code:
Cena, John
Norris, Chuck
Carey, Jim
Sen, Tim
 Cena, John
 Lee, Bruce
Rock, Dwayne
 Burt, Terrence
 Carey, Jim
Lock, Jessey

but i want it like
Code:
Carey, Jim; Cena, John;Sen, Tim; Burt, Terrence; Lock, Jessey; Norris, Chuck; Lee, Bruce; Rock, Dwayne

Simple change to the awk you have working:

Code:
awk -F"; " '{for (i=1;i<=NF;i++) a[$i]=1}END{for (i in a) printf( "%s; ", i ); printf( "\n" ); }' file

This User Gave Thanks to agama For This Post:
# 9  
Old 02-18-2012
That works fine, but there are multiple names coming which i want to avoid.

Current output of the code:
Code:
Cena, John; Norris, Chuck; Carey, Jim; Sen, Tim;  Cena, John;  Lee, Bruce; Rock, Dwayne;  Burt, Terrence;  Carey, Jim; Lock, Jessey;

i want the names only to be printed once, i.e a name should not come more than once. Like:
Code:
Burt, Terrence; Carey, Jim; Cena, John; Lee, Bruce; Lock, Jessey; Norris, Chuck; Rock, Dwayne; Sen, Tim

Sorry to be bothering with this..

Thanks,
Prashu
# 10  
Old 02-18-2012
I just cut and pasted both the script and your list of names and this is the output I am getting:

Code:
Burt, Terrence; Lee, Bruce; Cena, John; Sen, Tim; Lock, Jessey; Carey, Jim; Rock, Dwayne; Norris, Chuck;

Which seems what you want.

What version of awk are you running (try the command awk --version to see), and are you running on Solaris? If you are using Solaris then try nawk rather than awk and see if that makes a difference.

And no bother at all!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Print strings that match pattern with awk

I have a file with many lines which contain strings like .. etc. But with no rule regarding field separators or anything else. I want to print ONLY THE STRING from each line , not the entire line !!! For example from the lines : Flow on service executed with success in . Performances... (5 Replies)
Discussion started by: black_fender
5 Replies

3. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

4. Shell Programming and Scripting

Recursive search on pattern between two strings

Objective: Recursively search all files under a directory for SQL statements that end with ";" Sample input: UPDATE table1 set col=val UPDATE table2 set cola=vala ,colb=valb; UPDATE table3 set col=val Expected output: UPDATE table2 set cola=vala ,colb=valb; (1 Reply)
Discussion started by: krishmaths
1 Replies

5. Shell Programming and Scripting

awk to search similar strings and add their values

Hi, I have a text file with the following content: monday,20 tuesday,10 wednesday,29 monday,10 friday,12 wednesday,14 monday,15 thursday,34 i want the following output: monday,45 tuesday,10 wednesday,43 friday,12 (3 Replies)
Discussion started by: prashu_g
3 Replies

6. Shell Programming and Scripting

awk search strings from array in textfile

I am wanting to take a list of strings and loop through a list of textfiles to find matches. Preferably with awk and parsing the search strings into an array. // Search_strings.txt tag string dummy stuff things // List of files to search in textfile1.txt textfile2.txt The... (6 Replies)
Discussion started by: sdf
6 Replies

7. Shell Programming and Scripting

awk how to search strings within a file from two different lines

Hi, i would really appreciate any help anyone can give with the following info. Thanks in advance. I need to run a search on a file that contains thousands of trades, each trade is added into the file in blocks of 25 lines. i know the search has to take place between a time stamp specified... (4 Replies)
Discussion started by: sp3arsy
4 Replies

8. Shell Programming and Scripting

Using Awk to Search Two Strings on One Line

If i wanted to search for two strings that are on lines in the log, how do I do it? The following code searches for just one string that is one one line. awk '/^/ {split($2,s,",");a=$1 FS s} /failure agaf@fafa/ {b=a} END{print b}' urfile What if I wanted to search for "failure agaf@fafa"... (3 Replies)
Discussion started by: SkySmart
3 Replies

9. Shell Programming and Scripting

Awk search for a element in the list of strings

Hi, how do I match a particular element in a list and replace it with blank? awk 'sub///' $FILE list="AL, AK, AZ, AR, CA, CO, CT, DE, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VT, VA, WA,... (2 Replies)
Discussion started by: grossgermany
2 Replies

10. Shell Programming and Scripting

awk search for Quoted strings (')

Hi All, I have files: 1. abc.sql 'This is a sample file for testing' This does not have quotations this also does not have quotations. and this 'has quotations'. here I need to list the hard coded strings 'This is a sample file for testing' and 'has quotations'. So i have... (13 Replies)
Discussion started by: kprattip
13 Replies
Login or Register to Ask a Question