0

I have "|" delimited file, with fields that represent:

firstname|secondname|Kernel|Version|Year

For example:

Linus|Torvald|VMLINUZ||1994
Jochen|Liedtke|L4|||
Apple||Darwin||2000

I want "\t" delimited results, with few conditionals. If any column is empty, it'll substitute with "-", but it gets complex in few cases, where any of first or second name is empty. I don't want it to happen.

Simply, concatenate first & second name, but if any of these columns empty, don't replace with "-" otherwise this condition applies on other empty fields.

I wrote following awk script, but it replaces empty first or second name with "-" too.

BEGIN {
FS="|";
OFS="\t"
}
{ for (i = 1; i <= NF; ++i) sub(/^$/, "-", $i) }
{
print $5, $4, $2" "$1
}

It gives this output:

1994    -   Torvald Linus
-   -   Liedtke Jochen
2000    -   - Apple

so the last line should be

2000    -   Apple

Final Output

1994    -   Torvald Linus
-   -   Liedtke Jochen
2000    -   Apple
0

1 Answer 1

3

This is probably what you're trying to do:

$ cat tst.awk
BEGIN { FS="|"; OFS="\t" }
{
    for (i=3; i<=NF; i++) {
        if ($i == "") {
            $i = "-"
        }
    }
    print $5, $4, ($2=="" ? "" : $2 " ") $1
}

$ awk -f tst.awk file
1994    -       Torvald Linus
-       -       Liedtke Jochen
2000    -       Apple

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.