Hi All,
How can i convert a number 24 to 0024
In the same way how can i convert 123 to 0123?
All this has to be done inside a script
Thanks in advance
JS
Hi All,
How can i convert a number 24 to 0024
In the same way how can i convert 123 to 0123?
All this has to be done inside a script
Thanks in advance
JS
If you have printf, that's the ticket.
vnix$ printf '%04i\n' 24
0024
If you don't have it in the shell, try awk.
echo 24 | awk '{ printf "%04i\n", $0 }'
if you would like the transformed value inside the same variable then you could use
one=1
one=`printf "%04i" $one`
$ typeset -R4Z tmp
$ tmp=23
$ echo $tmp
0023
$
Hi All,
I have a file in teh format given below:
catanimal 0 animal 90 number90 cat_number_name
catanimal 1 animal 91 number91 cat_animal_name
catanimal 2 animal 92 number92 cat_name_animal
kiwiani 0 bird 90 number90 kiwi_number_name
kiwiani 1 bird 91 number91 kiwi_animal_name
kiwiani 2 bird 92 number92 kiwi_name_animal
cat 0 animal 90 number90 cat_number_name
cat 1 animal 91 number91 cat_animal_name
cat 2 animal 92 number92 cat_name_animal
bat 0 animal 90 number90 bat_number_name
bat 1 animal 91 number91 bat_animal_name
bat 2 animal 92 number92 bat_name_animal
When i use awk '$1 ~ /bat/ {if ($2==0){print $5} } ' file , i get the
result i want
But if i give awk '$1 ~ /cat/ {if ($2==0){print $5} } ' file, I get
result for $1=cat and $1=catanimal where as i want the result for only $1="cat"
Thanks in advance
JS
awk '$1 ~ /^cat / {if ($2==0){print $5} } ' file,
Or use equality (==) instead of pattern matching (~). The ~ operator will match the pattern anywhere in the field.
awk '$1 == "cat" && $2 == 0 { print $5 }' file
aju_kup's solution to use a better pattern is of course a perfectly good solution as well; just pointing out an alternative which may be more convenient in some situations.