P4, How to find changelist user from a given changelist? - perforce

Anybody knows how to get the change list user from a given changelist(say, #12345)?
p4 describe -s #12345
will give output like this:
Change #12345 by user#user_clientspec on 2010/07/26 10:26:29
affected files...
.......
Is there any command to give only the user name. Not with client spec as it shows user#user_clientspec.
Appreciate your help.
Thanks,
Tom

p4 change -o 12345 | grep ^User:
Or, if you're on a fairly recent version of the 'p4' command line:
p4 -F "%User%" -ztag change -o 12345

I think you'll just have to parse the output.
This ungainly bit of powershell will get you the user:
p4 describe -s 12345 | select-object -first 1 | %{ $_.Split()[3].Split('#')[0] }

to get only the user, and not other "User:" strings, trailing spaces, etc. try:
bash -c "p4 change -o 12345 | grep -oP '(?<=^User:).*' | xargs"

Related

Why doesn't the pipeline take effect in bash in Linux?

Here is to count the number of sessions by the same login user.
I could run the direct command if I know the specific user name, such as usera, as the following:
who | grep usera | wc -l
And if I don't know the current user, I need to user parameter.
But the following codes don't work:
currentuser=`whoami`
sessionnumber=`who | grep "$currentuser" | wc -l`
What's the error?
Thanks!
Grep has the -c flag so the wc -l plus the additional pipe is not needed.
who | grep -c -- "$USER"
"$LOGNAME" is also an option instead of "$USER", which one is bash specific? I don't know, all I know is that they are both on Linux and FreeBSD system. The -- is just a habit just in case the user starts with a dash grep will not interpret it as an option.
sessionnumber=`who | grep "$currentuser" | wc -l`
You are assigning the result of the who | ... command to a variable and to see its value you can use echo $sessionnumber
Looks like you are confused about parameters and variables.
What you are trying to get is likely
who | grep $(whoami) | wc -l
The $() is equivalent to the backticks you used.
When you write
sessionnumber=``
this will run whatever is within the backticks and save the output to a variable. You can then access the variable using the dollar notation:
echo "$sessionnumber"

managing user accounts by group name, username and last login linux

I created a script called monitornsuaccounts.sh that should append its output file to useraccountstatus.log. useraccountstatus.log is in the directory /var/local/nsu/logs/.
The output of this script should state every username and the following information about each username: username, last login, user home directory and associated groups. Preferably there should be columns with each information.
The command I use for the usernames is sudo cat /etc/passwd | grep ‘/home’. Last is to find the last login of each user. Groups is to the find the group of each user. When I run the command, the output file only shows the data I need for my current user rather than all users. Any recommendations that anyone has would be greatly appreciated.
#!/bin/bash
usernames=sudo cat /etc/passwd | grep ‘/home’
echo “$usernames” > /home/daniel/names.txt
mlast=$(cat names.txt | xargs -n1 last)
mgroup=$(cat names.txt | xargs -n1 groups)
cat names.txt > /var/local/nsu/logs/useraccountstatus.log
echo “$mlast” >>/var/local/nsu/logs/useraccountstatus.log
echo “$mgroup” >>/var/local/nsu/logs/useraccountstatus.log
There are a lot of issues in your script.
Your definition of users. Are you sure that this is what you want? For example: root does not have a directory under /home.
Watch your quotes. cat /etc/passwd | grep ‘/home’ returns nothing, while cat /etc/passwd | grep 'home' returns a list of stanzas in /etc/passwd
You'll probably want just a list of usernames, not a list of stanzas. Something along the line of
cat /etc/passwd | grep 'home' | sed 's/:.*//'
Why sudo in sudo cat /etc/passwd?
Look at your assignment in the
usernames=sudo cat /etc/passwd | grep ‘/home’
This does not make sense. You might try to do a
usernames=`sudo cat /etc/passwd | grep '/home'| sed 's/:.*//'`
And that is just the first line of the script.
Anyway, if your script does not work as intended, you will need to do some debugging. First question, especially if you are inexperienced, is "do the commands that I write give the result that I expect?" So in your case, you should have tried cat /etc/passwd | grep ‘/home’ and you would have seen that it does not give you the expected results. Even with the correct quotes, you'll get a list of stanzas, which is also not what you expected. Have you looked at /home/daniel/names.txt and was the content of the file what you wanted? I guess not: it was empty.
Just a quick hint, to get you started in the right direction (although there are still some issues and pepole might object to the backtics)
#!/bin/bash
usernames=`sudo cat /etc/passwd | grep '/home'| sed 's/:.*//'`
mlast=`echo $usernames | xargs -n1 last`
mgroup=`echo $usernames| xargs -n1 groups`
echo $usernames > /var/local/nsu/logs/useraccountstatus.log
echo "$mlast" >>/var/local/nsu/logs/useraccountstatus.log
echo "$mgroup" >>/var/local/nsu/logs/useraccountstatus.log
You will want to polish this and make the output more useful.

grep command to find the value of key from json

Please explain how to use grep command to extract some specified text from the following json -
{"result":"Customer information saved successfully with Customer id :59 and version :1","status":{"responseCode":200,"result":null,"responseMessage":"Success","responseType":"info"}}
After some googling, it looks like it is possible by using grep with a regular expression. But it is not working. Can you please point out the mistake
cat response.txt | grep -Po '(?<="Customer id ":)[0-9]+'
Assumption: response.txt contains the above json.
If yes please explain.
you missed K:
cat response.txt | grep -Po 'Customer id :\K[0-9]+'
59
screenshot, its working:
Try doing this :
$ jq -r '.result' json_file | grep -oP '(?<=Customer id :)\d+'
59
Check http://stedolan.github.io/jq/manual/

p4 list opened files that are shelved

I don't want the changelists; I want the actual files that are shelved. So the results should look like the results of p4 opened but filtered to show only the files that are shelved.
Anyone know how to do this in a few commands? (Preferably one)
I couldn't find a way to get it down to less than this, but maybe this will help as a starting point:
for cl in `p4 -ztag changes -u your.name -s shelved | \
grep -oP '(?<=^\.{3} change )\d+'`
do
p4 describe -Ss $cl | grep -E '^\.{3}'
done | sort | uniq
This does a p4 describe -S on each of your shelved changelists, but tries to only show the files. It might be a bit fragile. It should look like:
... //depot/yourfile.txt#4 edit
... //depot/otherfile.txt#5 edit

In Perforce, list all changesets by user

In Perforce, how do I list all changesets for a given user?
Can that be done via a single "p4" command?
Yes.
p4 changes -u <username>
In Powershell 2.0:
p4 users
| select-string "^\w+(.\w+)?" | %{$_.Matches} | %{$_.Value}
| %{p4 changes -u $_}
The first line shows all users, the second line parses out the username from the output, adn the third line sends that input to p4 changes.
EDIT: The regex assumes your usernames are either a single word or a firstname.lastname format. You may need to edit it for different formats.
EDIT2: Ooooh for a given user. Arse.
EDIT3: Shorter powershell:
p4 users
| select-string "^\w+(.\w+)?" | %{$_.Matches}
| %{p4 changes -u $_.Value }
EDIT4: even shorter powershell:
p4 users | % { p4 changes -u $_.Split()[0] }
For details of the changes for each changelist use:
p4 changes -u <user_name> | %{p4 describe $_.Split()[1]}
Use -s option for describe if you don't need the file diff.
p4 changes -m 1 -L -t -u

Resources