Convert row to column
Hi,
Not sure can I do this in awk.... I have the following text file where each field in their own row.. Device ID: SEP001121111111 IP address: 1.1.1.1 Platform: Cisco IP Phone 7970, Capabilities: Host Phone Two-port Mac Relay Device ID: SEP00115C222222 IP address: 2.2.2.2 Platform: Cisco IP Phone 7940, Capabilities: Host Phone Two-port Mac Relay Device ID: SEP000D29033333 IP address: 3.3.3.3 Platform: Cisco IP Phone 7960, Capabilities: Host Phone Two-port Mac Relay I want it to appear as a csv with 3 fields in each record.. Device ID: SEP001121111111;IP address: 1.1.1.1;Platform: Cisco IP Phone 7970, Capabilities: Host Phone Two-port Mac Relay Device ID: SEP00115C222222;IP address: 2.2.2.2;Platform: Cisco IP Phone 7940, Capabilities: Host Phone Two-port Mac Relay Device ID: SEP000D29033333;IP address: 3.3.3.3;Platform: Cisco IP Phone 7960, Capabilities: Host Phone Two-port Mac Relay How do I do this in awk? If can't, any other script that can do this? thanks in a million... |
Code:
#!/bin/bash |
If the fields do not contain semicolons, then
Code:
awk '(tolower($1) == "device") { if (FNR > 1) printf("\n%s", $0) ; else printf("%s", $0); next } { printf(";%s", $0) } END { printf("\n") }' To explain why and how it works, here it is in broken-down form: Code:
awk '(tolower($1) == "device") { The middle one prints a semicolon and the input line, without any newline. The final one is only run when all input lines have been processed. It adds the newline at end. You could also use tr and sed to get the exact same effect: Code:
( tr '\n' ';' A third approach would simply replace two out of three newlines with semicolons: Code:
awk '{ if (FNR % 3 == 0) printf("%s\n", $0) ; else printf("%s;", $0) }' |
Code:
$ split -l 3 file |
Quote:
Code:
Patch on top of coreutils-8.17 to add --print option If your comment was just idle banter, don't worry; I just hadn't looked at Coreutils for quite a while so I thought it was a perfect reason to download the latest sources and look at it. Edited to add: Ignore the patch above. It works, but existing --filter option can trivially achieve the same functionality: Code:
split --filter='printf "%s\n" "$FILE"; exec cat > "$FILE"' ... |
Quote:
|
Quote:
Daniel B. Martin |
Which version are you using: split --version I compiled 8.17 and there it is present and working nicely.
|
Quote:
Daniel B. Martin |
8.17 of coreutils.
|
danielbmartin, run split --version to see the version number. Coreutils-8.17 is the latest version (released in May). Reuti compiled it from sources, probably to test the patch I wrote above.
I can confirm the --filter option is available in Coreutils-8.13, split (GNU coreutils) 8.13. It is also listed in both the info and man pages for this version. |
Quote:
Code:
daniel@daniel-desktop:~$ split --version Daniel B. Martin |
Quote:
Code:
$ split -l 3 --filter="(cat; echo '--------') >> OUTFILE" INFILE |
And going back to the original question, there are no temporary files necessary any more:
Code:
$ split -l 3 --filter="paste -s -d ';' >> OUTFILE" INFILE |
Quote:
Distributions tend to keep the features at the time of the release, and just backport bug fixes to the packages. It is a good strategy, and keeps the dependencies between packages stable. Note that Lucid Lynx support ends in April 2013, so you'll have to upgrade anyway in the next year or so. |
All times are GMT -5. The time now is 08:58 AM. |