How to use the -e/-E option for p4 labels - perforce

I tried to use "p4 labels" to retrieve labels with names of format "FOO_2.4.4". I tried the following list of things but none of them works. On the other hand "p4 labels .... | grep FOO" did get me the labels. Is there anything I missed?
p4 labels -e FOO
p4 labels -e FOO ....
p4 labels -e FOO //comms/path_to_directory_containing_files_in_label/...
p4 labels -e "FOO"
p4 labels -e "FOO" ....
p4 labels -e "FOO" //comms/path_to_directory_containing_files_in_label/...
p4 labels -E FOO
p4 labels -E FOO ....
p4 labels -E FOO //comms/path_to_directory_containing_files_in_label/...
p4 labels -E "FOO"
p4 labels -E "FOO" ....
p4 labels -E "FOO" //comms/path_to_directory_containing_files_in_label/...
p4 labels -e FOO*
p4 labels -e FOO* ....
p4 labels -e FOO* //comms/path_to_directory_containing_files_in_label/...
p4 labels -e "FOO*"
p4 labels -e "FOO*" ....
p4 labels -e "FOO*" //comms/path_to_directory_containing_files_in_label/...
p4 labels -E FOO*
p4 labels -E FOO* ....
p4 labels -E FOO* //comms/path_to_directory_containing_files_in_label/...
p4 labels -E "FOO*"
p4 labels -E "FOO*" ....
p4 labels -E "FOO*" //comms/path_to_directory_containing_files_in_label/...

I'm not sure what's going wrong. It may be an issue with quoting. On my Windows command line shell, I have to use double quotes, not single quotes:
C:\Users\Bryan>p4 tag -l FOO_2.4.4 //...
//depot/a#1 - added
C:\Users\Bryan>p4 labels -e 'FOO*'
C:\Users\Bryan>p4 labels -e "FOO*"
Label FOO_2.4.4 2014/09/03 'Created by Bryan.'
But it seems that you have tried several combinations of quotes with no success.
Did you also try -E? That makes the matching case-insensitive.

Related

No files to submit even after I add them to perforce changlist

