Change the format of the strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change the format of the strings
# 1  
Old 11-26-2013
Change the format of the strings

I have Input file as below
Code:
1 a
1 b
1 c
2 d
2 e
2 f

I want below output as below.

Code:
1 a,b,c
2 c,d,e

Pls suggest how can i do it.
# 2  
Old 11-26-2013
Quote:
Originally Posted by millan
I have Input file as below
Code:
1 a
1 b
1 c
2 d
2 e
2 f

I want below output as below.

Code:
1 a,b,c
2 c,d,e

Pls suggest how can i do it.
if you expect 2 c,d,e following won't work ...

Try :


This doesn't care about order
Code:
$ cat <<eof | awk '{A[$1] = A[$1] ? A[$1] OFS $2 : $2 }END{for(i in A)print i FS A[i]}' OFS=\,
1 a
1 b
1 c
2 d
2 e
2 f
eof

1 a,b,c
2 d,e,f

whereas this cares about order with a assumption that file is sorted
Code:
$ cat <<eof | awk '{printf p == $1 ? NR == 1 ? $1 FS $2 : OFS $2 : RS $1 FS $2 }{p = $1}END{printf RS}' OFS=\,
1 a
1 b
1 c
2 d
2 e
2 f
eof

1 a,b,c
2 d,e,f


Last edited by Akshay Hegde; 11-26-2013 at 04:39 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change just the date format

need some more help please i have a large file and i want to change just the date format with awk or sed from this yyyy-mm-dd 2016-04-15 8.30 2016-04-15 7.30 2016-04-13 7.30 2016-04-11 8.30 2016-04-11 7.30 2016-04-08 8.30 2016-04-08 7.30 2016-04-06... (5 Replies)
Discussion started by: bob123
5 Replies

2. Programming

Reuse format strings

I have a collection of format strings for sscanf, such as "%02d%*1s%02d%*1s%02d" to read in certain formatted strings, such as dates, times, etc. I wonder if there is a way to use them in printf without some changes? The example above would not work - at least I can't think of any ways to... (4 Replies)
Discussion started by: migurus
4 Replies

3. Shell Programming and Scripting

Change name format

I am trying to use either awk or sed to make names like J. A. Smith and J.A. Martin Smith become JA Smith and JA Martin SmithThe code should concatenate abbreviated letters that have a dot after them. I have only been able to do this: echo $name | sed 's/\.\ //g'But that concatenates the... (3 Replies)
Discussion started by: locoroco
3 Replies

4. Shell Programming and Scripting

Format change

I wish to convert the following in shell. Help me SED or AWK ID= 12345,23456,67859 , 90225 , 67583 I can extract the right hand side of "=" using cut command "cut -d "=" -f2 after extracting that right side i would need like this '12345','23456','67859','90225','67583' 1 ) the... (2 Replies)
Discussion started by: ilugopal
2 Replies

5. Shell Programming and Scripting

format change

I have a text file sample.txt which contains some details like Order 9001 Item Code 34 Quantity 4 Entry Date 2009-04-23 Ordered by Ram Order 9002 Item Code 34 Quantity 3 (1 Reply)
Discussion started by: lazydev
1 Replies

6. Shell Programming and Scripting

Change Hex character strings to HTML entities

Hi! I am not a whiz at awk and very unsure about the aplication of awk solve my problem. I was hoping for some quick pointers so I can figure this out. I have a file that looks like so: label.Asked=\u8CEA\u554F\u6E08\u307F button.Edit=\u7DE8\u96C6... (3 Replies)
Discussion started by: pinnochio
3 Replies

7. Shell Programming and Scripting

extract strings from file and display in csv format

Hello All, I have a file whose data looks something like this I want to extract just the id, name and city fields in a csv format and sort them by id. Output should look like this. 1,psi,zzz 2,beta,pqr 3,theta,xyz 4,alpha,abc 5,gamma,jkl (12 Replies)
Discussion started by: grajp002
12 Replies

8. Shell Programming and Scripting

SCCS is messing up my date format strings

Does anybody know how to keep SCCS from changing the wrong module keywords? I'm thinking of a don't translate after this line kind of operation. I see where you can use get with a -k but then no keywords get translated. It's either all or none. I use simply # %A% # %G% %T% in all my... (1 Reply)
Discussion started by: Back-N-Black
1 Replies

9. Red Hat

RHEL 3 - how to change the SNMP community strings?

I have edited the snmpd.conf file on RHEL3, I changed: com2sec notConfigUser default public to : com2sec notConfigUser default new_string_name BUT, when my security guy scans the box, is still answers to public, I restarted snmpd. Is there more to this than just changing... (0 Replies)
Discussion started by: BG_JrAdmin
0 Replies

10. Shell Programming and Scripting

change the empty function from the old format to the new format

I have about 300 files which has the function getDBBackend(). How to write a program to change the empty function from the old format to the new format? Old empty function format are either: function getDBBackend() { // Not available } // getDBBackend or: function... (0 Replies)
Discussion started by: powah
0 Replies
Login or Register to Ask a Question