I found this command in dockerfile like below.
echo -e 'skip-host-cache\nskip-name-resolve' | awk '{ print } $1 == "[mysqld]" && c == 0 { c = 1; system("cat") }' /etc/my.cnf > /tmp/my.cnf
I couldn't undersand it.
The awk program prints each line from the input file. If it finds the first line that starts with [mysqld], it uses the system command cat to print the text from the pipe.
Personally, I would use something like this:
awk -v add='skip-host-cache\nskip-name-resolve' \
'{ print } $1 == "[mysqld]" && found == 0 { print add; found = 1; }' my.cnf
Related
I would like to modify several shell variables within awk:
echo "$LINE_IN" | awk '/pattern1/ {print $0; WRITTEN=1; REC=$REC+1}' >> $FILE1
I tried to put eval, but still does not work:
eval $( echo "$LINE_IN" | awk '/pattern1/ {print $0; WRITTEN=1; REC=$REC+1}' >> $FILE1 )
Any suggestion?
I would like to use k-shell script, thanks!
Count the hits when you are finished:
echo "${LINE_IN}" | grep -E 'pattern1' > "${FILE1}"
REC=$(wc -l < "${FILE1}")
if (( REC > 0 )); then
WRITTEN=1
fi
When you really want to use awk, you must let awk write the results to stdout and parse stdout:
echo "${LINE_IN}" | awk '/echo/ {print $0 > "x3"; WRITTEN=1; REC++}
END { print "WRITTEN=" WRITTEN; print "REC=" REC}'
WRITTEN=1
REC=6
And when you want the variables really set, wrap it:
source (echo "${LINE_IN}" | awk '/echo/ {print $0 > "x3"; WRITTEN=1; REC++}
END { print "WRITTEN=" WRITTEN; print "REC=" REC}')
Note: Get used to using lowercase variable names like written, file and rec.
Can anyone help me on this
I have one variable which contain
var="DDP_COMPLIANCE ,ONT_COMPLIANCE_CD --,TECHNICIAN_EMPLOYEE_ID --,ENTRY_OPERATOR_EMPLOYEE_ID --,LAST_UPDATE_OPERATOR_EMPLOYEE_ID ,PARENT_TROUBLE_CALL_WORK_ORDER_ID --,abc_line ,PARENT_INSTALL_WORK_ORDER_ID ,TECH_COMPLIANCE_STARTBY_TS "
i want to remove all the "--,columnname" and print rest
and the output will be
DDP_COMPLIANCE ,ONT_COMPLIANCE_CD,PARENT_TROUBLE_CALL_WORK_ORDER_ID ,PARENT_INSTALL_WORK_ORDER_ID ,TECH_COMPLIANCE_STARTBY_TS
i am using Sed command as
echo $var | sed 's/--.*,/,/'
but got output as
DDP_COMPLIANCE ,ONTRAC_COMPLIANCE_CD ,TECH_COMPLIANCE_STARTBY_TS
which is incorrect
I'm sure there's a simple way, but for time being, this works for you?
echo $var | awk '{ for (i=1; i<=NF; i++) print $i }' | grep -v "^--" | awk 'BEGIN { ORS=" " }; { print $0 }'
the last awk part was to format the result back in a single line.
Result:
$ echo $var | awk '{ for (i=1; i<=NF; i++) print $i }' | grep -v "^--" | awk 'BEGIN { ORS=" " }; { print $0 }'
DDP_COMPLIANCE ,ONT_COMPLIANCE_CD ,PARENT_TROUBLE_CALL_WORK_ORDER_ID ,PARENT_INSTALL_WORK_ORDER_ID ,TECH_COMPLIANCE_STARTBY_TS
$ echo foooobazbarrrrr |
> gawk 'match($0, /(fo+).+(bar*)/, arr)
> {print arr[1], arr[2] }'
The output of this code should be foooo barrrr but on my Ubuntu, it is not working and failed.
If I wrote this code
> gawk 'match($0, /(fo+).+(bar*)/)
> {print }'
Then its working. Why is the first version not working?
Your command is slightly different from the example in the GNU manual. It has the opening { at the very start so that there's no pattern to match and the newline is required to separate the two awk commmands.
$ echo foooobazbarrrrr | gawk '{ match($0, /(fo+).+(bar*)/, arr)
> print arr[1], arr[2] }'
foooo barrrrr
Alternatively, you could use a semi-colon instead of a newline to separate the commands:
$ echo foooobazbarrrrr | gawk '{ match($0, /(fo+).+(bar*)/, arr); print arr[1], arr[2] }'
foooo barrrrr
Your version of the command will work if it’s entered as one line:
$ echo foooobazbarrrrr | gawk 'match($0, /(fo+).+(bar*)/, arr) {print arr[1], arr[2] }'
foooo barrrrr
When I run the below program, I get no return, however the program still runs forever until I end it. Can some one please exoplain to me why this would happen. I am trying to get this complex awk statement to work, however, have been very unsuccessful.
The code I am using for my Cshell is (its all on one line, but I split it here to make it easier to read):
awk '{split($2,b,""); counter = 1; while (counter < 13)
{if (b[counter] == 1 && "'$cmonth'" > counter)
{{printf("%s%s%s\n", $1, "'$letter'","'$year3'")}; counter++;
else if (b[counter] == 1 && "'$cmonth'" <= counter)
{{printf("%s%s%s\n", $1, "'$letter'","'$year2'")}; counter++;}
else echo "fail"}}' fileRead >> $year$month
The text file I am reading from looks like
fff 101010101010
yyy 100100100100
Here $year2 and $year3 represent counters that start from 1987 and go up 1 year for each line read.
$cmonth is just a month counter from 1–12.
$letter is just a ID.
The goal is for the program to read each line and print out the ID, month, and year if the position in the byte code is 1.
You have some mismatched curly braces, I have reformatted to one standard of indentation.
awk '{ \
split($2,b,""); counter = 1 \
while (counter < 13) { \
if (b[counter] == 1 && "'$cmonth'" > counter){ \
printf("%s%s%s\n", $1, "'$letter'","'$year3'") \
counter++ \
} \
else if (b[counter] == 1 && "'$cmonth'" <= counter) { \
printf("%s%s%s\n", $1, "'$letter'","'$year2'") \
counter++ \
} \
else print "fail" \
} # while \
}' fileRead >> $year$month
Also awk does'nt support echo.
Make sure that the \ is the LAST char on the line (no space or tab chars!!!), or you'll get a syntax error.
Else, you can 'fold' up all of the lines into one line. adding the occasional ';' as needed.
edit
OR you can take the previous version of this awk script (without the \ line continuation chars), put it in a file (without any of the elements outside of the ' ....' (single quotes) and call it from awk as a file. You'll also need to made so you can pass the variables cmonth, letter, year2 and any others that I've missed.
save as file
edit file, remove any `\' chars, change all vars like "'$letter'" to letter **
call program like
**
awk -v letter="$letter" -v year2="$year2" -v month="$month" -f myScript fileRead >> $year$month
**
for example
printf("%s%s%s\n", $1, "'$letter'","'$year2'")
becomes
printf("%s%s%s\n", $1, letter,year2)
IHTH.
My code gives me an error at line 3 with message:
syntax error near unexpected token ``ARRAY1=$(awk 'FNR == 1{print $2}' $file)
set -vx
for file in ls bowtie-0.12.7-win32/bowtie-0.12.7/Hits_635_25bp/*.txt
ARRAY1=$(awk 'FNR == 1{print $2}' $file) # stores the value
let ARRAY1=$ARRAY1/100+1
echo $ARRAY1
value1=$(awk 'FNR == '$ARRAY1'{print $1}' bowtie-0.12.7-win32/bowtie-0.12.7/list_for_635_fasta.list) # GETS THE VALUES FROM THE LIST
echo $value1
value2=$(awk 'FNR == 1{print $2,$3,$4,$5,$6,$7}' bwa-0.5.9/bwa-0.5.9/GENOMES/${file:0:13}) # GETS THE VALUES FROM THE LIST
echo $value2
done
I'd appreciate it if someone could point out the mistake. It seems to make sense, but I guess I'm missing something.
Try this:
set -vx
for file in bowtie-0.12.7-win32/bowtie-0.12.7/Hits_635_25bp/*.txt; do
ARRAY1=$( awk 'FNR == 1 { print $2 }' "$file" )
let ARRAY1=$ARRAY1/100+1
echo "$ARRAY1"
value1=$( awk -v a1="$ARRAY1" 'FNR == a1 { print $1 }' bowtie-0.12.7-win32/bowtie-0.12.7/list_for_635_fasta.list )
echo "$value1"
value2=$( awk 'FNR == 1 { print $2, $3, $4, $5, $6, $7 }' bwa-0.5.9/bwa-0.5.9/GENOMES/"${file:0:13}" )
echo "$value2"
done
If you tell us what shell you're using, we could improve the code.
for file in ls bowtie-0.12.7-win32/bowtie-0.12.7/Hits_635_25bp/*.txt
above line should be
for file in `ls bowtie-0.12.7-win32/bowtie-0.12.7/Hits_635_25bp/*.txt`
do