Hot answers tagged

28

Using the del function in jq: jq 'del(.list[] | select(.name=="APP1"))' If you wanted to pass the app name as a shell variable to jq you can use the --arg option: jq --arg name "$name" 'del(.list[] | select(.name==$name))'


25

Parsing markup with regular expressions is notoriously problematic. While not an issue with your sample data, angle brackets may appear in tag attributes, comments and possibly other places, making regular expressions that match from < to > unreliable. You should resort to tools that implement a markup parser. For instance, using pandoc (version >= ...


21

try grep '^[^#]*#[^#]*$' file where ^ ; begin of line [^#]* ; any number of char ≠ # # ; # [^#]* ; any number of char ≠ # $ ; end of line as sugested, you can grep on the whole line, with grep -x '[^#]*#[^#]*' with same pattern without begin of line/end of line anchor. -x to grep whole line, see man grep -x, --line-regexp Select ...


18

Drafts are only available to Austin Group members, but the information is publicly available in the Austin Group bug tracker: sed -E is queued for issue 8. (Joining the Austin Group only requires signing up to the mailing list.) So the manpage is only slightly ahead of itself...


18

You can use the "OR" syntax for regular expressions: sed -E '/png|jpg|svg/d' url.txt This will delete all lines containing either pattern. If you want to make sure that this pattern is the filename extension, i.e. that the pattern occurs at the end of the line, you can include an anchor into the regular expression: sed -E '/(png|jpg|svg)$/d' url....


16

Using awk: awk -F'#' 'NF==2' infile based on # field separator, if number of fields in a line was exactly two fields then will print out. note that for example #x or x# or even # are considered two fields so.


15

With cut cut -d: -f2- file -d sets the separator and -f2- means from the second to the last field.


14

You can't do the whole operation with a single substitution in sed, but you can do it correctly in different ways depending on whether the two substrings A and B are single characters or longer strings. Assuming the two substrings A and B are single characters... You want to transform AYB into BBYAA. To do this, Change each A to B and B to A using y/AB/BA/....


14

Using csvformat from csvkit, and assuming that the end result should be a CSV file with comma as delimiter (as described in the text of the question): $ csvformat -d '|' file 1,"a,b",4 1,"c,d",4 1,"e,f",4 1,"g,h",4 1,"i,j",4 This reformats the CSV file from having |-characters as delimiter to having the ...


13

Although it has fallen out of fashion, few languages can match perl for text processing. For instance: Assume only one set of numbers, copy to the end of the line: $ perl -pe 's/.*?a(\d+).*/$& $1/' file a11.t 11 some text here a06.t 06 some text here Multiple sets of numbers, add both to the end $ cat file a11.t some text here a06.t some text ...


13

printf %.1s @{1..20} $'\n' the shell expands the braces first, this is called "Brace Expansion". @{1..20} into @1 @2 @3 ... and so on Then the first byte of each parameter will be output, including the last argument $'\n' consisting of one byte - the newline character


13

I tried: sed "s/\$Date\$/\$Date $(date) \$/" but it does not work. Because \$ in double-quotes only tells the shell not to interpret (expand) $, but then there's sed. It gets s/$Date$/$Date … $/ (where … denotes what $(date) expanded to) and interprets the second $ as an anchor matching the end of the line. In s/regexp/replacement/ $ is ...


13

The issue with the command is the g at the end. It will cause all double quotes to be substituted with < on each line matching ^#include. Notice how the StackOverflow question that you link to is concerned with replacing the <> with "" (which could be done using y/<>/""/, or less efficiently using s/[<>]/"/g, on ...


13

Force the rebuild of the record with $1=$1! This squeezes all contiguous spaces into a single one. awk '{$1=$1};!NF||!seen[$0]++'


13

Although possible, it is a very, very bad idea to attempt to parse XML or HTML with tools like sed that are based on regular expressions. That can work for simple cases but gets really hard to get right, even for experts, for even slightly more complex cases. So, use an XML parser such as xmlstarlet (should be installable from your operating system's ...


13

awk: awk 'p==$0{print " Address : NOT_FOUND"}{p=$0}1' A rather naive solution. p==$0 IF p == current line THEN print not found p=$0 SET p = current line 1: print Handles consecutive duplicate lines. And as noted by @san-fran in comments under question, "The last IP may be missing too, right?" – Ups. Should have thought of that. ...


13

This is what split is for. To split the file into multiple files with 1000 lines (or less, for the last one), you can do: split -d -l 1000 file '' That will split the file into files of 1000 lines each (-l 1000), with numerical suffixes with .txt as an additional suffix and using an empty prefix (''). The result for a file with 10023 lines will be 11 files ...


12

Strip off the Location: and you're left with JSON: $ echo '{"date": "16/07/20", "time": "19:01:22", "latitude": "34.321", "longitude": "133.453", "altitude": "30m"}' | jq .longitude "133.453" See in the man page if gps has an option to not print ...


12

This is the kind of problem where you need a loop so you can search for both patterns simultaneously. awk ' BEGIN { regex = "A|B" map["A"] = "BB" map["B"] = "AA" } { str = $0 result = "" while (match(str, regex)) { found = ...


11

-n Prefix each line of output with the 1-based line number. -o Print only the matched parts. [^ATGC] exclude characters. grep -no '[^ATGC]' file


11

Draft POSIX documents are being worked on by the The Austin Common Standards Revision Group, who announce: Jun 2020: The first draft of the 202x revision of the standard is now available. The draft is relatively tightly guarded: In order to download drafts from this web site, Austin Group members need to login using their individual web accounts and ...


11

Workaround with awk: awk '/two/{ n=NR+5 } NR==n{ sub(/.*/, "MODIFIED") }1' file or if you want to replace the line awk '/two/{ n=NR+5 } NR==n{ $0="MODIFIED" }1' file


10

sed here is the perfect tool for the task. However note that you almost never need to pipe several sed invocations together as a sed script can be made of several commands. If you wanted to extract the first sequence of 2 decimal digits and append following a space to end of the line if found, you'd do: sed 's/\([[:digit:]]\{2\}\).*$/& \1/' < your-...


10

sed -n 'N;s/^EMAIL://;s/PASSWORD://p' file N append next line to pattern space, s/^EMAIL:// substitute EMAIL: with nothing, s/PASSWORD://p substitute PASSWORD: with nothing and only print if the substitution was successful. Tested on sample input. Assumption: 1st line is EMAIL:, 2nd is PASSWORD: or HASH:, and repeat. As a bonus, if there may be blank ...


10

GNU Sed: sed 's/;./\U&/g' file For every character following a semicolon (;.), we make it uppercase with the \U special sequence. The g flag substitutes all occurrences in a line. If GNU Sed is not available, a POSIX compliant alternative is to use Ex. printf '%s\n' '%s/;./\U&/g' '%p' | ex file The substitute command is the same, but all lines ...


10

With sed: sed 's/[^:]*://' input.txt In words: match a sequence of zero or more non-colon characters followed by a colon and replace them with nothing.


10

code.awk: BEGIN{j=1} NR==FNR{a[NR]=$0;next} sub(/=> '.*',$/,"=> '"a[j]"',"){++j} 1 awk -f code.awk file2 file1 > file3 Line by line explanation: Initialize j=1. Put each line of file2 in the array a. In file1, for each line, try to substitute a string matching the => '.*',$ regex by the concatenation of => ' a[j] ',. If ...


10

Use the ADDR1,ADDR2 with FIRST~STEP as ADDR1 and +OFFSET as ADDR2, so: $ seq 30 | sed '10~10,+1!d' 10 11 20 21 30 In anycase, note that both ~ and + are non-standard GNU extensions. See info sed 'line selection' and info sed 'range of lines' on a GNU system for details. POSIXly, you'd use: seq 30 | sed -n '1n;n;n;n;n;n;n;n;n;p;n;p'


10

sed -n '/^Max/ { s/^.*=\s*//;h; }; /^Time/{ s/^.*=\s*//;G; s/\n/ /;p; }' infile match-run syntax /.../{ ... }: commands within {...} will only run on the lines that matched with regex/pattern within /.../; s/^.*=\s*//: deletes everything up-to last = and whitespaces \s* also if there was any. h: copy the result into hold-space G: append the hold-...


10

The problem is that you have - as is recommended - enclosed the sed instructions in single quotes ' ... '. However, inside single quotes, variable expansion such as $var is disabled, and the expression remains verbatim. As a solution, you can enclose the instructions in double quotes: sed -i "1s/^/$var\n/" file.txt Note however that I would ...


Only top voted, non community-wiki answers of a minimum length are eligible