How to restructure the line with proper alignment in shell script - bash

I am trying to get the Software Information from Red Hat. As far as I have tried,
rpm -qi <softwarename>
provides the enough information
from the above information, I do require Name, Version, Release, Vendor, Build Date, & Install Date.
I could able to get the Name Version Release & Install Date by using the following command.(here I have grepped only name and version for simplicity)
rpm -qi perl-Git | grep -E '^Name :|^Version :' | awk -F 'Relocations:|Vendor:' '{print $1}'
I have no clue to get the Right side of the Information, since I could not able to find a split string between two fields.
Any suggestions ??

Use --queryformat
$ rpm -q --queryformat "%{NAME}\n%{VERSION}\n%{RELEASE}\n%{VENDOR}\n%{BUILDTIME}\n%{INSTALLTIME}\n" perl-Git
perl-Git
2.8.0
1.WANdisco.308
(none)
1459260423
1493311622
Then you can print it how you like, and parse it how you like.

Related

how can I scrap data from reddit (in bash)

I want to scrap titles and date from http://www.reddit.com/r/movies.json in bash
wget -q -O - "http://www.reddit.com/r/movies.json" | grep -Po '(?<="title": ").*?(?=",)' | sed 's/\"/"/'
I have titles but I don't know how to add dates, can someone help?
wget -q -O - "http://www.reddit.com/r/movies.json" | grep -Po
'(?<="title": ").*?(?=",)' | sed 's/"/"/'
As extension suggest it is JSON (application/json) file, therefore grep and sed are poorly suited for working with it, as they are mainly for using regular expressions. If you are allowed to install tools, jq should be handy here. Try using your system package manager to install it, if it succeed you should get pretty printed version of movies.json by doing
wget -q -O - "http://www.reddit.com/r/movies.json" | jq
and then find where interesting values are placed which should allow you to grab it. See jq Cheat Sheet for example of jq usage. If you are limited to already installed tools I suggest taking look at json module of python.

How to list installed go packages

