Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
linux:misc [2025/10/07 15:20] – [Manipulating Strings] tmadelinux:misc [2026/06/12 11:07] (current) – [Set variable] tmade
Line 35: Line 35:
 } }
 </code> </code>
 +
 +====yq====
 +yq - commandline YAML processor, to visualize yaml output in a readable way
 +
 +Example to replace a openshift-route with yq:
 +
 +  oc get route/${route} -o yaml | yq eval '.spec.tls.certificate = strenv(TLSCERT) | .spec.tls.key = strenv(TLSKEY)' - | oc replace -f -
  
 ====Manipulating Strings==== ====Manipulating Strings====
Line 45: Line 52:
 Ueb="String1_String2_String3" Ueb="String1_String2_String3"
  
-Ersten wegnehmen:+Remove 1. string:
 Ueb1=${Ueb#*_} Ueb1=${Ueb#*_}
 +# echo $Ueb1
 String2_String3 String2_String3
  
-Ersten behalten:+Keep 1. string:
 Ueb1=${Ueb%%_*} Ueb1=${Ueb%%_*}
 +# echo $Ueb1
 String1 String1
  
-Letzten wegnehmen:+Remove last string:
 Ueb1=${Ueb%_*} Ueb1=${Ueb%_*}
 +# echo $Ueb1
 String1_String2 String1_String2
  
-Letzten behalten:+Keep last string:
 Ueb1=${Ueb##*_} Ueb1=${Ueb##*_}
 +# echo $Ueb1
 String3 String3
 </code> </code>
 +
 +Another example:
 +<code>
 +# echo ${TEST:-Foo}      #if "TEST" is empty you are getting default value
 +Foo
 +# echo $TEST
 +# echo ${TEST:=Foo}      #if "TEST" is empty it will be set 
 +Foo
 +# echo $TEST
 +Foo
 +</code>
 +
 +====Set variable====
 +Alternative to "sed":
 +
 +  var="oldvalue"
 +  var="${var/oldvalue/newvalue}"
 +  echo $var
 +  newvalue
 +
 +
 +====awk====
 +Examples:
 +<code>
 +# echo "somestring.or.url.com somestring1.or.url1.com" | awk '{print $1".to.add.anotherstring "$2".and.one.more"}'
 +somestring.or.url.com.to.add.anotherstring somestring1.or.url1.com.and.one.more
 +</code>
 +
 +  awk -F: /my-user/'{print $3}' /etc/passwd                                                                      #getting uid of user "my-user"
 +  java -version 2>&1 | awk -F '-' 'NR==2 {print $2}'                                                             #searching for "-" in string and cut all in between, but just from line 2 (NR==2) as the result has 3 lines
 +  ifconfig -a | awk '/inet 10/{print $2}'                                                                        #getting IP adress starting with "inet 10"
 +  echo "somestring1 somestring2" | awk '{print $1,$2}'
 +  awk -F ':' '{print $1 "\t" $4}' /etc/passwd                                                                    #print all username + uid
 +  awk 'length($0) > 18' marks.txt                                                                                #print only those lines that contain more than 18 characters
linux/misc.1759843217.txt.gz · Last modified: by tmade
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki