Comment by Sundeep on How to exclude lines with duplicate strings using grep
You don't need PCRE for backreferences. Both grep and sed support it, only awk doesn't in the search part.
View ArticleComment by Sundeep on Bash / SED: Greedy vs Non Greedy
To nit pick, sed doesn't have greedy quantifiers either. Instead, the longest match wins. For example, echo 'fig123312apple' | sed -E 's/g[1-3]+(12apple)?/X/' gives fiX and echo 'fig123312apple' | perl...
View ArticleComment by Sundeep on Filtering multi string file according to multiple patterns
some sample input/output would help.. also, perhaps you wanted to use !(/^HETATM/ && /lig|lih |^END/) or perhaps !/^HETATM.*(lig|lih)/
View ArticleAnswer by Sundeep for accumulate multiple values against one record in awk
With newer versions of datamash:$ datamash -t'|' -c';' -g 1,2 collapse 3,5 <ip.txt1|dev|Smith|minus1|ana|jhon;peter|plus;plus2|dev|dash;|minus;plus-g 1,2 group by 1st and 2nd columnscollapse 3,5...
View ArticleAnswer by Sundeep for Sort output in bash based on character count not...
With perl:$ perl -e 'print sort {length($a =~ s/ //gr) <=> length($b =~ s/ //gr)} <>' ip.txtaa ba b c abcdeab cd efabcdefgWith ruby:$ ruby -e 'puts readlines.sort_by {_1.sub(" ", "").size}'...
View ArticleAnswer by Sundeep for Get text between markers that span over multiple lines
OP's attempt can be simplified to delete the lines containing foo along with the start and end markers:sed -n '/START MARKER/,/END MARKER/ {//d; /foo/d; p}'This works with GNU sed, but other...
View ArticleAnswer by Sundeep for regex in sed to keep current line but remove next line
Here's one way to do it (checked with GNU sed, syntax might vary for other implementations):$ sed -E '/Value[0-9]{1,2} [0-9a-z]{8}$/{n; d}' ip.txtServerAValue1 fh824rfzServerBValue3...
View ArticleAnswer by Sundeep for how to print remaining portion of each line after a...
For a single character, you can use it as a field separator with cut and display except the first field. For example:$ echo 'log-2023-06-30-082821.txt:2023-06-30 08:30:32.648' | cut -d: -f2-2023-06-30...
View ArticleAnswer by Sundeep for Group by a column and creat a new data frame using bash
With GNU datamash:$ <ip.txt datamash -t, -g1,2 min 3 max 4 first 5-6chr1,A,1,40,y,-chr2,B,0,30,y,-chr3,C,1,3,y,--t, use , as the field separator-g1,2 group by first and second columns (assuming...
View ArticleAnswer by Sundeep for Change random text between strings with sed
You need to use ERE for () to act as metacharacters.$ sed -E 's/(text3").*(" text4)/\1foo\2/g' ip.txttext1"text2" text3"foo" text4 text5"text6"text1"text2" text3"foo" text4 text5"text6"I'd recommend...
View ArticleAnswer by Sundeep for linux command to transform 'VW_ABCD_EF_GHIJ_KLM_...'...
A slightly different solution, without the initial ${s,,} processing:$ s='VW_ABCD_EF_GHIJ_KLM'$ echo "$s" | sed -E 's/^VW//; s/_([^_]+)/\L\u\1/g'AbcdEfGhijKlm\L\u\1 will uppercase the first character...
View ArticleAnswer by Sundeep for How to modify grepped output with cut or awk in Unix?
Based on given sample, here are solutions using sed and awk (they both support filtering, so no need grep):$ sed -nE '/Receivedrequest/s/[^[]+\[([^,]+),[^=]+=([^,]+),[^=]+=([^,]+),[^=]+=([^]]+).+/\1 \2...
View ArticleAnswer by Sundeep for Need to get file name, line number, match position of a...
If you are okay with position starting with 1 (as opposed to 0), you can use ripgrep$ cat ip.txtabcdefxyzva$ rg -H --column --no-heading 'a' ip.txtip.txt:1:1:abcip.txt:3:5:xyzvaYou can use awk for a...
View ArticleAnswer by Sundeep for How to find last occurrence of pattern and print 20...
Since sample wasn't provided, I created one:$ cat ip.txtapple===123===bananafigmangocherry===423.14$ tac ip.txt | grep -m1 -A3 '===' | tacfigmangocherry===tac is handy for such cases. After reversing...
View ArticleAnswer by Sundeep for awk/sed: repeat 1st column for each column
Here's another awk solution:$ echo 'A B C D E F' | awk '{for(i=2; i<=NF; i++) $i=$i " " $1; sub(/^[^ ]+ /, "")} 1'B A C A D A E A F A
View ArticleAnswer by Sundeep for Combine two file according to a specific match
Here's one way to solve this:$ awk 'NR==FNR{a[$1]=$2 " " $5 " " $7; next} {print $0, $1 in a ? a[$1] : "NO MATCH"}' file2 file1ID1 -5.1 -4.4 juice ill,safe F25B9ID2 -6.2 -3.8 NO MATCHID3 -8.9 -7.1...
View ArticleAnswer by Sundeep for How do I count multi line recurring pattern in a file?
With ripgrep:$ rg -UPc '(?s)^A$((?!^A$).)*?^XYZ$' ip.txt2The -U option enables multiline search. -P is for PCRE (since we need lookarounds) and -c is to get the count. (?s) enables . to match newline...
View ArticleAnswer by Sundeep for Get last one-digit number after word
With GNU grep:$ echo 'Fund1 abcd2 Rail1 50 000 13.2 2Fund Train2' | grep -oP '[a-z]\K\d\b'1212-P option enables PCRE. [a-z]\K matches lowercase alphabets, but won't be part of the matched portion. \d...
View ArticleAnswer by Sundeep for How do I get rid of unwanted pipe symbols in a pipe...
Assuming you have fixed number of fields, here's one possible way to do it:perl -pe 's/^([^|]*\|){8}\K.*?(?=(\|[^|]*){6}$)/$&=~s,\|,\\|,gr/e' ip.txtThis will replace | with \| in the problematic...
View ArticleAnswer by Sundeep for How can I make my regular expression match variables...
If you are okay with installing ripgreprg -oU '\{[^}]*\} = process\.env'The -U option enables multiline matching.That said, grep -oz '{[^}]*} = process\.env' works for me for your given input samples....
View Article