Sort a the file & refine data column & row format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort a the file & refine data column & row format
# 8  
Old 09-08-2011
Final i got the Output

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell command to transpose Row & Column

Hi all, need a help with getting a one line command to do the following. i have an input file with rows of data containing credits for each office, the output should be one row for each office with all the credits in rows for that office, if its not there then it should say N/A. the credits are... (10 Replies)
Discussion started by: tech_frk
10 Replies

2. Shell Programming and Scripting

File Move & Sort by Name - Kick out Bad File Names & More

I have a dilemma, we have users who are copying files to "directory 1." These images have file names which include the year it was taken. I need to put together a script to do the following: Examine the file naming convention, ensuring it's the proper format (e.g. test-1983_filename-123.tif)... (8 Replies)
Discussion started by: Nvizn
8 Replies

3. Shell Programming and Scripting

awk - script help: column to row format of data allignment?

Experts Good day, I have the following data, file1 BRAAGRP1 A2X B2X C2X D2X BRBGRP12 A3X B3X Z10 D09 BRC1GRP2 LO01 (4 Replies)
Discussion started by: rveri
4 Replies

4. Shell Programming and Scripting

Sort data from column to row

Hi, I need somebody's help with sorting data with awk. I've got a file: 10 aaa 4584 12 bbb 6138 20 ccc 4417 21 ddd 7796 10 eee 7484 12 fff ... (5 Replies)
Discussion started by: killerbee
5 Replies

5. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

6. UNIX for Dummies Questions & Answers

How to remove duplicated based on longest row & largest value in a column

Hii i have a file with data as shown below. Here i need to remove duplicates of the rows in such a way that it just checks for 2,3,4,5 column for duplicates.When deleting duplicates,retain largest row i.e with many columns with values should be selected.Then it must remove duplicates such that by... (11 Replies)
Discussion started by: reva
11 Replies

7. Shell Programming and Scripting

replace & with & xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

8. Shell Programming and Scripting

sort the org_no & member_type column ascending

I have a FILE1.DAT with the following information 21111111110001343 000001004OLF-AA029100020091112 21111111110000060 000001004ODL-CH001000020091112 24444444440001416 000001045OLF-AA011800020091112 23333333330001695 000001039OLF-AA030600020091112 23333333330000111... (5 Replies)
Discussion started by: new2ksh
5 Replies

9. Shell Programming and Scripting

Format - Inventory Row data into Column - Awk - Nawk

Hi All, I have the following file that has computer data for various pcs in my network... Snap of the file is as follows ******************************************************************************* Serial 123456 Computer IP Address lo0:... (1 Reply)
Discussion started by: aavam
1 Replies

10. UNIX for Dummies Questions & Answers

Search for & edit rows & columns in data file and pipe

Dear unix gurus, I have a data file with header information about a subject and also 3 columns of n rows of data on various items he owns. The data file looks something like this: adam peter blah blah blah blah blah blah car 01 30 200 02 31 400 03 57 121 .. .. .. .. .. .. n y... (8 Replies)
Discussion started by: tintin72
8 Replies
Login or Register to Ask a Question