Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-05-2012
Registered User
 
Join Date: Sep 2012
Location: Sacramento, CA
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
transposing lines to columns

Okay folks, here's a question. I tried searching but couldn't find exactly what I needed.

I have a text file (excerpt below). This text file is an extract I did from several hundred pages of datasheets using grep so I could look only at the site history for each site. The problem is that there is a single row for EACH site visit. I want to condense this list so that there is one line per site, and multiple instances (columns) of "site visits" for each site.

There are a total of 47 unique site names, and each site has between 3 and 13 instances of "history" (i.e. the number of times the site has been visited), and each "history has 3 items ("date," "condition," and "report by")

So I'd go from this:

Code:
HS1415  HISTORY     - Date     Condition        Report By
HS1415  HISTORY     - 1972     MONUMENTED       CADH
HS1415  HISTORY     - 1972     GOOD             NGS
HS1415  HISTORY     - 20040519 GOOD             CADT
HS1415  HISTORY     - 20110214 MARK_NOT_FOUND   BOR
HS1412  HISTORY     - Date     Condition        Report By
HS1412  HISTORY     - 1972     MONUMENTED       CADH
HS1412  HISTORY     - 1972     GOOD             NGS
HS1412  HISTORY     - 19881117 GOOD             NGS
HS1412  HISTORY     - 20040519 GOOD             CADT
HS1412  HISTORY     - 20110928 GOOD             ATKNA


To this:

Code:
HS1415 HISTORY     - Date     Condition        Report By HISTORY     - 1972     MONUMENTED       CADH HISTORY     - 1972     GOOD             NGS (etc...)
HS1412 HISTORY     - Date     Condition        Report By HISTORY     - 1972     MONUMENTED       CADH HISTORY     - 1972     GOOD             NGS (etc...)
(etc...)

I don't really care that "HISTORY" and a few other characters are repeated, that's easy to remove with awk. I just can't figure out how to transpose the lines of unique site names to multiple columns in a single row for each site name.

Thanks so much in advance

Last edited by Franklin52; 09-06-2012 at 03:20 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #2  
Old 09-05-2012
Registered User
 
Join Date: Oct 2011
Location: London, UK
Posts: 657
Thanks: 29
Thanked 136 Times in 129 Posts
You could do something like:

Code:
awk '
oldone && $1!=oldone {
   printf ("\n")
}
{
  printf ("%s", $0);
  oldone=$1;
}
END {
  printf ("\n");
}' inputfile

The Following User Says Thank You to CarloM For This Useful Post:
jbrandt1979 (09-05-2012)
Sponsored Links
    #3  
Old 09-05-2012
Registered User
 
Join Date: Sep 2012
Location: Sacramento, CA
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by CarloM View Post
You could do something like:

Code:
awk '
oldone && $1!=oldone {
   printf ("\n")
}
{
  printf ("%s", $0);
  oldone=$1;
}
END {
  printf ("\n");
}' inputfile

AWESOME!!

Thanks so much, that's exactly what I needed.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
transposing columns into rows ida1215 Shell Programming and Scripting 8 02-06-2012 10:05 AM
Transposing Repeated Rows to Columns. ravzter Shell Programming and Scripting 2 08-12-2011 07:08 PM
Transposing rows into columns prasperl Shell Programming and Scripting 3 02-08-2011 06:59 PM
Transposing columns with awk phoenix_nebula Shell Programming and Scripting 5 07-29-2010 04:56 PM
Rows to columns transposing and reformating. bluethunder Shell Programming and Scripting 27 12-18-2009 02:58 AM



All times are GMT -4. The time now is 08:55 PM.