sed

Why is sorted uniq -c command showing duplicates

Why is sorted uniq -c command showing duplicates I am trying to count how many times I use a certain version of a library on my computer. For some reason, uniq -c is outputing duplicates, despite sorting it, and despite the sort order seeming in order. Any ideas or feedback? Thanks for your time. With …

Total answers: 1

how to de-duplicate block (timestamp+command) from bash history?

how to de-duplicate block (timestamp+command) from bash history? I’m working with bash_history file containing blocks with the following format: #unixtimestampncommandn here’s sample of the bash_history file: #1713308636 cat > ./initramfs/init << "EOF" #!/bin/sh /bin/sh EOF #1713308642 file initramfs/init #1713308686 cpio -v -t -F init.cpio #1713308689 cpio -v -t -F init.cpio #1713308690 ls #1713308691 ls My …

Total answers: 2

how to replace the last n occurences of string using sed

how to replace the last n occurences of string using sed I can replace the third occurrence of a string with: sed ‘s/OLD/NEW/3’ or, with the GNU implementation of sed, all occurrences starting from the third with: sed ‘s/OLD/NEW/3g’ But how do I replace the last 3 (or n) occurrences? Asked By: dhm || Source …

Total answers: 4

change newline with space on certain condition with sed

change newline with space on certain condition with sed I have the following text in a file: #1713251479 sh lowbat.sh #1713251495 nvim lowbat.sh #1713252186 sh lowbat.sh and I want to put the Unix timestamp inline with the command like this: #1713251479 sh lowbat.sh #1713251495 nvim lowbat.sh #1713252186 sh lowbat.sh I tried the following sed command: …

Total answers: 2

Add t to jq resulted array extraction

Add t to jq resulted array extraction I want to add t before each element in the resulting jq array and this is for formatting purposes as the result will get added to a file. here is the json file: { "array": ["element1", "element2"] } When you read this array with jq content=$(jq ‘.array’ file) …

Total answers: 1

How can I replace the last occurrence of " before ] (text-manipulation)

How can I replace the last occurrence of " before ] (text-manipulation) I’m doing some text manipulation and I want to replace the last occurrence of " (with ", "four") before ] which could be in different lines. (this essentially adds a new item in the array) e.g: array = ["one", "two", "three"] other_array = …

Total answers: 5

How to remove double quotes within the double-quoted field values in .dat file

How to remove double quotes within the double-quoted field values in .dat file I have a text file that has around 15 columns.  The fields are separated by comma.  One column that is description is double-quoted and also has some words which are double-quoted.  I need to retain the beginning and ending double quotes and remove only the inner …

Total answers: 5

bash/sed replace string of repeating string within a string

bash/sed replace string of repeating string within a string I have a file where each line represents data that may be encompassed with a string like "o o ". I need to replace the string between the encompassing characters with another string. The "internal" string is a pattern of two characters that may include an …

Total answers: 1

How can I add a property in the end of the object (text manipulation)

How can I add a property in the end of the object (text manipulation) I’m doing some text manipulation and I want to add a property (at the end) to an object named "object" that is inside a configuration file e.g . . object = { "one": "one", "two": "two", } . . Should become …

Total answers: 3

Regex pattern in sed to match hexadecimal address of fixed length

Regex pattern in sed to match hexadecimal address of fixed length I would like to match a hexadecimal address of fixed length using a regex pattern in sed. For example: 0x000000010b2e993c 0x is always present followed by the sixteen-character hexadecimal number. This is successful: ‘s/0x[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]//’ Is there a shorthand way to match whereby I can …

Total answers: 1

extract string before numbers and after underscore

extract string before numbers and after underscore The original string is like this: str-str001-002_01 str-str005-006_05 I would like to extract string before number, and after underscore, so it would be like this: str-str_01 str-str_05 I remember sed could separate pattern into groups like this: sed -n ‘s/(^.*)([0-9]-[0-9])(.*$)/13/p’ but it prints: str-str0002_01 Then I remember that …

Total answers: 2

How can I include any content in the sed replace command?

How can I include any content in the sed replace command? I want to be able to handle any type of content stored into the bash variable ${CONTENT}, to be used as sed replacement text into another content, no matter if there are quotation marks, single quotes or other special characters that an HTML file …

Total answers: 1

How to replace (***) with (***) with sed

How to replace (***) with (***) with sed so I wanted to write a script to replace special characters with their escaped equivalent so I can pass it to mkdir command. As a friend used a similar line I can use this to replace parantheses with underline like below: echo ‘Hello (World)’ | sed ‘s/(|)/_/g’ …

Total answers: 3

How to replace a file path with whitespace using sed?

How to replace a file path with whitespace using sed? I have many different files with a string in them. I need to replace this string with a path, although the path contains whitespace. So far, I have this: for a in files_[a-z][a-z]_all.conf ; do fileWithPath="~/Downloads/files_config_linux/auth-user-pass files_userpass.txt" otherOne="auth-user-pass mullvad_userpass.txt" sed -i ‘s|’$otherOne’|’$fileWithPath’|g’ $a done But …

Total answers: 2

Strange behavior with sed/head and named pipes/FIFOs (needs two writes)

Strange behavior with sed/head and named pipes/FIFOs (needs two writes) I have a fifo created with mkfifo in a shell, opened in rw mode with exec 3<>fifo. When I run sed 1q <&3 and write a line to the fifo in another shell, sed doesn’t print the line until I write another line to the …

Total answers: 1

How to let sed un-comment one of multiple paragraphs separated by newlines?

How to let sed un-comment one of multiple paragraphs separated by newlines? Clarification: Using GNU sed. Status quo – ~/.screenrc, last lines: # name synthpop # screen -t script emacs -nw /home/$USER/bin/synthpop # name thrashmetal # screen -t script emacs -nw /home/$USER/bin/thrashmetal # number 1 # split -v # focus # chdir "/home/$USER/thrashmetal/src" # screen …

Total answers: 5

multiline grep search into separate files per occurence

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 …

Total answers: 5

sed to replace in a file, with both old and new strings in files

sed to replace in a file, with both old and new strings in files I want to automatically comment out a code block in PHP file, as below: The original block: // Enable all errors ini_set(‘display_startup_errors’, 1); ini_set(‘display_errors’, 1); error_reporting(E_ALL); The new block with comments: /* For the production version, the following codelines are commented …

Total answers: 3

How to add content to a file before the last } character using Busybox utilities?

How to add content to a file before the last } character using Busybox utilities? There is a file with contents { "first_name": "John", "last_name": "Smith", "is_alive": true, "age": 27, "address": { "street_address": "21 2nd Street", "city": "New York", "state": "NY", "postal_code": "10021-3100" }, "phone_numbers": [ { "type": "home", "number": "212 555-1234" }, { "type": …

Total answers: 5