Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
I am having the following problem when executing this script. It currently looks as follows: Code: awk -F, -v dt="$(date "+%Y-%m-%d")" '{ OFS=FS; print "abc", dt, $1 }' foo.csv > ...
  1. #1
    Linux User
    Join Date
    Dec 2004
    Posts
    323

    Correcting bash script

    I am having the following problem when executing this script. It currently looks as follows:
    Code:
    awk -F, -v dt="$(date "+%Y-%m-%d")" '{ OFS=FS; print "abc", dt, $1 }' foo.csv > foo.txt
    The response is "Illegal variable name."

    The first few lines of the input file foo.csv look as follows:
    Code:
    SG70,B3B0M92,ANN8132R7036,25-Jun-08
    SG68,B3B2J97,ANN8132R6871,25-Jun-08
    ST71,B2Q4T68,ANN8132N4540,25-Jun-08
    SG67,B3B2J75,ANN8132R6798,24-Jun-08
    so it is a list of codes of which the codes in the first column are needed only. Prepended with the code "abc" and the date, the resulting file foo.txt should hold the following data:

    Code:
    "abc",2008-06-25,SG70
    "abc",2008-06-25,SG68
    "abc",2008-06-25,ST71
    "abc",2008-06-25,SG67
    How do I correct the code above and preferably still keep a one liner?

    Thanks in advance

  2. #2
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Code:
    awk -F"," '{print "abc" ",'$(date +%Y-%m-%d)'," $1}' foo.csv >foo.txt

  3. #3
    Linux User
    Join Date
    Dec 2004
    Posts
    323
    Thank you for your response. But why does it continue to say "Illegal variable name"?

  4. #4
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    post the output please
    Linux and me it's a love story

  5. #5
    Linux User
    Join Date
    Dec 2004
    Posts
    323
    Code:
    madrid# ls f*
    foo.csv
    madrid# awk -F"," '{print "abc" ",'$(date +%Y-%m-%d)'," $1}' foo.csv >foo.txt
    Illegal variable name.
    madrid#
    The machine is called madrid, there is only one foo-file before and after issuing the command.

  6. #6
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    this works flawlessly on my box.
    i just copied the command from your post then ran it with a dummy file i created for the purpose . and the result was as expected.
    Linux and me it's a love story

  7. #7
    Linux User
    Join Date
    Dec 2004
    Posts
    323
    Thanks for your response. So a good strategy would be the gradual addition of more of the components to this script until the culprit is found or could this be a permissioning issue?

  8. #8
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    i would create a small bash script containing only the maybe-faulty part then run it with
    Code:
    bash -x script_name
    this is the bash debug mode and you will see what is going wrong
    Linux and me it's a love story

  9. #9
    Linux User
    Join Date
    Dec 2004
    Posts
    323
    Thanks, that seems to work, because I have the foo.txt file now as designed. Can you explain however what is happening here:
    Code:
    madrid# bash -x foo.sh
    ++ date +%Y-%m-%d
    + awk -F, '{print "abc" ",2008-07-02," $1}' foo.csv
    The script has been named foo.sh and chmodded to +x. What do those plusses mean? Is it a problem that the first line of foo.sh is not #!/bin/bash or #!/bin/sh?

  10. #10
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    when you run it like
    Code:
    bash -option foo.sh
    you dont need to put the shebang(#!) because bash reads the file and executes the instructions found.

    as for the + they are output in debug mode
    Linux and me it's a love story

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...