This question already has answers here:
awk print overwrite strings
(1 answer)
How do I use shell variables in an awk script?
(7 answers)
Closed 4 years ago.
When I run below command, the "awk" changes my ip unexpectedly. I could not find how to fix this. Regards.
echo $ip
10.165.6.90
cat text.txt | grep STRING | awk -F'[. ="]' '{print '$ip' ";" $10 ";" $15}'
10.1650.60.9;163840000;X
10.1650.60.9;232525824;Y
10.1650.60.9;232526848;X
10.1650.60.9;232529920;Z
10.1650.60.9;232530944;T
Related
This question already has answers here:
How to find the last field using 'cut'
(14 answers)
Closed 4 years ago.
Hello everyone i want to know to cut the last field with separator :
without knowing how many fields that i have any ideas please .
is there any option for command cut .
You can revert the string and then print 1st character. Itself cut can't work from backwards.
echo "Your string ABC" | rev | cut -c 1
Awk is the right tool for this. Try :
ls -lh | awk '{ print $NF }'
This question already has answers here:
What does "|" mean in a terminal command line? [closed]
(3 answers)
Closed 4 years ago.
example :
diskFreeSpace="$(df | grep -v -E 'Filesystem|udev|tmpfs|Home|/dev/sr1|/dev/sr0' | awk '{ print $5-1+1}')"
and hope if you understand what is this command u help me
the symbol is called a "pipe".
The command df | grep is "piping" (redirecting) the standard output of the df command into the standard input of the grep command. In this example the grep command filters the output of the df command. Then the awk commands is for display/format purposes.
Inside the regular expression it is specific to the grep syntax since it is surrounded by quotes it is only considered as an argument passed to grep
This question already has answers here:
Unable to separate semi-colon separated line awk
(3 answers)
Closed 5 years ago.
I would like to display fields first and third in file full on entries like the one below
first;second;third;four
Simply with cut command:
cut -d';' -f1,3 file
Simple awk could help you here.
awk -F";" '{print $1,$3}' Input_file
This question already has answers here:
difference between grep Vs cat and grep
(5 answers)
Closed 8 years ago.
I saw an example where some one did this:
cat source.txt | grep a
But I always do it like:
grep a source.txt
What's the difference between the two?
The first one is a classical “useless use of cat” (UUOC).
This question already has answers here:
Using awk with variables
(3 answers)
Closed 8 years ago.
I am trying to write a shell script to get certain data from below sample logs..
Below is a sample log:
2014-07-08 16:08:25,684: |ABC_130|1|10123ffffff2|P|489440201
2014-07-08 17:08:25,684: |ABC_130|1|aaaaaxxxxaab|P|489440201
2014-07-08 19:08:25,684: |ABC_130|1|aaaaababbaab|P|489440201
Below is a part of the script where I am facing issue, the issue I am facing is that the awk command doesn't give any output.
#!/bin/sh
DATE_HOUR="`date -d '1 hour ago' "+%Y-%m-%d %H"`"
awk -F ":" '{if ($1='"$DATE_HOUR"') print $0}' log.txt
Don't use shell variable like that in awk. Use -v name=val:
awk -F ":" -v dt="$DATE_HOUR" '$1==dt' log.txt
btw I reduced your awk command to '$1==dt' since print $0 is default action and also if condition can be moved out of curly braces.