To my knowledge go distribution comes with some sort of package manager. After go 1.4.1 installation I've run go help in order to find any sub-command capable of listing locally installed go packages, but unfortunately got none.
So how to do it?
goinstall is now history
goinstall was replaced by go get. go get is used to manage external / 3rd party libraries (e.g. to download them, update them, install them etc).
Type go help get to see command line help, or check out these pages:
Command go
About the go command (blog post)
If you want to list installed packages, you can do that with the go list command:
Listing Packages
To list packages in your workspace, go to your workspace folder and run this command:
go list ./...
./ tells to start from the current folder, ... tells to go down recursively. Of course this works in any other folders not just in your go workspace (but usually that is what you're interested in).
List All Packages
Executing
go list ...
in any folder lists all the packages, including packages of the standard library first followed by external libraries in your go workspace.
Packages and their Dependencies
If you also want to see the imported packages by each package, you can try this custom format:
go list -f "{{.ImportPath}} {{.Imports}}" ./...
-f specifies an alternate format for the list, using the syntax of package template. The struct whose fields can be referenced can be printed by the go help list command.
If you want to see all the dependencies recursively (dependencies of imported packages recursively), you can use this custom format:
go list -f "{{.ImportPath}} {{.Deps}}" ./...
But usually this is a long list and just the single imports ("{{.Imports}}") of each package is what you want.
Also see related question: What's the Go (mod) equivalent of npm-outdated?
Start Go documentation server:
godoc --http :6060
Visit http://localhost:6060/pkg
There will be list of all your packages.
When you install new ones they do not appear automatically. You need to restart godoc.
go list ... is quite useful, but there were two possible issues with it for me:
It will list all packages including standard library packages. There is no way to get the explicitly installed packages only (which is what I assume the more interesting inquiry).
A lot of times I need only the packages used in my projects (i.e. those listed in the respective go.mod files), and I don't care about other packages lying around (which may have been installed just to try them out). go list ... doesn't help with that.
So here's a somewhat different take. Assuming all projects are under ~/work:
find ~/work -type f -name go.mod \
-exec sed $'/^require ($/,/^)$/!d; /^require ($/d;/^)$/d; /\\/\\/ indirect$/d; s/^\t+//g' {} \; \
| cut -d' ' -f1 \
| sort | uniq
A line by line explanation:
find all go.mod files
apply sed to each file to filter its content as follows (explained expression by expression):
extract just the require( ... ) chunks
remove the require( and ) lines, so just lines with packages remain
remove all indirect packages
remove leading tabs 1)
extract just the qualified package name (drop version information)
remove duplicate package names
1) Note the sed expression argument uses bash quoting to escape the TAB character as "\t" for readability over a literal TAB.
on *nix systems (possibly on windows with bash tools like msysgit or cmder), to see what packages I have installed, I can run:
history | grep "go get"
But thats messy output. For whatever reason I decided to see of i could clean that output up a little so i made an alias for this command:
history | grep 'go get' | grep -v ' history ' | sed -e $'s/go get /\\\\\ngo get /g' | grep 'go get ' | sed -e $'s/-u //g' | sed -e $'s/-v //g' | sed -e $'s/ &&//g' | grep -v '\\\n' | egrep 'get [a-z]' | sed -e $'s/go get //g' | sed -e $'s/ //g' | sort -u
please don't ask why I did this. Challenge maybe? Let me explain the parts
history the history
grep "go get" grep over history and only show lines where we went and got something
grep -v " history " and remove times when we have searched for "got get" in history
sed -e $'s/go get /\\\\\ngo get /g' Now we take any instances of "go get " and shove a new line in front of it. Now they're all at the beginning.
grep "go get " filter only lines that now start with "go get"
sed -e $'s/-u //g' and sed -e $'s/-v //g' remove flags we have searched for. You could possibly leave them in but may get duplicates when output is done.
sed -e $'s/ &&//g' some times we install with multiple commands using '&&' so lets remove them from the ends of the line.
grep -v "\\\n" my output had other lines with newlines printed I didnt need. So this got rid of them
egrep "get [a-z]" make sure to get properly formatted go package urls only.
sed -e $'s/go get //g' remove the "go get " text
sed -e $'s/ //g' strip any whitespace (needed to filter out duplicates)
sort -u now sort the remaining lines and remove duplicates.
This is totally untested on other systems. Again, I am quite sure there is a cleaner way to do this. Just thought it would be fun to try.
It would also probably be more fun to make a go ls command to show the actual packages you explicitly installed. But thats a lot more work. Especially since i'm only still learning Go.
Output:
> gols
code.google.com/p/go.crypto/bcrypt
github.com/golang/lint/golint
github.com/kishorevaishnav/revelgen
github.com/lukehoban/go-find-references
github.com/lukehoban/go-outline
github.com/newhook/go-symbols
github.com/nsf/gocode
github.com/revel/cmd/revel
github.com/revel/revel
github.com/rogpeppe/godef
github.com/tpng/gopkgs
golang.org/x/tools/cmd/goimports
golang.org/x/tools/cmd/gorename
gopkg.in/gorp.v1
sourcegraph.com/sqs/goreturns

Get Macbook screen size from terminal/bash

