Issue
In bash i want to make the .txt file to have the link in line 1. and the numbers in line 2.
curl -s "http://kodi:[email protected]:8080/jsonrpc?Base" -H 'Content-Type: application/json' --data '[{"jsonrpc":"2.0","method":"Player.GetProperties","params":[1,["time"]],"id":17},{"jsonrpc":"2.0","method":"Player.GetItem","params":[1,["file"]],"id":18}]' | jq
json file format with jq
[ { "id": 17, "jsonrpc": "2.0", "result": { "time": { "hours": 2, "milliseconds": 200, "minutes": 3, "seconds": 5 } } }, { "id": 18, "jsonrpc": "2.0", "result": { "item": { "file": "plugin://plugin.video.youtube/play/?video_id=rhMTZB2WJWA", "label": "FULL SHOW - Burton US Open Men's Slopestyle Semi-Finals", "type": "unknown" } } } ]
curl -s "http://kodi:[email protected]:8080/jsonrpc?Base" -H 'Content-Type: application/json' --data '[{"jsonrpc":"2.0","method":"Player.GetProperties","params":[1,["time"]],"id":17},{"jsonrpc":"2.0","method":"Player.GetItem","params":[1,["file"]],"id":18}]' | jq --raw-output '.[].result.item.file, .[].result.time.hours, .[].result.time.minutes, .[].result.time.seconds | select(. != null)' > "c:\kodi\info.txt"
with this i can get this format in the info.txt file
info.txt
plugin://plugin.video.youtube/play/?video_id=rhMTZB2WJWA 2 3 5
example 2.
plugin://plugin.video.youtube/play/?video_id=rhMTZB2WJWA 0 11 22
example 3.
plugin://plugin.video.youtube/play/?video_id=rhMTZB2WJWA 0 55 9
how could it be done with jq, awk, paste, sed or something similar to make it look like this and put a 0 in front of every 1-9?
example 1.
plugin://plugin.video.youtube/play/?video_id=rhMTZB2WJWA
020305
example 2.
plugin://plugin.video.youtube/play/?video_id=rhMTZB2WJWA
001122
example 3.
plugin://plugin.video.youtube/play/?video_id=rhMTZB2WJWA
005509
Thanks in advance for any help!
Solution
One awk
idea:
awk '
FNR==1 {print;next} # print line 1 as is
{printf "%02d",$1} # print all other lines on 2nd line (notice no "\n"),
# left padding numbers with "0" to 2 digits
END {printf "\n"} # print EOL
' file
Assuming the 3 samples of data are in files file{1..3}
:
for f in file{1..3}
do
echo "+++++++++++ ${f}"
awk 'FNR==1 {print;next} {printf "%02d",$1} END {printf "\n"}' "${f}"
done
This generates:
+++++++++++ file1
plugin://plugin.video.youtube/play/?video_id=rhMTZB2WJWA
020305
+++++++++++ file2
plugin://plugin.video.youtube/play/?video_id=rhMTZB2WJWA
001122
+++++++++++ file3
plugin://plugin.video.youtube/play/?video_id=rhMTZB2WJWA
005509
Answered By - markp-fuso