Issue
Using linux commands or bash, how can print the entries for column 6 as a single line when the corresponding entries for column 3 are the same value. e.g. in the sample file below, when column 3 has the value of 1 (rows 1 and 2) print '4 103', when column 3 takes the value of 2 (rows 3 and 4) print '113 4', and such on.
1 A 1 C 18 4
1 A 1 C 8 103
1 A 2 C 3 113
1 A 2 C 50 4
1 A 3 C 2 103
1 A 3 C 33 4
1 A 3 C 9 4
1 A 3 C 113 103
1 A 4 C 44 203
The expected output should be:
4 103
113 4
103 4 4 103
203
I have tried using awk for selecting the rows when column 3 takes a specific value (i.e. when column 3 is 1 only), but I cannot find a way to achieve this when column 3 can take any integer value. Thank you.
Solution
With any awk:
awk '
BEGIN{last=1}
$3 == last{printf "%d ", $6;last=$3;next}
{last=$3; printf "\n%d ", $6}
END{print ""}
' file
Output
4 103
113 4
103 4 4 103
203
Answered By - Gilles Quénot Answer Checked By - David Goodson (WPSolving Volunteer)