How to reorder a sequence of 4 numbers in a string using bash script on unix - bash

I have the following script:
#!/bin/bash
EXTENT=`ogrinfo -so area.geojson ogrgeojson | grep Extent | sed 's/Extent: //g' | sed 's/(//g' | sed 's/)//g' | sed 's/ - /, /g'`
echo $EXTENT
EXTENT=`echo $EXTENT | awk -F ',' '{print $1 " " $4 " " $3 " " $2}'`
echo $EXTENT
and for some reason it eats up some of the numbers and output:
12.834778, 52.014472, 13.610687, 52.399905
13.610687 52.014472
where expected on the second line is the 4 numbers.
The thing that I do not understand is that, if i copy paste the 4 commands myself to terminal everything works as expected. What do i need to do to make it work in a bash script?

The only way I found to reproduce your problem was this:
$ echo $'12.834778\r, 52.014472\r, 13.610687\r, 52.399905' |
awk -F ',' '{print $1 " " $4 " " $3 " " $2}'
52.014472 13.610687
Please remove the "carriage returns" in your file and you should be fine.

Related

sh to read a file and take particular value in shell

I need to read a json file and take value like 99XXXXXXXXXXXX0 and cccs and write in csv which having column BASE_No and Schedule.
Input file: classedFFDCD_5666_4888_45_2018_02112018012106.021.json
"bfgft":"99XXXXXXXXXXXX0","fp":"XXXXXX","cur_gt":225XXXXXXXX0,"cccs"
"bfgft":"21XXXXXXXXXXXX0","fp":"XXXXXX","cur_gt":225XXXXXXXX0,"nncs"
"bfgft":"56XXXXXXXXXXXX0","fp":"XXXXXX","cur_gt":225XXXXXXXX0,"fgbs"
"bfgft":"44XXXXXXXXXXXX0","fp":"XXXXXX","cur_gt":225XXXXXXXX0,"ddss"
"bfgft":"94XXXXXXXXXXXX0","fp":"XXXXXX","cur_gt":225XXXXXXXX0,"jjjs"
Expected output:
BASE_No,Schedule
99XXXXXXXXXXXX0,cccs
21XXXXXXXXXXXX0,nncs
56XXXXXXXXXXXX0,fgbs
44XXXXXXXXXXXX0,ddss
94XXXXXXXXXXXX0,jjjs
I am using below code for reading file name and date, but unable to read file for BASE_No,Schedule.
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for line in `ls -lrt *.json`; do
date=$(echo $line |awk -F ' ' '{print $6" "$7}');
file=$(echo $line |awk -F ' ' '{print $9}');
echo ''$file','$(date "+%Y/%m/%d %H.%M.%S")'' >> $File_Tracker`
Assuming the structure of the json doesnt change for every line, the sample code checks through line by line to retrieve the particular value and concatenates using printf. The output is then stored as new output.txt file.
#!/bin/bash
input="/home/kj4458/winhome/Downloads/sample.json"
printf "Base,Schedule \n" > output.txt
while IFS= read -r var
do
printf "`echo "$var" | cut -d':' -f 2 | cut -d',' -f 1`,`echo "$var" | cut -d':' -f 4 | cut -d',' -f 2` \n" | sed 's/"//g' >> output.txt
done < "$input"
awk -F " \" " ' {print $4","$12 }' file
99XXXXXXXXXXXX0,cccs
21XXXXXXXXXXXX0,nncs
56XXXXXXXXXXXX0,fgbs
44XXXXXXXXXXXX0,ddss
94XXXXXXXXXXXX0,jjjs
I got that result!

shell script not running but command line running [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 5 years ago.
I have a Error Log file with contents as -
2017/11/06 13:17:05 [notice] 18164#18164: signal process started
.
.
.
I have command that will extract the date and notice,warn message
cat whole_error.log | cut -d" " -f1,3,5-20 | sort -nr | grep "warn\|notice" | awk '$1 >= 2017/09/02 && $1 <= 2017/11/06' | awk '{print $1 " " $2" " $3}'
Its working fine entirely, i am getting the expected output
But, i want to take the start date and end date as command line argument input
and for that, i wrote the script as -
#!/bin/bash
file_name=$1
start_date=$2
end_date=$3
cat $file_name | cut -d" " -f1,3,5-20 | sort -nr | grep "warn\|notice" | awk '$1 >= $start_date && $1 <= $end_date' | awk '{print $1 " " $2" " $3}'
But its not returning anything. No error message nothing..Just the prompt arrives again.
How to fix this..
Use -v to pass shell variables into awk:
#!/bin/bash
file_name=$1
start_date=$2
end_date=$3
<"$file_name" cut -d" " -f1,3,5-20 \
| sort -nr \
| awk -v start_date="$start_date" -v end_date="$end_date" \
'/warn|notice/ && $1 >= start_date && $1 <= end_date {print $1 " " $2" " $3}'

Shell Scripting command not found Error

I'm new to programming with shell and want to ask what is wrong with my code?
#!/bin/bash
#DHT11
SCRIPT="/var/www/ErnestynoFailai/scripts/DHT 11 4"
#DHT22
#SCRIPT="/root/to/folder/DHT 22 4"
#AM2302
#SCRIPT="/root/to/folder/DHT 2302 4"
HUMIDITY=`$SCRIPT | grep "Temp" | awk -F " " '{print $7}'`
TEMPRATURE=`$SCRIPT | grep "Temp" | awk -F " " '{print $3}'`
#-a = AND = &&
while [ $HUMIDITY=="" -a $TEMPRATURE=="" ]
do
$HUMIDITY=`$SCRIPT | grep "Temp" | awk -F " " '{print $7}'`
$TEMPRATURE=`$SCRIPT | grep "Temp" | awk -F " " '{print $3}'`
done
echo "$HUMIDITY"
echo "$TEMPRATURE"
I'm getting:
line 14 or 15 =26: or =: command not found...
There are two problems:
These lines are not returning anything, or at least a string:
HUMIDITY=`$SCRIPT | grep "Temp" | awk -F " " '{print $7}'`
TEMPRATURE=`$SCRIPT | grep "Temp" | awk -F " " '{print $3}'`
This is causing =: command not found errors.
Your while condition needs to be
while [[ $HUMIDITY == "" && $TEMPRATURE == "" ]]
Finally, while not causing problems, TEMPERATURE is misspelled, which might cause you grief later on.
Variables have to be assigned without leading $:
HUMIDITY=`$SCRIPT | grep "Temp" | awk -F " " '{print $7}'`
TEMPRATURE=`$SCRIPT | grep "Temp" | awk -F " " '{print $3}'`

Echo without a newline character results a syntax error

I am writing a shell script and I would like to have this code
echo $(awk '{print $1}' /proc/uptime) / 3600 | bc
without the newline character at the end.
I wanted to write it using echo -n, but this code
echo -n $(awk '{print $1}' /proc/uptime) / 3600 | bc
results a syntax error:
(standard_in) 1: syntax error
Can you help me with this?
Thank you very much!
echo $(awk '{print $1}' /proc/uptime) / 3600 | bc | tr -d "\n"
Alternatives:
echo -n $(($(cut -d . -f 1 /proc/uptime)/3600))
mapfile A </proc/uptime; echo -n $((${A%%.*}/3600))
A solution using echo -n:
echo -n $(echo $(awk '{print $1}' /proc/uptime) / 3600 | bc)
In general, if foo produces a line of output, you can print the same output without a newline using echo -n $(foo), even if foo is complicated.
A more straightforward solution using pure awk (since awk does arithmetic and output formatting, there's not much point in using both awk and bc):
awk '{printf("%d", $1 / 3600)}' /proc/uptime

Bash: "xargs cat", adding newlines after each file

I'm using a few commands to cat a few files, like this:
cat somefile | grep example | awk -F '"' '{ print $2 }' | xargs cat
It nearly works, but my issue is that I'd like to add a newline after each file.
Can this be done in a one liner?
(surely I can create a new script or a function that does cat and then echo -n but I was wondering if this could be solved in another way)
cat somefile | grep example | awk -F '"' '{ print $2 }' | while read file; do cat $file; echo ""; done
Using GNU Parallel http://www.gnu.org/software/parallel/ it may be even faster (depending on your system):
cat somefile | grep example | awk -F '"' '{ print $2 }' | parallel "cat {}; echo"
awk -F '"' '/example/{ system("cat " $2 };printf "\n"}' somefile

Resources