Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

0 votes
1 answers
1968 views
'expect' not matching server response string
I'm using a bash script to log into a telnet server and execute a number of commands. It looks like: **login_and_run.sh** #!/bin/bash unset TELNET_USER_NAME_STRING unset TELNET_PASSWORD_STRING unset TELNET_USER_NAME unset TELNET_PASSWORD TELNET_USER_NAME_STRING=`cat SAP_output` TELNET_PASSWORD_STRIN...
I'm using a bash script to log into a telnet server and execute a number of commands. It looks like: **login_and_run.sh** #!/bin/bash unset TELNET_USER_NAME_STRING unset TELNET_PASSWORD_STRING unset TELNET_USER_NAME unset TELNET_PASSWORD TELNET_USER_NAME_STRING=cat SAP_output TELNET_PASSWORD_STRING="Password:" TELNET_USER_NAME="UserNam3\r" TELNET_PASSWORD="Passw0rd\r" # Expect script starts here expect <<- DONE spawn telnet localhost 50008 expect '$TELNET_USER_NAME_STRING' send "$TELNET_USER_NAME" sleep 3 expect "$TELNET_PASSWORD_STRING" send "$TELNET_PASSWORD" sleep 3 spawn ls expect eof DONE where **SAP_output**: Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. *********************************************** ********************************************** ****###*******####*****#######************** **##***##****##**##****##****##************ ***##*******##****##***##****##********** *****##*****########***######*********** ******##****##****##***##************* **##***##**##******##**##************ ****###****##******##**##********** ********************************** ******************************** Telnet Administration SAP Java EE Application Server v7.50 User name: telnet logs in, I get the banner, but it stops there (as if the strings are not matching). Would it be safer to use wildcards instead of the exact response (and match only " User name: ")?
Sebi (1029 rep)
Aug 15, 2019, 02:40 PM • Last activity: Jun 18, 2025, 03:04 PM
1 votes
1 answers
249 views
Sed delete everything after a word
I'm new to sed, I have a string lightdm --session-child 14 21 the two numbers can change and I would remove everything after lightdm I tried various way but none worked sed 's/ --session-child*//' sed 's/\ --session-child*//' sed 's/ --session-child*$//' and others, but none worked where is wrong ?
I'm new to sed, I have a string lightdm --session-child 14 21 the two numbers can change and I would remove everything after lightdm I tried various way but none worked sed 's/ --session-child*//' sed 's/\ --session-child*//' sed 's/ --session-child*$//' and others, but none worked where is wrong ?
klatls (33 rep)
Mar 15, 2025, 09:14 PM • Last activity: Mar 15, 2025, 09:25 PM
2 votes
1 answers
109 views
Make parameter substitution in newline-separated string more efficient
The following code should demonstrate and help with testing inefficient `Pattern Matching` expressions in a `Parameter Substitution` for a newline-separated strings var vs. array. The goal is to achieve at least on-par performance, as compared to `grep`, when filtering for only `git status -s` resul...
The following code should demonstrate and help with testing inefficient Pattern Matching expressions in a Parameter Substitution for a newline-separated strings var vs. array. The goal is to achieve at least on-par performance, as compared to grep, when filtering for only git status -s results, that involve index changes (fully or partially staged). So basically, every change entry that starts with a Git short status flag char like [MTARDC] (signalling a staged/index change), including double-flags (signalling partially staged changes in index and worktree), except untracked or unstaged-only changes (beginning with [ ?]). Note, that R (rename) change flags can be followed by a multiple digits, see also examples in the test data below (possibly even for both index and worktree, so i. e. R104R104). See: short format of Git status The test data also contains file names with potentially problematic special chars, like escape sequences, space or ampersand: [\ $*"]. Also note, that the substitution based on Pattern Matching requires a negation of patterns, as compared to a RegEx for grep with the same results. To print the results, simply comment out the &>/dev/null parts. #! /bin/bash # Set extended pattern matching active shopt -s extglob clear unset -v tmpVar tmpArr # Populate tmpVar and tmpArr for testing for i in {1..3}; do tmpVar+=' A addedW1'[$i]$'\n' tmpVar+='A addedI1'[$i]$'\n' tmpVar+='AM addedA1'[$i]$'\n' tmpVar+=' C copiedW1'[$i]$'\n' tmpVar+='C copiedI1'[$i]$'\n' tmpVar+='CR copied A1'[$i]$'\n' tmpVar+=' D removedW1'[$i]$'\n' tmpVar+='D removedI1'[$i]$'\n' tmpVar+='DM removedA1'[$i]$'\n' tmpVar+=' M modifiedW1'[$i]$'\n' tmpVar+='M modifiedW1'[$i]$'\n' tmpVar+='MR modifiedA1'[$i]$'\n' tmpVar+=' R101 renamedW1'[$i]$'\n' tmpVar+='R102 renamedI2'[$i]$'\n' tmpVar+='R103D renamedA1'[$i]$'\n' tmpVar+=' T typeChangedW1'[$i]$'\n' tmpVar+='T typeChangedI1'[$i]$'\n' tmpVar+='TM typeChangedA1'[$i]$'\n' tmpVar+='?? exec2.bin'[$i]$'\n' tmpVar+='?? file1.txt'[$i]$'\n' tmpVar+='?? test.launch2'[$i]$'\n' tmpVar+='A file00 0.bin'[$i]$'\n' tmpVar+='A file11*1.bin'[$i]$'\n' tmpVar+='A file22\03457zwei.bin'[$i]$'\n' tmpVar+='A file33\t3.bin'[$i]$'\n' tmpVar+='A file44$4.bin'[$i]$'\n' tmpVar+='A file55"$(echo EXE)"5.bin'[$i]$'\n' tmpVar+='M exec1.bin'[$i]$'\n' tmpVar+=' M test.launch1'[$i]$'\n' tmpVar+=' M myproject/src/main/java/util/MyUtil.java'[$i]$'\n' tmpVar+='M myproject/src/test/util/MyUtilTest.java'[$i]$'\n' tmpVar+='R104R104 myproject/src/test/util/MyUtil2Test.java'[$i]$'\n' tmpVar+=' A invalidAdd'[$i]$'\n' tmpVar+='R invalidRename'[$i]$'\n' tmpArr+=(" A addedW1[$i]") tmpArr+=("A addedI1[$i]") tmpArr+=("AM addedA1[$i]") tmpArr+=(" C copiedW1[$i]") tmpArr+=("C copiedI1[$i]") tmpArr+=("CR copied A1[$i]") tmpArr+=(" D removedW1[$i]") tmpArr+=("D removedI1[$i]") tmpArr+=("DM removedA1[$i]") tmpArr+=(" M modifiedW1[$i]") tmpArr+=("M modifiedW1[$i]") tmpArr+=("MR modifiedA1[$i]") tmpArr+=(" R101 renamedW1[$i]") tmpArr+=("R102 renamedI2[$i]") tmpArr+=("R103D renamedA1[$i]") tmpArr+=(" T typeChangedW1[$i]") tmpArr+=("T typeChangedI1[$i]") tmpArr+=("TM typeChangedA1[$i]") tmpArr+=("?? exec2.bin[$i]") tmpArr+=("?? file1.txt[$i]") tmpArr+=("?? test.launch2[$i]") tmpArr+=("A file00 0.bin[$i]") tmpArr+=("A file11*1.bin[$i]") tmpArr+=("A file22\03457zwei.bin[$i]") tmpArr+=("A file33\t3.bin[$i]") tmpArr+=("A file44$4.bin[$i]") tmpArr+=('A file55"$(echo EXE)"5.bin['$i']') tmpArr+=("M exec1.bin[$i]") tmpArr+=(" M test.launch1[$i]") tmpArr+=(" M myproject/src/main/java/util/MyUtil.java[$i]") tmpArr+=("M myproject/src/test/util/MyUtilTest.java[$i]") tmpArr+=("R104R104 myproject/src/test/util/MyUtil2Test.java[$i]") tmpArr+=(" A invalidAdd[$i]") tmpArr+=("R invalidRename[$i]") done # Perf-test array or string var filtering via grep _IFS="$IFS"; IFS=$'\n' startTime="$EPOCHREALTIME" grep '^[MTARDC]' /dev/null stopTime="$EPOCHREALTIME" IFS="$_IFS" echo awk 'BEGIN { printf "ELAPSED TIME via grep filtering from ARRAY: "; print '"$stopTime"' - '"$startTime"' }' # Perf-test array filtering via Pattern Matching in Parameter Substitution startTime="$EPOCHREALTIME" printf '%s\n' "${tmpArr[@]/#[? ][?MTARDC]*([0-9]) *}" &>/dev/null stopTime="$EPOCHREALTIME" echo awk 'BEGIN { printf "ELAPSED TIME via parameter substitution from ARRAY: "; print '"$stopTime"' - '"$startTime"' }' # Perf-test string var filtering via Pattern Matching in Parameter Substitution startTime="$EPOCHREALTIME" printf '%s\n' "${tmpVar//[? ][?MTARDC]*([0-9]) *([^$'\n'])?($'\n')}" &>/dev/null stopTime="$EPOCHREALTIME" echo awk 'BEGIN { printf "ELAPSED TIME via parameter substitution from VAR: "; print '"$stopTime"' - '"$startTime"' }' # RESULT: #ELAPSED TIME via grep filtering from ARRAY: 0.054975 #ELAPSED TIME via parameter substitution from ARRAY: 0.00031805 #ELAPSED TIME via parameter substitution from VAR: 4.546 As can be seen, grep is good, but variant #2 (array filtering via Parameter substitution) is way faster, so for huge arrays, there's a good alternative to grep. Var string filtering via Parameter Substitution, on the other hand is terribly slow. Mostly due to the fact that the matching pattern cannot end with * (which would remove everything to the string's end from the first match), but because it needs *([^$'\n'])?($'\n') instead, in order to match (and remove) everything in a match, up to the next newline and, to some extent, due to the tmpVar// greedy matching. Is there another way/pattern for the example, to process var strings with Pattern Matching, likewise to array - without using the problematic and slowing newline-negation char matching and to get near the speed of the array example?
fozzybear (59 rep)
Jan 23, 2025, 06:19 PM • Last activity: Mar 8, 2025, 06:03 AM
2 votes
4 answers
7033 views
Delete files with specific content
Having a thousand of data files (same extension) in a directory, how to delete those which contain a specific parameter statement? For example, if I wented to delete all files containing the line ``` Key = 0 ``` could I do something like ``` grep -i "Key = 0" * | rm * ```
Having a thousand of data files (same extension) in a directory, how to delete those which contain a specific parameter statement? For example, if I wented to delete all files containing the line
Key = 0
could I do something like
grep -i "Key = 0" * | rm *
Andr&#233; Fonseca (31 rep)
Aug 11, 2020, 07:11 PM • Last activity: Feb 16, 2025, 02:42 PM
0 votes
4 answers
125 views
How to replace pattern in last line
I'm trying to replace `"},"` by `"}"` from the last line of a file : My `/etc/firefox/policies/policies.json` file like this because I added the certificate lines using a `for` loop : ```shell $ cat /etc/firefox/policies/policies.json { "policies": { "Certificates": { "Install": [ {"cert1.der", "/ho...
I'm trying to replace "}," by "}" from the last line of a file : My /etc/firefox/policies/policies.json file like this because I added the certificate lines using a for loop :
$ cat /etc/firefox/policies/policies.json
{
  "policies": {
    "Certificates": {
      "Install": [
        {"cert1.der", "/home/username/cert1.pem"},
        {"cert2.der", "/home/username/cert2.pem"},
$
Please note there no "\n" at the end of this file (yet). Before adding this to the end of my JSON file :
]
    }
  }
}
, how can I remove the last , : replacing "}," by "}" for example ? sed seems a complicated solution to do that according to [this solution](https://stackoverflow.com/a/17115550) . Is there a more readable solution using awk or perl ?
SebMa (2433 rep)
Sep 5, 2024, 05:47 PM • Last activity: Sep 6, 2024, 03:45 PM
29 votes
2 answers
4541 views
What is the difference between `a[bc]d` (square brackets) and `a{b,c}d` (braces)?
What is the difference between `a[bc]d` and `a{b,c}d`? Why do people use `a{b,c}d` when there is already `a[bc]d`?
What is the difference between a[bc]d and a{b,c}d? Why do people use a{b,c}d when there is already a[bc]d?
Weijun Zhou (3548 rep)
Apr 27, 2019, 05:35 PM • Last activity: Sep 6, 2024, 03:15 AM
-2 votes
3 answers
243 views
Bash: How to extract portion from the end of a line that's in a variable?
In the context of a bash script, how do I extract the `uniqueID` from the `uniqueID_db.dat` part of URLs such as these, where the uniqueID can be anything, for example: ``` https://cdn.somedomain.com/fetch/uniqueID/uniqueID_db.dat https://server123456.eu.somedomain.com/789/storage/uniqueID/uniqueID_...
In the context of a bash script, how do I extract the uniqueID from the uniqueID_db.dat part of URLs such as these, where the uniqueID can be anything, for example:
https://cdn.somedomain.com/fetch/uniqueID/uniqueID_db.dat 
https://server123456.eu.somedomain.com/789/storage/uniqueID/uniqueID_db.dat 
https://cdn.somedomain.com/fetch/6234449e1539130b/6234449e1539130b_db.dat 
https://server654321.eu.somedomain.com/0123/storage/afd85b3f9ae5bc9/afd85b3f9ae5bc9_db.dat 
Lines always end in _db.dat and it's the uniqueID before it that I'd like to extract. Any line is in the variable $link. Is this extraction possible with sed or some other tool? If so, how? And can you please explain the workings so I can learn? I imagine something like:
echo "${link}" | sed '...'
Thanks.
algalg (107 rep)
Dec 28, 2020, 11:17 AM • Last activity: Jun 4, 2024, 10:19 AM
3 votes
1 answers
354 views
To understand zmv ## patterns
This is `zmv` command to non-recursively replace any number of spaces with a single underscore (thanks to St&#233;phane Chazelas, who helped me with it): ``` zmv -- '* *' '${f// ##/_}' ``` And here is a command from the internet, that seems to do the same thing, but recursively: ``` zmv -- '(**/)(*...
This is zmv command to non-recursively replace any number of spaces with a single underscore (thanks to Stéphane Chazelas, who helped me with it):
zmv -- '* *' '${f// ##/_}'
And here is a command from the internet, that seems to do the same thing, but recursively:
zmv -- '(**/)(* *)' '$1${2//( #-## #| ##)/_}'
Could someone explain how does it work? Specifically, how the ( #-## #| ##) part works?
jsx97 (1347 rep)
May 17, 2024, 08:52 PM • Last activity: May 17, 2024, 10:30 PM
2 votes
1 answers
312 views
How is the correct syntax for a more complex case statement?
``` case "$1","$name" in -py | --python | --python3,*) if [[ "$name" =~ \..+$ ]]; then ``` That doesn't catch stuff, which actually it should, like… ``` USERNAME@HOSTNAME:~$ myscript --python surfer ``` Funny thing: Simplify the multi pattern conditional to… ``` --python,*) if [[ "$name" =~ \..+$ ]]...
case "$1","$name" in
    -py | --python | --python3,*) if [[ "$name" =~ \..+$ ]]; then
That doesn't catch stuff, which actually it should, like…
USERNAME@HOSTNAME:~$ myscript --python surfer
Funny thing: Simplify the multi pattern conditional to…
--python,*) if [[ "$name" =~ \..+$ ]]; then
and it works! With the bitterly-repetitive outlook to have to place that section 3 times: 1st for
-py
, then for
--python
, and finally for
--python3
for catching all patterns. But the other thing is - the other way around:
case "$1" in
    -py | --python | --python3)    if [[ ! "$name" =~ \.py$ ]]; then
That's fine, that works! So, that disproves my assumption, that the multi pattern syntax might be incorrect, might needs the spaces to be removed, or any kind of bracket around the sum of all 3 patterns to be interpreted as a group, where the first OR the second OR the third pattern is supposed to be catched. And with all this I really have the impression, that you can't have both in GNU bash, version 4.3, multi pattern AND aside of that conditional a second conditional like "$name". Could that be? Or have I made a mistake in trying to acchieve that?
futurewave (213 rep)
Apr 4, 2024, 10:13 PM • Last activity: Apr 4, 2024, 11:11 PM
-1 votes
5 answers
77 views
multiline grep search into separate files per occurence
I have a file as following: ***example.txt*** ``` -1 15 1 0 0 11 -1.0000E+001 1.0000E+001 -1.0000E+001 2 0 0 11 1.0000E+001 1.0000E+001 -1.0000E+001 ... 29 0 0 11 1.0000E+001 2.0000E+001 1.0000E+001 30 0 0 11 5.0000E+000 5.0000E+000 5.0000E+000 -1 #ffafsda -1 780 1 116 1 2 1 1 7 20 1 11 2 15 4 18 3...
I have a file as following: ***example.txt***
-1
    15
         1         0         0        11 -1.0000E+001  1.0000E+001 -1.0000E+001
         2         0         0        11  1.0000E+001  1.0000E+001 -1.0000E+001
...
        29         0         0        11  1.0000E+001  2.0000E+001  1.0000E+001
        30         0         0        11  5.0000E+000  5.0000E+000  5.0000E+000
    -1
 
#ffafsda
    -1
    780
         1       116         1         2         1         1         7        20
         1        11         2        15         4        18         3        12
        13        16        22        19         5        24         9        29
         8        27         6        23
    -1
	
	asfasd
	
	afsdasdf
It consists of blocks always starting and ending by line entirely matching ^ {4}-1$. I need to separate a file into multiple by these blocks. What I figured out right now is this multiline regex that extracts these blocks: grep -Pzo '(?s)((?m:^)\s{4}-1(?m:$).*?(?m:^)\s{4}-1(?m:$))' example.txt output:
-1
    15
         1         0         0        11 -1.0000E+001  1.0000E+001 -1.0000E+001
         2         0         0        11  1.0000E+001  1.0000E+001 -1.0000E+001
...
        29         0         0        11  1.0000E+001  2.0000E+001  1.0000E+001
        30         0         0        11  5.0000E+000  5.0000E+000  5.0000E+000
    -1    -1
    780
         1       116         1         2         1         1         7        20
         1        11         2        15         4        18         3        12
        13        16        22        19         5        24         9        29
         8        27         6        23
    -1
You see second match is exactly printed behind first match (no newline or separator) - I'm failing to separate these occurences into files required output is following: ***file1:***
-1
    15
         1         0         0        11 -1.0000E+001  1.0000E+001 -1.0000E+001
         2         0         0        11  1.0000E+001  1.0000E+001 -1.0000E+001
...
        29         0         0        11  1.0000E+001  2.0000E+001  1.0000E+001
        30         0         0        11  5.0000E+000  5.0000E+000  5.0000E+000
    -1
***file2***
-1
    780
         1       116         1         2         1         1         7        20
         1        11         2        15         4        18         3        12
        13        16        22        19         5        24         9        29
         8        27         6        23
    -1
Any help appreciated.
Honza S. (101 rep)
Mar 20, 2024, 12:47 PM • Last activity: Mar 21, 2024, 11:17 PM
5 votes
6 answers
570 views
Count the longest stretch of consecutive patterns
I have a sequence file: $ cat file CACCGTTGCCAAACAATG TTAGAAGCCTGTCAGCCT CATTGCTCTCAGACCCAC GATGTACGTCACATTAGA ACACGGAATCTGCTTTTT CAGAATTCCCAAAGATGG I want to calculate the longest stretch of C+T. I could only count total C+T, but I want the longest stretch. $ cat file | awk '{ print $0, gsub(/[cCtT...
I have a sequence file:
$ cat file
CACCGTTGCCAAACAATG
TTAGAAGCCTGTCAGCCT
CATTGCTCTCAGACCCAC
GATGTACGTCACATTAGA
ACACGGAATCTGCTTTTT
CAGAATTCCCAAAGATGG
I want to calculate the longest stretch of C+T. I could only count total C+T, but I want the longest stretch.
$ cat file | awk '{ print $0, gsub(/[cCtT]/,"",$1)}'
CACCGTTGCCAAACAATG 9
TTAGAAGCCTGTCAGCCT 10
CATTGCTCTCAGACCCAC 12
GATGTACGTCACATTAGA 8
ACACGGAATCTGCTTTTT 11
CAGAATTCCCAAAGATGG 7
The *Expected result* would be to show the longest C+T stretch.
CACCGTTGCCAAACAATG 9 2
TTAGAAGCCTGTCAGCCT 10 3
CATTGCTCTCAGACCCAC 12 5
GATGTACGTCACATTAGA 8 2
ACACGGAATCTGCTTTTT 11 6
CAGAATTCCCAAAGATGG 7 5
CN_229133 (115 rep)
Jun 29, 2018, 09:32 AM • Last activity: Feb 7, 2024, 09:58 AM
1 votes
5 answers
557 views
Find pattern between special characters
I need a Sed/awk command to print the data between special charactes provided the pattern match should present in between special characters ({ & }) I have got a file which contains define service { host_name dns_vips service_description Multi Lookup use standard_service_template active_checks_enabl...
I need a Sed/awk command to print the data between special charactes provided the pattern match should present in between special characters ({ & }) I have got a file which contains define service { host_name dns_vips service_description Multi Lookup use standard_service_template active_checks_enabled 1 passive_checks_enabled 1 notifications_enabled 1 contact_groups mailgrp max_check_attempts 3 normal_check_interval 5 retry_check_interval 1 notification_interval 10 check_period 24x7 notification_period 24x7 notification_options w,r,c } define service { host_name dns_vips1 service_description Multi Lookup 2 use standard_service_template active_checks_enabled 1 passive_checks_enabled 1 notifications_enabled 1 contact_groups mailgrp1 max_check_attempts 3 normal_check_interval 5 retry_check_interval 1 notification_interval 10 check_period 24x7 notification_period 24x7 notification_options w,r,c } I need data between { and } when the service description matches Multi Lookup
harish (13 rep)
Oct 16, 2015, 08:46 AM • Last activity: Feb 6, 2024, 10:46 PM
0 votes
1 answers
58 views
Retrieve the Following Occurrence of the String 'PWD' Once the Given String is Located
Below is my sample test.log file export SQRDIR=/v/orahome/Middleware/Oracle/bin64 export OID=ap0092 export PWD=pass1 export FDPWD=pass1 export AP0085_PWD=pass1 export SVR=AFFPROD export TWO_TASK=db01 export EF_OID=AP0093 export EF_PWD=pass2 export CCC_PER_OID=CCC_JAS export CCC_PER_PWD=pass3 export...
Below is my sample test.log file export SQRDIR=/v/orahome/Middleware/Oracle/bin64 export OID=ap0092 export PWD=pass1 export FDPWD=pass1 export AP0085_PWD=pass1 export SVR=AFFPROD export TWO_TASK=db01 export EF_OID=AP0093 export EF_PWD=pass2 export CCC_PER_OID=CCC_JAS export CCC_PER_PWD=pass3 export CAN_PER_OID=CAN_JAS export CAN_PER_PWD=pass4 ################################################### The user inputs ap0092. Upon this input, I need to search for the first occurrence of PWD after the first occurrence of the search string i.e. =ap0092 Thus, the desired output will be pass1 Likewise, for CCC_JAS the desired output will be pass3 I need the solution for Solaris OS. I was doing grep -A 1 [for non-Solaris] to get the next line but do not know how to search if the desired line may not be the very next line. Please note: There may be multiple lines or Whiteline between export OID=ap0092 and export PWD=pass1 Kindly suggest.
Ashar (527 rep)
Dec 4, 2023, 04:20 AM • Last activity: Dec 4, 2023, 08:48 AM
1 votes
3 answers
367 views
awk print between lines when "/" is part of the name
I need to print lines between those that contain a "/" in the name. I tried with: ``` awk '/+SOLUTION/ESTIMATES/,/-SOLUTION/ESTIMATES/' $F > fil$F ``` and ``` awk '/+SOLUTION"/"ESTIMATES/,/-SOLUTION"/"ESTIMATES/' $F > fil$F ``` and ``` awk '/"{+SOLUTION/ESTIMATES}"/,/"{-SOLUTION/ESTIMATES}"/' $F > f...
I need to print lines between those that contain a "/" in the name. I tried with:
awk '/+SOLUTION/ESTIMATES/,/-SOLUTION/ESTIMATES/' $F > fil$F
and
awk '/+SOLUTION"/"ESTIMATES/,/-SOLUTION"/"ESTIMATES/' $F > fil$F
and
awk '/"{+SOLUTION/ESTIMATES}"/,/"{-SOLUTION/ESTIMATES}"/' $F > fil$F
but the error is always more or less the same like "unterminated string". How can I print lines between those patterns?
euge1780 (23 rep)
Nov 25, 2023, 04:42 PM • Last activity: Nov 26, 2023, 06:37 PM
1 votes
2 answers
52 views
replace both filename and filename's content relying solely on pattern found on the name-of-the-file
I'd like to know if it's possible to replace both `filename.ext` and a matched pattern inside its content regarding a pattern founded on the *filename* itself using `sed` (and `grep` also, maybe?). Let me show you as an example a few files that you may re-create with: ``` cat > '[2022] Diary 2022 (e...
I'd like to know if it's possible to replace both filename.ext and a matched pattern inside its content regarding a pattern founded on the *filename* itself using sed (and grep also, maybe?). Let me show you as an example a few files that you may re-create with:
cat > ' Diary 2022 (essay, Travels).txt'  ' Diary 2022 (list, Recipes).txt'  ' Diary 2022 (watchlist, Movies-).txt'  ' Diary 2022 (list, Movies+).txt'  \(.+\s(\w.+)\)

I'd like things to became like this:

 Diary 2022 (essay, Travels).txt > becomes > travels.txt
and its content becomes >
TITLE/- travels -\TITLE @ sometext...
 Diary 2022 (watchlist, Movies-).txt > becomes > movies-.txt
and its content becomes >
TITLE/- movies- -\TITLE @ sometext...
 Diary 2022 (list, Movies+).txt > becomes > movies+.txt
and its content becomes >
TITLE/- movies+ -\TITLE @ sometext... ``` and so on...
dAllARA (33 rep)
Sep 7, 2023, 08:59 PM • Last activity: Nov 17, 2023, 04:24 PM
0 votes
4 answers
95 views
Wrap specific string with characters
For example, in a large file I might have: This is a /16 text file I'd like to change it to the following: This is a [[[/16]]]] text file Would like to match the exact string `/16` Is there a way to do this?
For example, in a large file I might have: This is a /16 text file I'd like to change it to the following: This is a [[[/16]]]] text file Would like to match the exact string /16 Is there a way to do this?
Proletariat (707 rep)
Oct 11, 2023, 02:43 PM • Last activity: Oct 11, 2023, 06:19 PM
2 votes
0 answers
80 views
Pattern list in bash extglob containing a / and a |
Here is a transcript of commands (and their output) that explains my problem: ``` /tmp/example $ shopt -s cdspell on checkwinsize on cmdhist on complete_fullquote on direxpand on dirspell on expand_aliases on extglob on extquote on failglob on force_fignore on globstar on histappend on interactive_c...
Here is a transcript of commands (and their output) that explains my problem:
/tmp/example $ shopt -s
cdspell         on
checkwinsize    on
cmdhist         on
complete_fullquote      on
direxpand       on
dirspell        on
expand_aliases  on
extglob         on
extquote        on
failglob        on
force_fignore   on
globstar        on
histappend      on
interactive_comments    on
login_shell     on
nocaseglob      on
nullglob        on
progcomp        on
promptvars      on
sourcepath      on

/tmp/example $ ls -R
.:
prefix_bar  prefix_foo

./prefix_bar:
test.c

./prefix_foo:
baz  test.c

./prefix_foo/baz:
test.c

/tmp/example $ ls prefix_@(foo/baz|bar)/test.c
prefix_bar/test.c

/tmp/example $ ls @(prefix_foo/baz|prefix_bar)/test.c
prefix_bar/test.c
Question: Why does bash ignore the foo/baz or the pattern_foo/baz portion of the pattern-list? For the record, I looked around before posting this question and the question closest to what I am about to ask is https://unix.stackexchange.com/questions/548745/bash-extglob-should-the-order-of-patterns-in-a-pattern-list-matter (and it does not answer my question).
Happy Green Kid Naps (121 rep)
Oct 6, 2023, 11:10 PM
12 votes
1 answers
5222 views
How to grep MATCH colored input?
Say I have output from a command that is colorized for the terminal. I want to match any line that contains the color yellow. How can I do this in grep, eg: `mycommand -itSomtimesPrintsLinesWithYellowColorCodes | grep -e "?????"` Note: This is NOT about colorizing the output of grep, or adding any c...
Say I have output from a command that is colorized for the terminal. I want to match any line that contains the color yellow. How can I do this in grep, eg: mycommand -itSomtimesPrintsLinesWithYellowColorCodes | grep -e "?????" Note: This is NOT about colorizing the output of grep, or adding any colors. It's only about how to filter/match colored of input coming into grep.
Zombies (665 rep)
Feb 15, 2018, 08:52 PM • Last activity: Aug 6, 2023, 10:51 PM
1 votes
1 answers
3624 views
Searching for a pattern in a binary file using python script
I want to find repeated copies of the config section within the partition dump (binary file), using pattern and 'magic' header. The config section always starts with 202 `'0xff'` bytes followed by 4 bytes `'\x00\x00\x23\x27'`. The script should identify different copies of configuration within the p...
I want to find repeated copies of the config section within the partition dump (binary file), using pattern and 'magic' header. The config section always starts with 202 '0xff' bytes followed by 4 bytes '\x00\x00\x23\x27'. The script should identify different copies of configuration within the partition and print addresses (in bytes) where occurrences of the pattern starting. I adjusted an existing python script for my pattern, but it doesn't works, just throws errors, due to mixing bytes with strings. How to fix this script? #!/usr/bin/env python3 import re import mmap import sys magic = '\xff' * 202 pattern = magic + '\x00\x00\x23\x27' fh = open(sys.argvReferenced image, "r+b") mf = mmap.mmap(fh.fileno(), 0) mf.seek(0) fh.seek(0) for occurence in re.finditer(pattern, mf): print(occurence.start()) mf.close() fh.close() _errors:_ $ ./matcher.py dump.bin Traceback (most recent call last): File "/home/eviecomp/BC2UTILS/dump_previous_profile/./matcher.py", line 13, in for occurence in re.finditer(pattern, mf): File "/usr/lib/python3.9/re.py", line 248, in finditer return _compile(pattern, flags).finditer(string) TypeError: cannot use a string pattern on a bytes-like object pattern and magic: enter image description here
minto (575 rep)
Jul 6, 2023, 09:12 PM • Last activity: Jul 6, 2023, 10:56 PM
0 votes
1 answers
124 views
Bash: Difference between "Pattern Matching" and "Filename Expansion" in the context of parameter expansions that do matching
**Edit:** Question ansewered by Gordon Davisson's comment I was reading the GNU Bash manual, and I noticed that there are basically three types of "Parameter expansion" that do pattern matching: 1. `${parameter#word} ${parameter##word}` 2. `${parameter%word} ${parameter%%word}` 3. `${parameter/patte...
**Edit:** Question ansewered by Gordon Davisson's comment I was reading the GNU Bash manual, and I noticed that there are basically three types of "Parameter expansion" that do pattern matching: 1. ${parameter#word} ${parameter##word} 2. ${parameter%word} ${parameter%%word} 3. ${parameter/pattern/string} ${parameter//pattern/string} ${parameter/#pattern/string} ${parameter/%pattern/string} What I found odd is that the first two kinds are described like this: > The word is expanded to produce a pattern and matched according to the > rules described below (see Pattern Matching). But the third kind is described like this: > The pattern is expanded to produce a pattern just as in filename expansion. [...] The match is performed according to the rules described below (see Pattern Matching). So I'm wondering, is there actually a difference between the expansions? The first two descriptions are not exactly clear about what kind of expansion is made, but, in any case, the "pattern matching" rules are virtually the same as "filename expansion". So maybe this is an issue of lack of clarity in the documentations, and we can say that they are all the same? My guess would be that the first two descriptions are more accurate, because I doubt that any sort of actual filesystem check is performed during the third kind.
Sebastian Carlos (262 rep)
Jul 1, 2023, 06:37 PM • Last activity: Jul 2, 2023, 04:41 AM
Showing page 1 of 20 total questions