I get a changelist number and I am able to add the files to the changelist
>>> createdCLNumber
'1157545'
>>> p4.run_add("-c", createdCLNumber, "/Users/ciasto/ciasto_piekarz/sandbox/main/upgrade_tools/upgrade_gitlab")
['//depot/td/main/bin/upgrade_gitlab#1 - currently opened for add']
but when I try to submit them I get the error.
>>> p4.run_submit(changeList)
P4.P4Exception: [P4#run] Errors during command execution( "p4 submit -i" )
[Error]: 'No files to submit.'
If you want to submit a numbered changelist (implied by the fact that you used p4 add -c CHANGELIST), do:
p4 submit -c CHANGELIST
See p4 help submit:
submit -- Submit open files to the depot
p4 submit [-Af -r -s -f option --noretransfer 0|1]
p4 submit [-Af -r -s -f option] file
p4 submit [-Af -r -f option] -d description
p4 submit [-Af -r -f option] -d description file
p4 submit [-Af -r -f option --noretransfer 0|1] -c changelist#
p4 submit -e shelvedChange#
p4 submit -i [-Af -r -s -f option]
--parallel=threads=N[,batch=N][,min=N]
If you only specify a single argument, it's interpreted as a file path:
p4 submit [options] file
To specify a changelist you want this form:
p4 submit [options] -c changelist#

How To Delete A File Every X Times A Script Is Run - Manage A Log File From Inside A Script?

I would normally just schedule this as a cron job or script, however, I would like to delete a log file (it's constantly appended to every time a script runs) only after 50 times.
Needed Inside The Script:
The thing is, since the script does not run consistently, it has be to be implemented within the script itself. Please note: for various reasons, I need this inside the script.
What I Was Trying:
I was thinking of setting a variable to increment, outputting it to a file and then having the script read that file every time. Then, if that value is greater than X, remove the file. That portion of the code would be a grep or awk statement.
Anyone know an easy, better way to do this? Your positive input is highly appreciated.
Since the Gnu awk v.4.1 the inplace edit has been available
(see awk save modifications in place) so, you could store the counter to your awk script variable and use the awk script to edit itself and decrement the counter varible like this:
$ cat program.awk
BEGIN {
this=5 # the counter variable
}
/this=[0-9]+/ { # if counter (this=5) matches
if(this==0) # if counter is down to 0...
; # ... do what you need to do
split($0,a,"=") # split "this=5" by the "="
sub(/=[0-9]+$/,"=" (a[2]==0?5:a[2]-1)) # decrement it or if 0 set to 5 again
}
1 # print
Run it:
$ awk -i inplace -f program.awk program.awk
$ head -3 program.awk
BEGIN {
this=4 # the counter variable
}
Basically you run program.awk that changes one record in program.awk inplace and once counter hits 0, the if gets executed.
You could use xattr to associate arbitrary metadata with a file, like this:
touch a.txt # Create file
xattr -w runs 1 a.txt # Set run count to 1
xattr -p runs a.txt # Print run count
1
xattr -w runs 49 a.txt # Change value
xattr -l a.txt # List all attributes
runs: 49
The beauty of that is it doesn't show up in grep or when looking at the file with normal tools.
Note that not all filesystems (e.g. Microsoft FAT) will support xattr.
Using sed in the code below it will increment x by 1 within the script, each time the script is run, up to 50 and then set x back to 1. You can set the command to process the logfile in the else branch of the if statement along with whatever other code you want to run in each branch.
#!/bin/bash
x=1
y=$((x+1))
z=1
if [ $x -lt 50 ]; then
# Do something...
sed -i -e "s/x=$x/x=$y/" "$0"
else
# Do something...
# Delete logfile...
sed -i -e "s/x=$x/x=$z/" "$0"
fi
Here I run the script to show x gets incremented and reset back to 1 after 50 runs:
$ cat testscript
#!/bin/bash
x=1
y=$((x+1))
z=1
if [ $x -lt 50 ]; then
# Do something...
sed -i -e "s/x=$x/x=$y/" "$0"
else
# Do something...
# Delete logfile...
sed -i -e "s/x=$x/x=$z/" "$0"
fi
$ ./testscript
$ cat testscript
#!/bin/bash
x=2
y=$((x+1))
z=1
if [ $x -lt 50 ]; then
# Do something...
sed -i -e "s/x=$x/x=$y/" "$0"
else
# Do something...
# Delete logfile...
sed -i -e "s/x=$x/x=$z/" "$0"
fi
$
As you can see x=1 has became x=2 within the script.
I now manually set x=2 to x=50 and saved the script to show it resets to x=1.
$ cat testscript
#!/bin/bash
x=50
y=$((x+1))
z=1
if [ $x -lt 50 ]; then
# Do something...
sed -i -e "s/x=$x/x=$y/" "$0"
else
# Do something...
# Delete logfile...
sed -i -e "s/x=$x/x=$z/" "$0"
fi
$ ./testscript
$ cat testscript
#!/bin/bash
x=1
y=$((x+1))
z=1
if [ $x -lt 50 ]; then
# Do something...
sed -i -e "s/x=$x/x=$y/" "$0"
else
# Do something...
# Delete logfile...
sed -i -e "s/x=$x/x=$z/" "$0"
fi
$
As mentioned, my main requirement is to do this inside the script or in-line. As #EvansWinner mentioned, I can manage the file by using properties like the file size or age. However, I went with something even more simple, using number of lines.
sed -i '100000,$ d' file.txt
Thus, I don't have to worry about how many times it runs. Hopefully, for anyone who is trying to also delete a file every x times, within a script, this will help you look at the file properties and how they can be managed using size, age or as I have used, number of lines. These solutions are much more portable than creating programs or packages that are required on other systems.

OR condition in Shell Scripting - Unix

I declare three variables.
$1=`ssh <server_1> cat /etc/passswd|cut -f -d:|grep -e $IID -e $EID`
$2=`ssh <server_2> cat /etc/shadow|cut -f -d:|grep -e $IID -e $EID`
$3=`ssh <server_3> cat /etc/passwd}|cut -f -d:|grep -i $CID`
The above three variables are created by taking ssh to servers and checking the presence of the IDs which I give as input. If the ID doesn't exist already, the the variable is going to be null.
Now, how do I verify if all the three variables are null. I wanted to use the OR condition specified within an IF.
I tried,
if [ -s "$1" -o -s "$2" -o -s "$3"];then
echo -$1 $2 $3 "already exist(s)"
It didnt work. Please advise.
PS: I have just begun my career in Unix and correct me If am wrong anywhere.
Several points.
When you assign to a variable, don't use the dollar sign:
foo=xxx
Variables $1, $2 etc are already used for your command line arguments. Pick other names. But not $4please. :-)
When you specify a command for ssh, and it has arguments, it has to be quoted, because the command needs to be a single argument for ssh. In your case use double quotes, as you want variable expansion for $IID etc.
Most Unix utils are able to open input files themselves, so you don't need to start your pipeline with cat.
foo=`ssh <server_1> "cut -f -d: /etc/passwd | grep -e $IID -e $EID"`
Or something like that.
It was a typo in my question. I had actually declared it as,
1=`ssh <server_1> cat /etc/passswd|cut -f -d:|grep -e $IID -e $EID`
2=`ssh <server_2> cat /etc/shadow|cut -f -d:|grep -e $IID -e $EID` and so on.
And I tried it as ,
if [ -s "$1" -o -s "$2" -o -s "$3"];then
echo -e $1 $2 $3 "already exist(s)"
Since I had to Deliver my script today, I used the conventional method of,
ssh <server_1> "cat /etc/passswd|cut -f -d:|grep -e $IID -e $EID" > file1
ssh <server_2> "cat /etc/shadow|cut -f -d:|grep -e $IID -e $EID" > file2
ssh <server_3> "cat /etc/passwd|cut -f -d:|grep -ix $CID" > file3
if [ -s file1 -o -s file2 -o -s file3]; then
for i in `cat file1 file2 file3`
do
echo $i "already exists"
done
else
And I have now learnt from my first post, that -s to ensure that a file is not empty and -z is to ensure string is empty.

Find and highlight text in linux command line

I am looking for a linux command that searches a string in a text file,
and highlights (colors) it on every occurence in the file, WITHOUT omitting text lines (like grep does).
I wrote this handy little script. It could probably be expanded to handle args better
#!/bin/bash
if [ "$1" == "" ]; then
echo "Usage: hl PATTERN [FILE]..."
elif [ "$2" == "" ]; then
grep -E --color "$1|$" /dev/stdin
else
grep -E --color "$1|$" $2
fi
it's useful for stuff like highlighting users running processes:
ps -ef | hl "alice|bob"
Try
tail -f yourfile.log | egrep --color 'DEBUG|'
where DEBUG is the text you want to highlight.
command | grep -iz -e "keyword1" -e "keyword2" (ignore -e switch if just searching for a single word, -i for ignore case, -z for treating as a single file)
Alternatively,while reading files
grep -iz -e "keyword1" -e "keyword2" 'filename'
OR
command | grep -A 99999 -B 99999 -i -e "keyword1" "keyword2" (ignore -e switch if just searching for a single word, -i for ignore case,-A and -B for no of lines before/after the keyword to be displayed)
Alternatively,while reading files
grep -A 99999 -B 99999 -i -e "keyword1" "keyword2" 'filename'
command ack with --passthru switch:
ack --passthru pattern path/to/file
I take it you meant "without omitting text lines" (instead of emitting)...
I know of no such command, but you can use a script such as this (this one is a simple solution that takes the filename (without spaces) as the first argument and the search string (also without spaces) as the second):
#!/usr/bin/env bash
ifs_store=$IFS;
IFS=$'\n';
for line in $(cat $1);
do if [ $(echo $line | grep -c $2) -eq 0 ]; then
echo $line;
else
echo $line | grep --color=always $2;
fi
done
IFS=$ifs_store
save as, for instance colorcat.sh, set permissions appropriately (to be able to execute it) and call it as
colorcat.sh filename searchstring
I had a requirement like this recently and hacked up a small program to do exactly this. Link
Usage: ./highlight test.txt '^foo' 'bar$'
Note that this is very rough, but could be made into a general tool with some polishing.
Using dwdiff, output differences with colors and line numbers.
echo "Hello world # $(date)" > file1.txt
echo "Hello world # $(date)" > file2.txt
dwdiff -c -C 0 -L file1.txt file2.txt

How to create a script to add sed command into a file (bash script)

I have .csv file that contain 2 columns delimited with ,.
file.csv
word1,word2
word3,word4
word5,word6
.
.
.
.
word1000,1001
I want to create a new file from file.csv and insert sed command like this:
mynewfile
sed -e 's,word1,word2,gI' \
-e 's,word3,word4,gI' \
-e 's,word5,word6,gI' \
....
How can I make a script to add sed command?
You can use sed to process each line:
echo -n 'sed ' ; sed -e "s/^\(.*\)/-e 's,\1,gl'\ \\\/" file.csv
will produce as requested
sed -e 's,word1,word2,gl' \
-e 's,word3,word4,gl' \
-e 's,word5,word6,gl' \
Your goal seams to be performing custom replacements from a file. In this case, I would not generate a file containing a bash script to do the job, but I would generate a sed script to do the job:
sed -e 's/^/s,/' -e 's/$/,gI/' file.csv > sed_script
sed -f sed_script <<< "word1"
We can even avoid to use the sed_script file with bash's process substitution:
sed -f <(sed -e 's/^/s,/' -e 's/$/,gI/' file.csv) <<< "word1"
Update:
Simplifying the sed script generation, it becomes:
sed -e 's/.*/s,&,gI/' file.csv > sed_script
sed -f sed_script <<< "word1"
and
sed -f <(sed -e 's/.*/s,&,gI/' file.csv) <<< "word1"

Resources