How to split correctly when using bash script - bash

I have a bash command which the result is something like this in my file:
sm-apolo: 2.1-835
sql-apolo: 1.0-2
srmq-processor: 1.0-214
I want to get a result of all items that have sql at the start and only the first part before ":" so need the second part to be removed. Then I want to use the result in choiceParam in Jenkins pipeline to be shown as a drop-down list.
If I run something like this:
names = sh (script: "A command here | grep sql", returnStdout: true).trim()
names_list = names.trim().tokenize("\n")
In the drop-down I get
sql-apolo: 1.0-2
How I can get rid of everything after the name including ": 1.0-2" to show up in the drop down list? something like:
sql-apolo
sql-sxdf
sql-pokf

With GNU grep and a Perl-compatible regular expression (-P):
grep -Po '^sql.*(?=:)'
or:
grep -o '^sql[^:]*'
See: man grep and The Stack Overflow Regular Expressions FAQ

Related

Grep Search in Shell Script

Trying to implement a system which is based on shell script and PHP. Getting the string from PHP file and processing through shell scripts.
Every time it's working except some time where strings are like : "/jobs?location_country=united+states&sort_by=cfml10%2cdesc&v_location=usa"
grep for this command not working.
How to solve this?
Code is :
hcm=$(php largest.php "$file"_hcm_input.txt "$remove")
echo "$hcm"
grep "$hcm" "$file"_sorted.txt > "$file"_jobs.txt
Use grep -F or grep --fixed-strings to tell grep to treat the argument as a fixed string rather than a regex.
As you have & in the string, even if you enclose it in single/double quotes it won't work.
In this case you have to escape &, using \ like \&.
After getting lot hit and trial found out that this is not the issue with & = or + as i have checked individually
grep "=" s37_sorted.txt
grep "&" s37_sorted.txt
grep "+" s37_sorted.txt
Giving me output.
Exact reason was the case. So we need to find with case insensitive manner for that we have to follow the following code and parameter with grep is -i
hcm=$(php largest.php "$file"_hcm_input.txt "$remove")
echo "Highest Common String:"$hcm
grep -i "$hcm" "$file"_sorted.txt > "$file"_jobs.txt
Now it's showing me 366 records.

Search all occurences of a instance ids in the variable

I have a bash variable which has the following content:
SSH exit status 255 for i-12hfhf578568tn
i-12hdfghf578568tn is able to connect
i-13456tg is not able to connect
SSH exit status 255 for 1.2.3.4
I want to search the string starting with i- and then extract only that instance id. So, for the above input, I want to have output like below:
i-12hfhf578568tn
i-12hdfghf578568tn
i-13456tg
I am open to use grep, awk, sed.
I am trying to achieve my task by using following command but it gives me whole line:
grep -oE 'i-.*'<<<$variable
Any help?
You can just change your grep command to:
grep -oP 'i-[^\s]*' <<<$variable
Tested on your input:
$ cat test
SSH exit status 255 for i-12hfhf578568tn
i-12hdfghf578568tn is able to connect
i-13456tg is not able to connect
SSH exit status 255 for 1.2.3.4
$ var=`cat test`
$ grep -oP 'i-[^\s]*' <<<$var
i-12hfhf578568tn
i-12hdfghf578568tn
i-13456tg
grep is exactly what you need for this task, sed would be more suitable if you had to reformat the input and awk would be nice if you had either to reformat a string or make some computation of some fields in the rows, columns
Explanation:
-P is to use perl regex
i-[^\s]* is a regex that will match literally i- followed by 0 to N non space character, you could change the * by a + if you want to impose that there is at least 1 char after the - or you could use {min,max} syntax to impose a range.
Let me know if there is something unclear.
Bonus:
Following the comment of Sundeep, you can use one of the improved versions of the regex I have proposed (the first one does use PCRE and the second one posix regex):
grep -oP 'i-\S*' <<<$var
or
grep -o 'i-[^[:blank:]]*' <<<$var
You could use following too(I tested it with GNU awk):
echo "$var" | awk -v RS='[ |\n]' '/^i-/'
You can also use this code (Tested in unix)
echo $test | grep -o "i-[0-z]*"
Here,
-o # Prints only the matching part of the lines
i-[0-z]* # This regular expression, matches all the alphabetical and numerical characters following 'i-'.

