Hi ,
I want simple awk code to sort column1 elements and append its column 2 elements .
Inputs file content:
001 string4
002 string8
003 string12
002 string16
Expected output:
001 string4
002 string8 string16
003 string12
Hi ,
I want simple awk code to sort column1 elements and append its column 2 elements .
Inputs file content:
001 string4
002 string8
003 string12
002 string16
Expected output:
001 string4
002 string8 string16
003 string12
Welcome to the community @rsinha.
Firstly, please markdown code tags when posting data/code samples. I've edited your original post for now
Given the requirement of being "simple", any ideas/attempts of your own that youve tried already?
Should be relatevely "simple" and there plenty of examples around.
Please lets us know how we can help.
Hi,
I tried below option but it did not worked .Lookinf for suggestion .
awk 'BEGIN{FS=OFS="\t"}{unique[$1]=(unique[$2] FS $2); next}END{for (i in unique) print i,unique[i]}' ~/awk_test.tcl
The first suggestion is that you follow forum rules and correct your spelling errors.
The second suggestion is to follow the rules (again) and the moderator's request that you segment your code using Markdown.
Did you read our rules which you agreed to when you joined our great community?
These rules are not optional, they are mandatory:
To learn more about Markdown, check out this link:
Awesome. It needs a couple of tweaks..
Firstly, let's get the order correct ly.
Let's pipe the output of your awk into sort -k1,1.
Change the the sort options as needed.
See what comes out and we can take things from there.
I tried below and see this :.Now i want 002 to be printed only once and string16 and sring8 comes with it
cat ~/awk_test.tcl | sort -k1,1
001 string4
002 string16
002 string8
003 string12
@rsinha , going forward you need to take note of the advice/recommendations given wrt use of Markdown , if you continue to ignore that then your posts may be ignored/removed as it may appear that you cannot be bothered/fail to see the point ....
that said, you have made the effort to write a solution and as advised by @vgersh99 you are almost there.
I've given a potential solution to the 'awk', there may be other valid/'better' but take a look at this and see if it helps. Obviously it is for you to test,check,test ... any given code and provide feedback.
**NB:**in addition to using your input file I added a few entries to demonstrate the mechanics of the solution.
$ cat input.txt
001 string4
002 string8
003 string12
002 string3
005 string17
002 string16
006 string11
006 string1
006 string10
002 string99
$
$ awk '{ key[$1]=(key[$1] FS $2) } END{ for (k in key) print k,key[k] }' FS='\t' input.txt
002 string8 string3 string16 string99
003 string12
005 string17
006 string11 string1 string10
001 string4
$
the remaining operation of sorting is for you to complete.