-1

Code typically speaks better than thousand bytes. So did this demo code that does not obey my logic. Typically I trust compilers and programs, so I think I have a typo or something somewhere. Please, could you point it out to me?

copy & paste code below into mydemo.sh file, and run it using command sh ./mydemo.sh

in my ref system this prints out:

mytux:~# ./mydemo.sh
This should spit out:
{USER1NAME} {Firstname Surname} {user1name}
{USER2NAME} {Secondname Surname} {user2name}
{USER3NAME} {Thirdname Surname} {user3name}
---- but it spits out:
{USER1NAME} {somestring: user1name}, {user1name}
{USER2NAME} {somestring: user2name}, {user2name}
{USER3NAME} {somestring: user3name}, {user3name}
----
Why so and how to fix it?

and here's the code:

#!/bin/sh
echo "# Firstname Surname, Company">usernames.txt
echo "somestring: user1name">>usernames.txt
echo "# Secondname Surname, Company">>usernames.txt
echo "somestring: user2name">>usernames.txt
echo "# Thirdname Surname, Company">>usernames.txt
echo "somestring: user3name">>usernames.txt

echo "This should spit out:"
echo "{USER1NAME} {Firstname Surname}, {user1name}"
echo "{USER2NAME} {Secondname Surname}, {user2name}"
echo "{USER3NAME} {Thirdname Surname}, {user3name}"
echo "---- but it spits out:"
echo "{USER1NAME} {somestring: user1name}, {user1name}"
echo "{USER2NAME} {somestring: user2name}, {user2name}"
echo "{USER3NAME} {somestring: user3name}, {user3name}"
echo "---- See:"
cat usernames.txt|awk \
        'BEGIN { $mylink="";} \
        { \
                if(match($0,"^#")!=0) \
                { \
                        split($0, a, ","); \
                        $mylink=$a[1]; \
                } \
                else \
                { \
                        if(length($mylink)>0) \
                        { \
                                print "{" toupper($2) "} {" $mylink "}, {" $2 "}"; \
                        } \
                        $mylink=""; \
                } \
        }'
echo "----"
echo "Why so and how to fix it?"
  • 2
    what exactly is your question? – Lawrence Benson Sep 15 '15 at 13:08
  • 1
    You don't need all those backslashes in the `awk` script. – chepner Sep 15 '15 at 13:10
  • 1
    We can't tell what you want to do from a code sample that doesn't do what you want to do. Edit your question to clarify and provide testable sample input and expected output. First, though - you don't prepend a `$` to access awk variables, you just use the variable name, just like C and most other languages (e.g. use `mylink=a[1]`, not `$mylink=$a[1]`) so correct that first and then see if you still have a problem. Also - get rid of all those useless trailing semi-colons and backslashes and get the book Effective Awk Programming, 4th Edition, by Arnold Robbins. – Ed Morton Sep 15 '15 at 14:56

1 Answers1

1

Is this what you're trying to achieve? It would be easier to post the input text as is.

$ awk -F"[, ]" -v OFS="}, {" '
        /^#/{n=$2" "$3;next} 
            {print "{" toupper($2), n, $2"}"}
         ' usernames.txt

{USER1NAME}, {Firstname Surname}, {user1name}
{USER2NAME}, {Secondname Surname}, {user2name}
{USER3NAME}, {Thirdname Surname}, {user3name}
karakfa
  • 66,216
  • 7
  • 41
  • 56
  • Yes, this is what I wanted to create. My question though is still why above example does output this: `mytux:~# ./mydemo.sh This should spit out: {USER1NAME} {Firstname Surname} {user1name} {USER2NAME} {Secondname Surname} {user2name} {USER3NAME} {Thirdname Surname} {user3name} ---- but it spits out: {USER1NAME} {somestring: user1name}, {user1name} {USER2NAME} {somestring: user2name}, {user2name} {USER3NAME} {somestring: user3name}, {user3name} ---- Why so and how to fix it? ` – Jyrki Pesonen Sep 17 '15 at 10:48