I have a script that outputs a column of text. I want to prepend each entry in the column with a string stored as a variable. How can I do this?  For example, consider the command  $ echo -e "john\npaul\ngeorge\nringo" john paul george ringo   Now, I'd like to prepend each entry in this column with the value of the environment variable $HOME. I've tried piping into awk like this:  $ echo -e "john\npaul\ngeorge\nringo" | awk '{printf("%s%s\n", $HOME, $1)}' johnjohn paulpaul georgegeorge ringoringo   This output is not what I'm looking for. What am I doing wrong?
37784
Possible Duplicate:          I have a script, in which a script snippet is  x=3 awk '$2=$x{print $1}' infile   the external variable is x,  but it prompts an error in awk  so in this situation,how to make awk run successfully?  thanks!
23359
I'll appreciate your help with the following issue:  I'm trying to set an array which contains a variable as part of the array name, example: Arr_$COUNTER (where $COUNTER is changed based on a loop count)  Every possible way I have tried came up with an error, such as "bad substitution" or "syntax error near unexpected token"  Here is the entire flow:   There is one file which contain multiple lines. each line has 6 values separated by space  10 20 30 40 50 60   100 200 300 400 500 600  The script, is meant to read each line from the file, and declare it as an array (with the line number which is the variable. as a test, each value should be printed and eventually another function will be executed on each value.  #!/bin/bash COUNTER=1 LINES=`wc -l VALUES_FILE.txt | awk '{print $1}'` echo "Total number of lines "$LINES echo while [ $COUNTER -le $LINES ] do echo "Counter value is $COUNTER" field=`awk "NR == $COUNTER" VALUES_FILE.txt` echo "Field = $field" declare -a "arr$COUNTER=($field)" echo "arr$COUNTER[0] = ${arr$COUNTER[0]}" echo "arr$COUNTER[1] = ${arr$COUNTER[1]}" echo "arr$COUNTER[2] = ${arr$COUNTER[2]}" echo "arr$COUNTER[3] = ${arr$COUNTER[3]}" echo "arr$COUNTER[4] = ${arr$COUNTER[4]}" echo "arr$COUNTER[5] = ${arr$COUNTER[5]}" let COUNTER=COUNTER+1 echo done echo "The End" echo    Here is the result:   Total number of lines 2  Counter value is 1 Field = 10 20 30 40 50 60 ./sort.sh: line 12: arr$COUNTER[0] = ${arr$COUNTER[0]}: bad substitution The End    What should be changed / fixed in order to have it working properly?  thank !
4318
I was wanting to initialize some strings at the top of my script with variables that have no yet been set, such as:  str1='I went to ${PLACE} and saw ${EVENT}' str2='If you do ${ACTION} you will ${RESULT}'   and then later on PLACE, EVENT, ACTION, and RESULT will be set. I want to then be able to print my strings out with the variables expanded. Is my only option eval? This seems to work:  eval "echo ${str1}"   is this standard? is there a better way to do this? It would be nice to not run eval considering the variables could be anything.
69962
I have an application which takes as an input attributes in double quotes embedded in single quotes. Take for example this right command:  command -p 'cluster="cl1"'   In order to automate it, I created a bash file using $CLUSTER as a variable. How should be my command? In other words, what should I put instead of cl1?  Please note that, If I modified the above command, it won't be accepted. For instance: command -p "cluster=cl1" is not accepted
35975
The variable BUILDNUMBER is set to value 230. I expect 230_ to be printed for the command echo $BUILDNUMBER_ but the output is empty as shown below.  # echo $BUILDNUMBER_  # echo $BUILDNUMBER 230
106954
When a script runs, commands in it may output some text to stdout/stderr. Bash itself may also output some text.  But if a few scripts are running at the same time, it is hard to identify where does an error come from.  So is it possible to insert a prefix to all output of the script? Something like:  #!/bin/bash prefix 'PREFIX' &amp;2 echo "wrong!" &gt;&amp;2   Then:  $ ./script.sh PREFIXwrong!
81486
I have a client that updates/writes into myfile.csv arbitrarily.  I've written the following code, I left the MySQL snippet out:  while true do   awk_variables_value_array=`tail -n2 myfile.csv | awk -F, '$7 == "status" {print $4, $5, $10 }'`   var1=${awk_variables_value_array[0]}   var2=${awk_variables_value_array[1]}   var3=${awk_variables_value_array[2]}    if[ "var3" -gt "0" ]   --MYSQL SNIPPET IS--   fi done  Q: tail -n2 reads the last 2 lines, how can I change it is so it's the second last line  the file:  where n is the last line of the file  line 1 line 2 line 3 line 4 .... line n-2 line n-1 line n  current output yields:  line n-1 line n   I would like it so that the output is:  line n-1
9871
I have the following file:   id  name  age 1   ed    50 2   joe   70      I want to print just the id and age columns. Right now I just use awk:   cat file.tsv | awk '{ print $1, $3 }'   However, this requires knowing the column numbers. Is there a way to do it where I can use the name of the column (specified on the first row), instead of the column number?
33752
I'm sure it is relatively simple, I just don't know how to do it.  #!/usr/bin/ksh set `iostat` myvar=6   I want to something like echo ${$myvar} which i want interpreted as ${$myvar} -> ${6} -> value
77730
Let's suppose I've declared the following variables:  $ var='$test' $ test="my string"   If I print their contents I see the following:  $ echo $var $test  $ echo $test my string   I'd like to find a way to print the content of the content of $var (which is the content of $test). So I tried to do the following:  $ echo $(echo $var) $test   But here the result is $test and not "my string"... Is it possible to print the content of the content of variables using bash?
109811
I have an awk script, new.awk: BEGIN { FS = OFS = &quot;,&quot; }  NR == 1 {     for (i = 1; i &lt;= NF; i++)         f[$i] = i }  NR &gt; 1 {     begSecs = mktime(gensub(/[&quot;:-]/, &quot; &quot;, &quot;g&quot;, $(f[&quot;DateTime&quot;])))     endSecs = begSecs + $(f[&quot;TotalDuration&quot;])     $(f[&quot;CallEndTime&quot;]) = strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, endSecs) }  { print }  I am calling this in shell awk new.awk sample.csv  ... but I can see the changes in the terminal. How to make the change in-place in the file, like when using sed -i?
33412
I run a command and pipe it through awk as below :  mycommand |  awk '{print $1,tolower($3),$4}  This gives me the following output:  fred.o: t .abcdefg  fred.o: u .rstuvwxy  fred.o: t .defghi  fred.o: t .jklmnop  fred.o: d abcdefg  fred.o: d zyxwvuts    As you can see the sometimes the third column contains a leading "." but not always.  How can I modify my awk command to get rid of the leading "." in the third column if it exists ?  eg I would like my output to look like this :   fred.o: t abcdefg   fred.o: u rstuvwxy   fred.o: t defghi   fred.o: t jklmnop   fred.o: d abcdefg   fred.o: d zyxwvuts
102481
Hi I want to prepend text to a file.  For example I want to add tasks to the beginning of a todo.txt file.  I am aware of echo 'task goes here' &gt;&gt; todo.txt but that adds the line to the end of the file (not what I want).
63360
I'm running Ubuntu 11.04. I use the terminal to start a bash session, and I want to add an environment variable:  $r@hajt:~$ env THEVAR=/example   But it's not working. It shows all the variables with THEVAR being the last one, but another call to env does not show THEVAR anymore- env | grep THEVAR returns nothing.  Similarly, scripts with export (export THEVAR=/example) or other variable assignments (THEVAR=/example) don't add the environment variable.  I know I'm doing something wrong, I know it should be something simple, but I just can't find what.  UPDATE: The real meaning of my question was this one:   (Anyway I'll choose the most voted answer and leave the edited title -that wasn't what I was asking)  env runs a program in a modified environment, then dismisses all the changes.
111951
The idea would be to use it as... a pipe in a command. For instance: say there's some kind of long path which has to be retyped again and again, followed by a pipe and a second program, i.e.  "directory1/directory2/direcotry3/file.dat | less -I "   I'd like that part to be stored in a variable, so it could be used like this:  r="directory1/directory2/direcotry3 \| less -I -p " $ cat path1/path2/$r &lt;searchterm&gt;   Instead, I get  cat: invalid option -- I Try `cat --help' for more information.   ... meaning the pipe clearly didn't work.
69705
Below is a complete copy of a demo I'm using to to figure out to get the sed command to get rid of the spaces in a persons name, and compress it down to not have spaces.  Once this is done, I want to assign it to the variable comp then I can re-use it later on in the script. Here I am just trying to echo it to the stdout so I can see it worked.  If I run the script and enter my name as Ronald McDonald the result I get returned is RonaldMcDonald} with that curly brace on the end of his name, or whatever I type in.  How do I get it to work, so that the result doesn't append the } to the back of the assigned text.  #!/bin/bash  function readName {     echo "Enter your full name:"     read fullName     clear }    # end readName  function cmprsName {     comp={ echo "$fullName" } | sed 's/ //g' }    # end cmprsName  function sayItNow {     echo $comp }    # end sayItNow  function allTogether {     readName     cmprsName     sayItNow }    #end allTogether  case $1 in         -h | --help )           allTogether                                 exit                                 ;;         * )                     echo "$0 -h"                                 exit 1 esac
51902
In Windows 7, when I start the Command prompt, is there any command to display the contents of an environment variable (such as the JAVA_HOME or PATH variables)?  I have tried with echo $PATH, echo PATH and $PATH but none of these work.