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
# 1  
Old 02-18-2012
Java awk to search similar strings and arrange in a specified pattern

Hi,

I'm running a DB query which returns names of people and writes it in a text file as shown below:

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

I want to use awk and get all the names (excluding multiple entries) and write it in a separate text file in the pattern shown below:

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

Please advice how this can be done.. Also, can this be done in perl?

P.S- I'm working on ksh.

Thanks & Regards,
Prashu
# 2  
Old 02-18-2012
Code:
awk -F ';'  '{for(i=1; i<=NR; i++) {arr[$i]++}
                END{for(i in arr){printf "%s;", i)} {print ""} }' oldfile > newfile

# 3  
Old 02-18-2012
Code:
awk -F"; " '{for (i=1;i<=NF;i++) a[$i]=1}END{for (i in a) print i}' file

# 4  
Old 02-18-2012
awk -F"; " '{for (i=1;i<=NF;i++) a[$i]=1}END{for (i in a) print i}' file
This is throwing an errror:

Code:
awk: syntax error near line 1
awk: bailing out near line 1

---------- Post updated at 07:19 PM ---------- Previous update was at 07:15 PM ----------

Sorry.. This was the code thrwoing the error:
Code:
awk -F ';'  '{for(i=1; i<=NR; i++) {arr[$i]++}
                END{for(i in arr){printf "%s;", i)} {print ""} }' oldfile > newfile

And

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

# 5  
Old 02-18-2012
Code:
sed -e 's#; #\n#g' infile | sort -u | awk '{printf $0"; "}' | sed 's/; $//'

# 6  
Old 02-18-2012
does not return anything.. Smilie
# 7  
Old 02-18-2012
Quote:
Originally Posted by prashu_g
does not return anything.. Smilie
Code:
[root@node2 ~]# cat data 
Carey, Jim; Cena, John
Cena, John
Sen, Tim; Burt, Terrence
Lock, Jessey; Carey, Jim
Norris, Chuck; Lee, Bruce
Rock, Dwayne; Lee, Bruce
[root@node2 ~]# sed -e 's#; #\n#g' data | sort -u | awk '{printf $0"; "}' | sed 's/; $//'
Burt, Terrence; Carey, Jim; Cena, John; Lee, Bruce; Lock, Jessey; Norris, Chuck; Rock, Dwayne; Sen, Tim

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