Firstly i have used the perl command to modified the file.
And then the awk command
------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
cat file1.txt
field1 data "user1":
field2:"data-cde"
field3:"data-pqr"
field4:"data-mno"
field1 data "user1":
field2:"data-dcb"
field3:"data-mxz"
field4:"data-zul"
field1 data "user2":
field2:"data-cqz"
field3:"data-xoq"
field4:"data-pos"
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Used the perl command and modified the file1.txt data
perl -pi -e "s/field1 data /field1_data:g" file1.txt
so, now modified file1.txt
cat file1.txt
field1_data:"user1":
field2:"data-cde"
field3:"data-pqr"
field4:"data-mno"
field1_data:"user1":
field2:"data-dcb"
field3:"data-mxz"
field4:"data-zul"
field1_data:"user2":
field2:"data-cqz"
field3:"data-xoq"
field4:"data-pos"
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
then use the awk script to get sorted info
cat file1.txt | Sort_a_File.awk > Sorted_Info.txt
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
cat Sort_a_File.awk
set -x
function Extract_Arg(line) {
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# Pull the argument after the equal sign
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
n = split(line, a, ":")
sub(/;$/, "", a[2])
return a[2]
}
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# MAIN BODY
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
BEGIN {
tb = "\t" # handy tab
}
{
currentline = $0
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check for each data value that we want to save
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if (index(currentline, "field1_data:") > 0) {
col1 = Extract_Arg(currentline)
next
}
if (index(currentline, "field2:") > 0) {
col2 = Extract_Arg(currentline)
next
}
if (index(currentline, "field3:") > 0) {
col3 = Extract_Arg(currentline)
next
}
if (index(currentline, "field4:") > 0) {
col4 = Extract_Arg(currentline)
next
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Output the data line
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if (index(currentline, "field4") == 0) {
print col1 " " col2 " " col3 " " col4 " "
}
}
field4 {
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
cat Sorted_Info.txt
user1 data-cde data-pqr data-mno
user1 data-dcb data-mxz data-zul
user2 data-cqz data-xoq data-pos
above file does not contains the header...
you may append the header before sorting the data.
Hope this helps.
Regards
Chandrasekhar K