Extract flag and its value from string [duplicate] - bash

This question already has answers here:
How do I parse command line arguments in Bash?
(40 answers)
Closed 4 years ago.
I want to be able to extract a flag (-q) and its value from a string. An example:
<string1> -q <value> <string2>
Here, <string1> and <string2> can contain any set of characters. To clarify <string1> and <string2> are placeholders for any possible string.
I want two things:
Be able to get the string
<string1> <string2>
Be able to get the string <value>
I would also be able to do something similar for this input:
<string1> --all <string2>
where --all is extracted and <string1> <string2> is is kept.
Unlike the proposed solution in How do I parse command line arguments in Bash?, I want to be able to extract a flag and its value and then keep the original command line input without the flag and that value.

A quick and very simple solution using cut:
echo "<string1> -q value <string2>" | cut -d'>' -f2 | cut -d'<' -f1
outputs : -q value
NOTE this only works if whatever is between your string1 / string2 tags does not contain <>

Related

How to remove last n character from string in shell script [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Closed 21 days ago.
I have a variable reponame in Shell Script(bash) holding a string
echo $reponame
"testrepo.git"
I want to remove the last 4 character of this string and assign the result to a new variable repo
echo $repo
"testrepo"
How can I do this?
If I understand correctly, you want to get rid of the .git extension. Then the correct expression would be
repo=${reponame%.*}
or
repo=${reponame%.git}
for that very specific case.
For substrings in general, the expression removing last 4 characters would go like
repo=${reponame:0:-4}
Very nice resource on Bash string operations:
https://tldp.org/LDP/abs/html/string-manipulation.html
For shell in general you might use various approaches such as
repo=$(echo -n "$reponame" | sed 's/\.git$//')
or
repo=$(echo -n "$reponame" | rev | cut -f 2- -d '.' | rev)

How to add double quotation marks ("") around pipe-separated fields [duplicate]

This question already has answers here:
How can I add quotation marks to fields in a CSV file?
(4 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Let's say I have a file with this structure:
1|2|3|4|
5|6|7|8|
9|10|11|12|
However, I want my file to look like this (expected output):
"1"|"2"|"3"|"4"|
"5"|"6"|"7"|"8"|
"9"|"10"|"11"|"12"|
I am trying to used sed command in the following way:
sed 's/^/"/g'
Unfortunately, it only adds quotation marks at the beginning of each line:
"1|2|3|4|
"5|6|7|8|
"9|10|11|12|
^ means "the beginning of a line". Use [^|] instead which means "anything but |". If your implementation of sed supports +, you can use
sed -E 's/[^|]+/"&"/g'
otherwise, you need to be more verbose
sed 's/[^|][^|]*/"&"/g'
& represents the matched part.
You can use
sed -E 's/[^|]+/"&"/g' file > newfile
The -E option enables the POSIX ERE syntax and [^|]+ thus matches one or more chars other than |, and "&" replaces each with its copy enclosed with " on both sides.
See the online sed demo:
s='1|2|3|4|
5|6|7|8|
9|10|11|12|'
sed -E 's/[^|]+/"&"/g' <<< "$s"
Output:
"1"|"2"|"3"|"4"|
"5"|"6"|"7"|"8"|
"9"|"10"|"11"|"12"|
Here is a gnu awk way of doing the same:
awk -v RS="[|\n]+" '{ORS=RT; print "\"" $0 "\""}' file
"1"|"2"|"3"|"4"|
"5"|"6"|"7"|"8"|
"9"|"10"|"11"|"12"|

How to extract string between IDs in shell [duplicate]

This question already has answers here:
How to use sed/grep to extract text between two words?
(14 answers)
Closed 2 years ago.
I have a string that looks like this:
RG="#RG\tID:HS2000-1015_160.7\tDS:ADNI_1380^LP6005117-DNA_G04^ADNI_WGS\tLB:LP6005117-DNA_G04\tPL:illumina\tPU:HS2000-1015_160.7\tSM:ADNI_1380"
I want to extract everything after ID: and before first the \t
and get HS2000-1015_160.7 as a result. I would like a one-liner if possible.
code I tried:
echo ${RG} | grep -oP "(?<=ID:)[^"\t"]*"
which gives me HS2000-1015_160.7\
Try:
sed 's/.*ID://;s/\\t.*//'
s/.*ID:// remove everything in front and including ID:
s/\\t.*//' remove everything after and including \t characters.

How to get a string out of a plain text file [duplicate]

This question already has answers here:
How do I split a string on a delimiter in Bash?
(37 answers)
Closed 6 years ago.
I have a .txt file that has a list containing a hash and a password so it looks likes this:
00608cbd5037e18593f96995a080a15c:9e:hoboken
00b108d5d2b5baceeb9853b1ad9aa9e5:1c:wVPZ
Out of this txt file I need to extract only the passwords and add them in a new text file so that I have a list that would look like this:
hoboken
wVPZ
etc
etc
etc
etc
How to do this in bash, a scripting language or simply with a text editor?
Given your examples, the following use of cut would achieve what you want:
cut -f3 -d':' /folder/file >> /folder/result
The code above would delete anything before (and including) the second colon : on each line, which would work on your case, given your examples. The result is stored on /folder/result.
Edit: I edited this answer to make it simpler.
I suggest to use awk to get always last column from your file:
awk -F ':' '{print $NF}' file
Output:
hoboken
wVPZ
With sed, to remove the string up to ::
sed 's/.*://' file
You could also use grep:
$ grep -o [^:]*$ file
hoboken
wVPZ
-o print only matching part
[^:] anything but :
* all matching characters
$ end of record

Returning only a part of the string from a grep result [duplicate]

This question already has answers here:
Can grep show only words that match search pattern?
(15 answers)
Closed 6 years ago.
I'm using grep on text file containing some simple logs in the following form:-
[ABC.txt]
1=abc|2=def|3=ghi|4=hjk|5=lmn|6=opq
8=rst|9=uvx|10=wyz
.
.
.
.
and so on
the values for the tags 1,2,3,4 etc are different throughout the file and include special characters in some case too. Is there a way I can only retrieve the value for the tag 4 and no other tags via GREP?
BTW,this log file is itself a result of grep .So please advice if I should redirect the output first and then apply the second grep or apply the second grep over the first one,considering it's a large file.
grep -Po '(?<=4=)[^|]*' ABC.txt
You could pipe the result of grep to cut, split the fields by "|" and select the fourth field
[your grep command] | cut -d | -f 4
If you want the "4=" to be gone you can just do the same by using cut a second time but this time using "=" as a delimiter.
http://pubs.opengroup.org/onlinepubs/009695399/utilities/cut.html

Resources