AWK passing variables - shell

Can I able to give the value(32) in the form of variable in the below awk command instead of hardcoding 32 directly?
df -h | head -1|awk '{printf "%s%32s",$1,$2}'
I tried like awk -v var=32 but its not working.
Can anyone suggest me please.

awk -v var=32 '{printf "%s%*s\n", $1, var, $2}'

awk -v var=32 '{printf "%s%"var"s", $1,$2}'
Note: The solution of Ed Morton is cleaner as it does not use AWK string concatenations but makes direct use of the printf formatting
Simultaneously, if you want to get rid of your head then you can combine everything as:
$ df -h | awk -v var=32 '{printf "%s%"var"s", $1,$2; exit}'

One alternative to the answer given by Ed Morton, could be useful, if you have the number already stored in an environment variable (say: FIELD_WIDTH). In this case,
df -h | head -1|awk '{printf "%s%" ENVIRON["FIELD_WIDTH"] "s",$1,$2}'
would do the job.
UPDATE:
You should also get rid of the head:
df -h | awk 'NR==1 {printf "%s%" ENVIRON["FIELD_WIDTH"] "s",$1,$2}'

Related

Match a length of string in column using awk

I'm trying to extract the column which matches the date pattern like: YYYY-MM-DD, but didn't understand the difference between below commands
Working command
echo 1,2,3,4,2015-12-34 | awk -F, '{if($5~/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/) print $1}'
Not working command
echo 1,2,3,4,2015-12-34 | awk -F, '{if($5~/^[0-9]{d}-[0-9]{2}-[0-9]{2}$/)print $1}'
Can someone explain me why it's happening, am i missing a granular thing?
You could try following and let me know if this helps you.
echo 1,2,3,4,2015-12-34 | awk --re-interval -F, '{if($5~/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/)print $1}'
OR
echo 1,2,3,4,2015-12-34 | awk --re-interval -F, 'match($NF,/[0-9]{4}-[0-9]{2}-[0-9]{2}/){print $1}'
NOTE: My awk version is OLD so I am using --re-interval you could remove it in case you have recent awk version.

i need to use variable in instead of direct date in shell script awk

i need to use variable in instead of direct date.
cat file | awk -F, '{ if ($1>"2012-08-20 11:30" && $1<"2012-08-22 16:00") print }'
thanks in advance
Based on your shown code, could you please try following and let me know if this helps you.(In lack of samples I haven't tested it)
awk -v date1="2012-08-20 11:30" -v date2="2012-08-22 16:00" -F, '($1>date1 && $1<date2)' Input_file
In case your variables are coming from shell to awk then following could help you on same, you could change date subtraction order as per your need too:
date1="2012-08-20 11:30"
date2="2012-08-22 16:00"
awk -v date_1="$date1" -v date_2="$date2" -F, '($1>date_1 && $1<date_2)' Input_file

How do I find a number in between numbers and colons?

I'm trying to get the numbers that's in between two colons and other numbers in a file.
Example:
1234:12345678:1234
1234:12345678:1234
1234:12345678:1234
I want the output to show all of the 12345678's and nothing else.
Like this:
12345678
12345678
12345678
I achieved this using:
egrep -o "[0-9]{8}" file
Problem is that I need a different solution than egrep -o (awk or sed?)
I searched and tried a couple of things but without succes.
Any help would be appreciated!
If the "number" is always 2nd column, you could do with awk:
awk -F: '{print $2}' file
For the awk solution,
awk -F: '{print $2}' file
Or simply use cut to do that,
cut -d: -f2 file
would this work?
awk -F':' '{print $2}' test > test results.txt

Awk adding constant values

I have data in the text file like val1,val2 with multiple lines
and I want to change it to 1,val1,val2,0,0,1
I tried with print statement in awk(solaris) to add constants by it didn't work.
What is the correct way to do it ?
(From the comments) This is what I tried
awk -F, '{print "%s","1,"$1","$2"0,0,1"}' test.txt
Based on the command you posted, a little change makes it:
$ awk -F, 'BEGIN{OFS=FS} {print 1,$1,$2,0,0,1}' file
1,val1,val2,0,0,1
OR using printf (I prefer print):
$ awk -F, '{printf "1,%s,%s,0,0,1", $1, $2}' file
1,val1,val2,0,0,1
To prepend every line with the constant 1 and append with 0,0,1 simply do:
$ awk '{print 1,$0,0,0,1}' OFS=, file
1,val1,val2,0,0,1
A idiomatic way would be:
$ awk '$0="1,"$0",0,0,1"' file
1,val1,val2,0,0,1
Using sed:
sed 's/.*/1,&,0,0,1/' inputfile
Example:
$ echo val1,val2 | sed 's/.*/1,&,0,0,1/'
1,val1,val2,0,0,1

Get a number out of a string without spaces

I grep this string from a SVG file:
<g transform="translate(0.000000,980.000000) scale(0.100000,-0.100000)"
and I want to get the number 980 within a bash script variable. My problem is that I don't manage to pipe the right expression to get only that number 980 out of the string. I need to add that, of course, the number 980 can be any number from any length (for example, 5967 or 98...).
Could you offer me a solution? I'm stuck there...
Assuming your SVG input file is input.svg. You should try:
cat input.svg | grep translate | sed 's/^.*translate(.*,\(.*\)\..*) scale.*$/\1/'
From my understanding, you can use awk.
grep whatever | awk -F"," '{print $2}' | awk -F"." '{print $1}'
No need for more than one simple command:
awk -F"[,.]" '/your_search_string/{print $3}'
HTH Chris
just bash, I assume you want the 2nd parameter to "translate()"
$ line='<g transform="translate(0.000000,980.000000) scale(0.100000,-0.100000)"'
$ num=${line#*translate*,}; echo $num
980.000000) scale(0.100000,-0.100000)"
$ num=${num%%.*}; echo $num
980
This might work for you:
sed '/^<g transform/!d;s/[^,]*,//;s/[^0-9].*//
The below awk command should work for you.
awk -v FS="[ =(,)]" '{print $5}' "Yourfilename" | awk -v FS="[.]" '{print $1}'

Resources