I am new to c shell. I have a problem using the while loop, the error message is "directory stack not deep". Here is my while loop.
set i = 1
while ($i < =10)
echo $i
end
EDIT:
I solve the problem by removing the space between '<' and '='.
set i = 1
while ($i <=10)
echo $i
end
OP answered his own question: the less-than-or-equal operator is spelled <=, not < =.
Related
There's a weird thing with awk conditional statements:
when running awk 'if-else if-else' with a single operation after each condition, it works fine as below:
awk 'BEGIN {a=30; \
if (a==10) print "a = 10"; \
else if (a == 20) print "a = 20"; \
else print "a = 30"}'
output:
a = 30
However, when running awk 'if-else if-else' with multiple operations (properly braced) after 'else if' , syntax error occured:
awk 'BEGIN {a=30; \
if (a==10) print "a = 10"; \
else if (a == 20) {print "a = 20"; print "b = 20"}; \
else print "a = 30"}'
output:
awk: cmd. line:4: else print "a = 30"}
awk: cmd. line:4: ^ syntax error
Can anyone tell if this is an awk issue that intrinsically doesn't allow multiple operations in such cases, or if it's just my syntax error that could be corrected?
P.S. I looked through all relevant posts of awk 'if else' syntax error, but none of them is addressing this issue.
Removed semi-colon at end of third line after close brace.
awk 'BEGIN {a=30; \
if (a==10) print "a = 10"; \
else if (a == 20) {print "a = 20"; print "b = 20"} \
else print "a = 30"}'
Output: a = 30
I have the following input file
system info com1 command set
system info com2 command set
system command set1 information description test
system command 21-22 information description pass
system command T1-T2-T3 information description file
system command commonset information description info
and the following command
awk '/system command/&&/information description/ { gsub("\"","") ; print ++ OFS") Command = " $3}' inputfile.txt
gives me the following output
1) Command = set1
2) Command = 21-22
3) Command = T1-T2-T3
4) Command = commonset
Is there a way that my commands list count is not with simple numbers but with sort ordinal numbers
and to have an output like this
1st) Command = set1
2nd) Command = 21-22
3rd) Command = T1-T2-T3
4th) Command = commonset
There is no built function to get that ordinal. We need to get our hands dirty and write this code:
awk 'function ordinal(i, mod, str) {mod = i%10; str=i; if (i~/1[1-3]$/) str=str "th"; else if (mod==1) str=str "st"; else if (mod==2) str=str "nd"; else if (mod==3) str=str "rd"; else str=str "th"; return str;} /system command/&&/information description/ { gsub(/"/,"") ; print ordinal(++i) ") Command = " $3}' file
1st) Command = set1
2nd) Command = 21-22
3rd) Command = T1-T2-T3
4th) Command = commonset
Expanded form:
awk '
function ordinal(i, mod, str) {
mod = i%10
str = i
if (i~/1[1-3]$/) # for numbers ending in 11, 12, 13
str = str "th"
else if (mod==1)
str = str "st"
else if (mod==2)
str = str "nd"
else if (mod==3)
str = str "rd"
else
str = str "th"
return str
}
/system command/&&/information description/ {
gsub(/"/,"")
print ordinal(++i) ") Command = " $3
}' file
An alternative way to implement the above function would be:
function ordinal(num, idx, sfxs) {
split("st nd rd th",sfxs," ")
idx = ( (num ~ /[123]$/) && (num !~ /1[123]$/) ? num % 10 : 4 )
return num sfxs[idx]
}
I have an issue with our golang programs when querying sql statement as below.
"database/sql"
sqlStatement := `
UPDATE user_posts SET
content = $2,
post_image = CASE WHEN ($3 IS NULL) THEN post_image ELSE $3 END,
updated_at = NOW()
WHERE id = $1
`
Found the answers parameter $3 cannot compare with clause "IS NULL" fixed as below.
sqlStatement := `
UPDATE user_posts SET
content = $2,
post_image = CASE WHEN $3 != '' THEN $3 ELSE post_image END,
updated_at = NOW()
WHERE id = $1
`
I'm having a problem when i call a variable defined in gnuplot while using sed:
pi.plt
N= 10000
set term gif animate delay 80
set output "pi.gif"
j = 1
load 'pi2.plt'
pi2.pĺt
k = ` sed -n "$j p" pi.dat | cut -f3 -d ' ' `
set label 1 sprintf('Pi = %f', k) at graph 0.85, 0.85
set parametric
plot fx(t), fy(t), "pi.dat" every ::::j using 1:2 with points
j = j + 100
if (j < N+1) reread
The variable j, although is defined in gnuplot, is not recognized by sed and i keep getting the error "invalid command".
Can anyone help me solving this issue? Thanks in advance!
Try:
k = real(system(sprintf('sed -n "%d p" pi.dat | cut -f3 -d " "', j)))
I have a sample file like this:
probeset_id submitted_id chr snp_pos alleleA alleleB 562_201 562_202 562_203 562_204 562_205 562_206 562_207 562_208 562_209 562_210
AX-75448119 Chr1_41908741 1 41908741 T C 0 -1 0 -1 0 0 0 0 0 -1
AX-75448118 Chr1_41908545 1 41908545 T A 2 -1 2 2 2 -1 -1 2 2 0
AX-75448118 Chr1_41908545 1 41908545 T A 1 2 -1 2 2 -1 2 -1 2 0
and I want to exclud the lines that have a sum of negative number equal or less than -3, I know how to calculate the sum of negative number and print it, with this code:
awk 'BEGIN{sum=0} NR >=2 {for (i=7;i<=NF;i++) if ($i ~ /^-/) sum += $i; print $1,$2,$3,$4,$5,$6,sum; sum=0}' test.txt > out.txt
But I don't want to do this I just want to calculate the sum of negative number and then select the lines that have less or equal to -3.
These are the commands that I wrote:
awk 'BEGIN{sum=0} NR >=2 {for (i=7;i<=NF;i++) if ($i ~ /^-/) sum += $i; sum=0}' test.txt | awk 'sum <= -3' > out.txt
I get no errors but the out.txt file is empty!
awk 'BEGIN{sum=0} NR >=2 {for (i=7;i<=NF;i++) if ($i ~ /^-/) sum += $i; if sum >= -3 pritn R; sum=0}' test.txt | wc -l
which I get:
^ syntax error
and how can I make sure that the first line(header) would be also in my output file?
so I would like to have this out put:
probeset_id submitted_id chr snp_pos alleleA alleleB 562_201 562_202 562_203 562_204 562_205 562_206 562_207 562_208 562_209 562_210
AX-75448119 Chr1_41908741 1 41908741 T C 0 -1 0 -1 0 0 0 0 0 -1
AX-75448118 Chr1_41908545 1 41908545 T A 2 -1 2 2 2 -1 -1 2 2 0
Try this:
awk '
NR == 1 {
print
next
}
{
negsum=0
for(i=7; i<=NF; i++) {
if ($i<0) {
negsum += $i
}
}
negsum <= -3'
Your first try fails because you use two different invocations of awk. These are two different programs being run, and the second knows nothing about the sum variable in the first, so it uses the default value sum = 0.
The second try just has a mis-spelling. You used pritn instead of print.
What you described can be easier to code with proper formatting. (Not that I'd always resort to using editor when scripting awk...)
The first condition (NR == 1) just ensures we print the first line as is.
awk '
NR == 1 { print }
NR >= 2 {
sum = 0;
for (i=7;i<=NF;i++) {
if ($i < 0)
sum += $i;
}
if (sum <= -3)
print;
}
' test.txt > out.txt