get the number after zeroes - linux

I need to get 88090000 after zeroes. How can I do that using awk?
There can be any number of zeroes before the number. But, I need the number after the zeroes.
0000000088090000
I appreciate your help.

Just add 0.
$ awk '{ print $0 + 0 }' <<< '0000000088090000'
88090000

Using regular expressions:
echo '0000000088090000' | awk '{ sub(/^0+/, ""); print }'

One way:
echo "0000000088090000" | awk '{ printf "%d\n", $0 }'

Using sed:
[jaypal:~/Temp] echo "0000000088090000" | sed 's/^0\+//g'
88090000

Related

Changing the date from 2012-01-31 to 31-01-2012 in sed/awk/grep

I'm trying to swap the digits format from 2012-01-31 to 31-01-2012. Looked for lots of pages on Stack Overflow and found nothing. Can someone help me with this task?
I tried this so far:
echo 2012-01-31 | sed 's/^([0-9]*)-([0-9]*)-([0-9]*)/\3-\2-\1/'
You can use date to first read and then format the output:
date -d 2012-01-31 +%d-%m-%Y
The correct version of what you were trying to do is following.
echo '2012-01-31' | sed -E 's/^([0-9]*)-([0-9]*)-([0-9]*)/\3-\2-\1/'
or without extended regex,
echo '2012-01-31' | sed 's/^\([0-9]*\)-\([0-9]*\)-\([0-9]*\)/\3-\2-\1/'
If you know the input is correct, but just need to reorder them, following will be more simple.
echo '2012-01-31' | sed -E 's/^(....)-(..)-(..)/\3-\2-\1/'
With pure bash,
a='2012-01-31'
[[ "$a" =~ (....)-(..)-(..) ]] || exit # exit if regex match failed
echo "${BASH_REMATCH[3]}-${BASH_REMATCH[2]}-${BASH_REMATCH[1]}"
With GNU awk:
awk -v FIELDWIDTHS='4 1 2 1 2' '{ print $5, $3, $1 }' OFS=-
With any awk:
awk '{ print $3, $2, $1 }' FS=- OFS=-

How to extract words between two characters in linux?

I have the following stored in a file named tmp.txt
user/config/jars/content-config-factory-3.2.0.0.jar
I need to store this word to a variable -
$variable=content-config-factory
I have written the following
while read line
do
var=$(echo $line | awk 'BEGIN{FS="\/"; OFS=" "} {print $NF}' )
var=$(echo $var | awk 'BEGIN{FS="-"; OFS=" "} {print $(1)}' )
echo $var
done < tmp.txt
This returns the result "content" instead of "content-config-factory".
Can anyone please tell me how to extract a word between two characters from a string efficiently.
An awk solution would be like
awk -F/ '{sub("-[^-]+$", "", $NF); print $NF}
Test
$ echo "user/config/jars/content-config-factory-3.2.0.0.jar" | awk -F/ '{sub("-[^-]+$", "", $NF); print $NF}'
content-config-factory
You can try this way also and get your expected result
variable=$(sed 's:.*/\(.*\)-.*:\1:' FileName)
echo $variable
OutPut :
content-config-factory
You could use grep,
grep -oP '(?<=/)[^/]*(?=-\d+\.)' file
Example:
$ var=$(echo 'user/config/jars/content-config-factory-3.2.0.0.jar' | grep -oP '(?<=/)[^/]*(?=-\d+\.)')
$ echo "$var"
content-config-factory

error bash extracting second column of a matched pattern

I am trying to search for a pattern and from the results i am extracting just the second column. The command works well in command line but not inside a bash script.
#!/bin/bash
set a = grep 'NM_033356' test.txt | awk '{ print $2 }'
echo $a
It doesnt print any output at all.
Input
NM_033356 2
NM_033356 5
NM_033356 7
Your code:
#!/bin/bash
set a = grep 'NM_033356' test.txt | awk '{ print $2 }'
echo $a
Change it to:
#!/bin/bash
a="$(awk '$1=="NM_033356"{ print $2 }' test.txt)"
echo "$a"
Code changes are based on your sample input.
.......
a="$(awk '/NM_033356/ { print $2 }' test.txt)"
Try this:
a=`grep 'NM_033356' test.txt | awk '{ print $2 }'`

extracting a text with awk

I want to grep a file and extract the third part of this line
#define SIM_VERSION_COMPAT 1302
with awk. So I wrote:
grep "#define SIM_VERSION_COMPAT" global.h | awk '{ print $$3 }'
The result should be 1302 but I get nothing (blank).
No need to use grep and pipe you can use awk like this:
awk '/#define SIM_VERSION_COMPAT/{print $3}' global.h
[spatel#tc01 ~]$ echo "#define SIM_VERSION_COMPAT 1302" | awk '{ print $3 }'
1302
Just using grep:
$ grep -Po '(?<=#define SIM_VERSION_COMPAT )[0-9]+' global.h
1302
This uses positive lookbehind to match lines containing #define SIM_VERSION_COMPAT but only prints the digit string following.
You can also use cut command as well
grep "#define SIM_VERSION_COMPAT" temp.txt | cut -d" " -f 3

Can I use awk to convert all the lower-case letters into upper-case?

I have a file mixed with lower-case letters and upper-case letters, can I use awk to convert all the letters in that file into upper-case?
Try this:
awk '{ print toupper($0) }' <<< "your string"
Using a file:
awk '{ print toupper($0) }' yourfile.txt
You can use awk, but tr is the better tool:
tr a-z A-Z < input
or
tr [:lower:] [:upper:] < input
Try this:
$ echo mix23xsS | awk '{ print toupper($0) }'
MIX23XSS
Something like
< yourMIXEDCASEfile.txt awk '{print toupper($0)}' > yourUPPERCASEfile.txt
You mean like this thread explains:
http://www.unix.com/shell-programming-scripting/24320-converting-file-names-upper-case.html
(Ok, it's about filenames, but the same principle applies to files)
If Perl is an option:
perl -ne 'print uc()' file
-n loop around input file, do not automatically print line
-e execute the perl code in quotes
uc() = uppercase
To print all lowercase:
perl -ne 'print lc()' file

Resources