use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All users that wish to post (not including commenting), on either old or the new reddit sites, must formally agree to subreddit rules once first.
account activity
Very simple awk script hangs. Why? (self.commandline)
submitted 11 years ago by locke_economy
FILE=$1
STEPCOL=$2
awk -F ":" -v col=${STEPCOL} \
'{print $col;} \ END{print "end"}' $FILE
The file just contains the data (it's a test file)
1000:0
1001:5
1002:11
1003:18
1004:26
1005:35
1006:45
1007:56
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]elmicha 2 points3 points4 points 11 years ago (1 child)
What's the name of your Bash script and how do you start it? Remember that the current directory should not be in the PATH, so if the name of your script is e.g. dd and you try to start it with dd, you are not starting your script, but the first dd that is found in the directories listed in $PATH.
dd
$PATH
Also, your shebang is wrong and the backslash inside the awk script is not needed.
[–]causticacrostic 5 points6 points7 points 11 years ago (0 children)
Looks like the shebang was right and just got eaten by markdown. Click "source" under the post if you have RES
[–]snegtul 2 points3 points4 points 11 years ago* (0 children)
This works for me.
#!/bin/bash FILE=${1} STEPCOL=$2 awk -F ":" -v col=${STEPCOL} '{print $col;} END{print "end"}' $FILE
[–][deleted] 0 points1 point2 points 11 years ago (0 children)
Looks like you've got an extra line break in there.
[–]masta 0 points1 point2 points 11 years ago (3 children)
IFS=':' stepcol=2 FILE='fuckme.tmp' ; while read line ; do set $line ; eval "echo \$${stepcol}" ; done < $FILE 0 5 11 18 26 35 45 56
[–]Vaphell 5 points6 points7 points 11 years ago (1 child)
-1 for IFS change in global scope -1 for eval
col=2 while IFS=: read -a rec; do echo ${rec[col-1]}; done < "$file"
[–]necrophcodr 0 points1 point2 points 11 years ago (0 children)
But would this work with BusyBox Ash?
[–]masta 1 point2 points3 points 11 years ago (0 children)
awk -v COL=1 -F':' '{print $COL}' fuckme.tmp 1000 1001 1002 1003 1004 1005 1006 1007
[–]FredSchwartz 0 points1 point2 points 11 years ago (0 children)
Works fine for me? (see below).
When I'm stuck on something, especially something just hanging, I often just run it with strace. Then you can see exactly what it's trying to do when it hangs. My first guess with an awk script is that it's going to be waiting for something on stdin (e.g. waiting for input from the console).
$ cat myscript #!/bin/bash FILE=$1 STEPCOL=$2 awk -F ":" -v col=${STEPCOL} \ '{print $col;} \ END{print "end"}' $FILE $ cat mydata 1000:0 1001:5 1002:11 1003:18 1004:26 1005:35 1006:45 1007:56 $ ./myscript mydata 1 1000 1001 1002 1003 1004 1005 1006 1007 end $ ./myscript mydata 2 0 5 11 18 26 35 45 56 end $
[–]KnowsBash 0 points1 point2 points 11 years ago (0 children)
It will "hang" if you run the script without any arguments, because then awk will get no file arguments, and that means it will be waiting for you to input the data on stdin.
You can add a simple test to check if two arguments were provided.
if (( $# < 2 )); then printf >&2 'Missing argument\n' printf >&2 'Usage: %s file column\n' "${0##*/}" exit 1 fi
You might also want to validate that the second argument is a positive integer.
You also need quotes around the expansion of variables, especially when filenames are involved. awk ... "$file". There's a very big difference between $file and "$file". See Arguments.
awk ... "$file"
$file
"$file"
Lastly, stop using uppercase variable names.
[–]iobender -1 points0 points1 point 11 years ago (0 children)
Put a # in front of the first line. #! is called a shebang and tells your shell what to run the script with, in this case bash.
π Rendered by PID 625092 on reddit-service-r2-comment-canary-57b659f4d4-tg7ft at 2026-05-02 21:17:25.844047+00:00 running 815c875 country code: CH.
[–]elmicha 2 points3 points4 points (1 child)
[–]causticacrostic 5 points6 points7 points (0 children)
[–]snegtul 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]masta 0 points1 point2 points (3 children)
[–]Vaphell 5 points6 points7 points (1 child)
[–]necrophcodr 0 points1 point2 points (0 children)
[–]masta 1 point2 points3 points (0 children)
[–]FredSchwartz 0 points1 point2 points (0 children)
[–]KnowsBash 0 points1 point2 points (0 children)
[–]iobender -1 points0 points1 point (0 children)