some arguments not work from AppleScript to grep thru do shell script

in applescript editor:
do shell script "grep -w 'SomeText' /tmp/test"
ignores -w
in Bash:
grep -w 'SomeText' /tmp/test
not ignores arguments
But for example arguments -v (negative) works in AppleScript with do shell script
it is happening on both different computers with different systems
how i can use -w argument in grep from applescript?
Thanks!
Regardless of where I run the grep -w ... command from, Terminal or ApplesScript's do shell script command, I get identical output.
The manual page for the -w option in grep states the following:
−w, −−word-regexp
The expression is searched for as a word (as if surrounded by ‘[[:<:]]’ and ‘[[:>:]]’; see re_format(7)).
The manual page for re_format states:
There are two special cases‡ of bracket expressions: the bracket expressions [[:<:]] and [[:>:]] match the null string at the beginning and end of a word respectively. A word is defined as a sequence of word characters which is neither preceded nor followed by word characters. A word character is an alnum character (as defined by ctype(3)) or an underscore.
In Terminal:
Contents of /tmp/test:
$ cat /tmp/test
SomeText
MoreText
ASomeTextZ
Other Text
0 SomeText 1
$
Using grep without -w on /tmp/test:
$ grep 'SomeText' /tmp/test
SomeText
ASomeTextZ
0 SomeText 1
$
As it should, grep finds all three lines containing 'SomeText'.
Using grep with -w on /tmp/test:
$ grep -w 'SomeText' /tmp/test
SomeText
0 SomeText 1
$
As it should, grep -w finds only the lines conforming to what's stated in the manual page excerpts shown above. In this case, only two of the three lines that contain 'SomeText'.
The output of each grep command, show above, when wrapped in a do shell script command in AppleScript are identical, as should be.
In Script Editor:
Because these are the expected results is why I'm adamant about following How to create a Minimal, Complete,and Verifiable example, when asking questions such that you have, in the manner you have!
I'd suggest you show us the actual content of your /tmp/test file and the actual output you get from each of the grep commands, with and without the -w option, from both Terminal and AppleScript's do shell script command.
Although it shouldn't make a difference, nonetheless you should also provide macOS version info so we can test this under the actual version of macOS you're using, so as to see if that's a relevant factor in the equation.

Defining a variable using head and cut

might be an easy question, I'm new in bash and haven't been able to find the solution to my question.
I'm writing the following script:
for file in `ls *.map`; do
ID=${file%.map}
convertf -p ${ID}_par #this is a program that I use, no problem
NAME=head -n 1 ${ID}.ind | cut -f1 -d":" #Now: This step is the problem: don't seem to be able to make a proper NAME function. I just want to take the first column of the first line of the file ${ID}.ind
It gives me the return
line 5: bad substitution
any help?
Thanks!
There are a couple of issues in your code:
for file in `ls *.map` does not do what you want. It will fail e.g. if any of the filenames contains a space or *, but there's more. See http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29 for details.
You should just use for file in *.map instead.
ALL_UPPERCASE names are generally used for system variables and built-in shell variables. Use lowercase for your own names.
That said,
for file in *.map; do
id="${file%.map}"
convertf -p "${id}_par"
name="$(head -n 1 "${id}.ind" | cut -f1 -d":")"
...
looks like it would work. We just use $( cmd ) to capture the output of a command in a string.

Using grep (on Windows) to find a string contained by 's

I'm trying to write a shell script in windows, which is why I'm not using something like awk or grep -o etc.
I'm trying to parse my angular files for the controllers being used. For example, I'll have a line like this in a file.
widgetList.controller('widgetListController', [
What I want is to pull out widgetListController
Here's what I've got so far:
grep -h "[[:alpha:]]*Controller[[:alpha:]]*" C:/workspace/AM/$file | tr ' ' '\n' | grep -h "[[:alpha:]]*Controller[[:alpha:]]*"
It works decently well, but it will pull out the entire line like so:
widgetList.controller('widgetListController', rather than just the word.
Also in instances where the controller is formatted as so:
controller : 'widgetListController',
It returns 'widgetListController',
How can I adjust this to simply return whatever is between the 's? I've tried various ways of escaping that character but it doesn't seem to be working.
You can use this sed command:
sed "/Controller/s/.*'\([^']*\)'.*$/\1/" C:/workspace/AM/$file
Output:
widgetListController

Resources