Does anyone know of any possible way to determine or glean this information from the terminal (in order to use in a bash shell script)?
On my Macbook Air, via the GUI I can go to "About this mac" > "Displays" and it tells me:
Built-in Display, 13-inch (1440 x 900)
I can get the screen resolution from the system_profiler command, but not the "13-inch" bit.
I've also tried with ioreg without success. Calculating the screen size from the resolution is not accurate, as this can be changed by the user.
Has anyone managed to achieve this?
I think you could only get the display model-name which holds a reference to the size:
ioreg -lw0 | grep "IODisplayEDID" | sed "/[^<]*</s///" | xxd -p -r | strings -6 | grep '^LSN\|^LP'
will output something like:
LP154WT1-SJE1
which depends on the display manufacturer. But as you can see the first three numbers in this model name string imply the display-size: 154 == 15.4''
EDIT
Found a neat solution but it requires an internet connection:
curl -s http://support-sp.apple.com/sp/product?cc=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | cut -c 9-` |
sed 's|.*<configCode>\(.*\)</configCode>.*|\1|'
hope that helps
The next script:
model=$(system_profiler SPHardwareDataType | \
/usr/bin/perl -MLWP::Simple -MXML::Simple -lane '$c=substr($F[3],8)if/Serial/}{
print XMLin(get(q{http://support-sp.apple.com/sp/product?cc=}.$c))->{configCode}')
echo "$model"
will print for example:
MacBook Pro (13-inch, Mid 2010)
Or the same without perl but more command forking:
model=$(curl -s http://support-sp.apple.com/sp/product?cc=$(system_profiler SPHardwareDataType | sed -n '/Serial/s/.*: \(........\)\(.*\)$/\2/p')|sed 's:.*<configCode>\(.*\)</configCode>.*:\1:')
echo "$model"
It is fetched online from apple site by serial number, so you need internet connection.
I've found that there seem to be several different Apple URLs for checking this info. Some of them seem to work for some serial numbers, and others for other machines.
e.g:
https://selfsolve.apple.com/wcResults.do?sn=$Serial&Continue=Continue&num=0
https://selfsolve.apple.com/RegisterProduct.do?productRegister=Y&country=USA&id=$Serial
http://support-sp.apple.com/sp/product?cc=$serial (last 4 digits)
https://selfsolve.apple.com/agreementWarrantyDynamic.do
However, the first two URLs are the ones that seem to work for me. Maybe it's because the machines I'm looking up are in the UK and not the US, or maybe it's due to their age?
Anyway, due to not having much luck with curl on the command line (The Apple sites redirect, sometimes several times to alternative URLs, and the -L option doesn't seem to help), my solution was to bosh together a (rather messy) PHP script that uses PHP cURL to check the serials against both URLs, and then does some regex trickery to report the info I need.
Once on my web server, I can now curl it from the terminal command line and it's bringing back decent results 100% of the time.
I'm a PHP novice so I won't embarrass myself by posting the script up in it's current state, but if anyone's interested I'd be happy to tidy it up and share it on here (though admittedly it's a rather long winded solution to what should be a very simple query).
This info really should be simply made available in system_profiler. As it's available through System Information.app, I can't see a reason why not.
Hi there for my bash script , under GNU/Linux : I make the follow to save
# Resolution Fix
echo `xrandr --current | grep current | awk '{print $8}'` >> /tmp/width
echo `xrandr --current | grep current | awk '{print $10}'` >> /tmp/height
cat /tmp/height | sed -i 's/,//g' /tmp/height
WIDTH=$(cat /tmp/width)
HEIGHT=$(cat /tmp/height)
rm /tmp/width /tmp/height
echo "$WIDTH"'x'"$HEIGHT" >> /tmp/Resolution
Resolution=$(cat /tmp/Resolution)
rm /tmp/Resolution
# Resolution Fix
and the follow in the same script for restore after exit from some app / game
in some S.O
This its execute command directly
ResolutionRestore=$(xrandr -s $Resolution)
But if dont execute call the variable with this to execute the varible content
$($ResolutionRestore)
And the another way you can try its with the follow for example
RESOLUTION=$(xdpyinfo | grep -i dimensions: | sed 's/[^0-9]*pixels.*(.*).*//' | sed 's/[^0-9x]*//')
VRES=$(echo $RESOLUTION | sed 's/.*x//')
HRES=$(echo $RESOLUTION | sed 's/x.*//')

Bash script - For loop multiple arguments and awk

I'm trying to write a BASH script for patching my CentOS servers. My goal is that when I run the script, it outputs the package name, the currently installed version, and new package version. Something like this:
nspr.x86_64 / 4.8.8-3.el6 / 4.8.9-3.el6_2
This way, if I ever need to downgrade because a package broke something, I have a record of this.
The command yum check-update command gives me the 1st piece of information (the package name) and the 3rd piece of information (the new version) listed above, and I can use awk to separate the two. It's easy after that; just run rpm -q {package} to get the 2nd piece of information, then concatenate them.
However, I'm running into trouble with the for loop in my code. I need to pass multiple arguments into the loop (the package name and newer version) so I can echo them later.
Here's my code:
for package in `/usr/bin/yum --cacheonly --noplugins check-update | awk '{print $1, $2}'`;
do
OLD_VER=`rpm -q ${package}` # ${package} should actually be $1 from the awk statement above
NEW_VER=${2} # This is $2 from the awk statement above
echo "${package} / ${OLD_VER} / ${NEW_VER}"
done
Pardon the obvious errors in the code; I'm new to BASH scripting. My confusion mostly stems from awk; I'm not very familiar with it, nor how it interacts with a BASH script. However, I think it's clear what I'm trying to do. Does package get passed as an array? Is there a better way to do this?
you want to read the whole line:
/usr/bin/yum --cacheonly --noplugins check-update |
while read line; do
set - $line;
echo first: $1 second: $2
done
Try this:
/usr/bin/yum --cacheonly --noplugins check-update \
| awk '{package=$1; newVer=$2; cmd = "rpm -q " package; cmd | getline oldVer; close(cmd); print package " / " oldVer " / " newVer}'
The issue with your script was that although both bash and awk use $ to reference variables, they are different vars and you can't reference awk's $2 from bash or vice versa. Writing everything in either awk or bash should solve the issue.

SVN: How to know in which revision a file was deleted?

Given that I'm using svn command line on Windows, how to find the revision number, in which a file was deleted? On Windows, there are no fancy stuff like grep and I attempting to use command line only, without TortoiseSVN. Thanks in advance!
EDIT:
I saw a few posts, like examining history of deleted file but it did not answer my question.
Is there any way other than svn log -v url > log.out and search with Notepad?
Install Cygwin.
I use this:
svn log -v --limit <nr> -v | grep -E '<fileName>|^r' | grep -B 1 <fileName>
where
fileName - the name of the file or any pattern which matches it
nr - the number of latest revisions in which I want to look for
This will give you the revisions for all the actions (add, delete, remove, modify) concerning the file, but with a simple tweak with grep you can get the revisions only for deletion.
(Obviously, --limit is optional, however you usually have an overview about how deep you need to search which gains you some performance.)
The log is the place to look. I know you don't want to hear that answer, but that is where you need to look for deleted files in SVN.
The reason for this is simply that a deleted file is not visible after it's been deleted. The only place to find out about its existence at all is either in the logs, or by fetching out an earlier revision prior to it being deleted.
The easiest way I know of to deal with this problem is to move away from the the command line, and use a GUI tool such as TortoiseSVN.
TortoiseSVN links itself into the standard Windows file Explorer, so it's very easy to use. In the context of answering this question, you would still use it to look at the logs, but it becomes a much quicker excersise:
Browser to the SVN folder you want to examine. Then right-click on the folder icon and select TortoiseSVN -> View Logs from the context menu.
You'll now get a window showing all the revisions made in that folder. In particular, it is easy to see which revisions have had additions and deletions, because the list includes a set of Action icons for each revision. You can double-click on a revision to get a list of files that were changed (or straight into a diff view if only one file was changed)
So you can easily see which revisions have had deletions, and you can quickly click them to find out which files were involved. It really is that easy.
I know you're asking about the command-line, but for administrative tasks like this, a GUI browser really does make sense. It makes it much quicker to see what's happening compared with trying to read through pages of cryptic text (no matter how well versed you are at reading that text).
This question was posted and answered some time ago.
In this answer I'll try to show a flexible way to get the informations asked and extend it.
In cygwin, use svn log in combination with awk
REPO_URL=https://<hostname>/path/to/repo
FILENAME=/path/to/file
svn log ${REPO_URL} -v --search "${FILENAME}" | \
awk -v var="^ [D] ${FILENAME}$" \
'/^r[0-9]+/{rev=$1}; \
$0 ~ var {print rev $0}'
svn log ${REPO_URL} -v --search "${FILENAME}" asks svn log for a verbose log containing ${FILENAME}. This reduces the data transfer.
The result is piped to awk. awk gets ${FILENAME} passed via -v in the var vartogether with search pattern var="^ [D] ${FILENAME}$"
In the awk program /^r[0-9]+/ {rev=$1} assigns the revision number to rev if line matches /^r[0-9]+/.
For every line matching ^ [D] ${FILENAME}$ awk prints the stored revision number rev and the line: $0 ~ var {print rev $0}
if you're interested not only in the deletion of the file but also creation, modification, replacing, change Din var="^ [D] ${FILENAME}$"to DAMR.
The following will give you all the changes:
svn log ${REPO_URL} -v --search "${FILENAME}" | \
awk -v var="^ [DAMR] ${FILENAME}$" \
'/^r[0-9]+/ {rev=$1}; \
$0 ~ var {print rev $0}'
And if you're interested in username, date and time:
svn log ${REPO_URL} -v --search "${FILENAME}" | \
awk -v var="^ [DAMR] ${FILENAME}$" \
'/^r[0-9]+/ {rev=$1;user=$3;date=$5;time=$6}; \
$0 ~ var {print rev " | " user " | " date " | " time " | " $0}'

Resources