Sundeep
  • Member for 6 years, 9 months
  • Last seen more than a week ago
Extract first block of consecutively matching lines from a file?
Accepted answer
1 votes

With awk: $ awk '/^#/{f=1} f && !/^#|^[[:space:]]*$/{exit} f' ip.txt #!/usr/bin/env bash # header: add, replace, and delete header lines. # # Example usage: # $ seq 10 | header -a 'values' # $...

View answer
How to search for multiline text files containing a set of words (e.g., AAA & (BBB | CCC) & ~DD)?
1 votes

I'd be interested to see a timing comparison with these solutions, especially for ripgrep which has built-in parallel processing. With GNU grep grep -rLZ 'DDD' | xargs -0 grep -lZ 'AAAA' | xargs -0 ...

View answer
Join a number of lines with a starting and ending pattern
3 votes

With awk: awk 'f{sub(/^[[:space:]]+/, "")} {f=/,$/; ORS = f ? "" : RS} 1' This will set a flag f whenever a line ends with ,. Based on the flag, output record separator will be ...

View answer
Find and add quotes in between particular string
3 votes

With sed: $ sed 's/[^|]*,[^|]*/"&"/g; y/|/,/' ip.txt 1,"a,b",4 1,"c,d",4 1,"e,f",4 1,"g,h",4 1,"i,j",4 s/[^|]*,[^|]*/"&"/...

View answer
Bash: Sort version numbers in a string separated by whitespace with awk etc
3 votes

With perl: $ s='1.3.2 1.3.1 1.2.3 1.1.1.5' $ echo "$s" | perl -Mversion -lane 'print join " ", sort { version->parse($a) cmp version->parse($b) } @F' 1.1.1....

View answer
Compare two files based on first column
3 votes

$ awk 'NR==FNR{a[$1]; next} FNR==1 || !($1 in a)' file2 file1 ID firstname lastname 4 Brenda Something NR==FNR{a[$1]; next} Use first column from file2 to build array keys ...

View answer
Substitute entire line while removing newline of that particular line
5 votes

I'd suggest to use perl here, syntax isn't that different from sed for this case: $ cat ip.txt this this is a test another test? $ perl -pe 's/^this\n/XYZ/' ip.txt XYZthis is a test another test?

View answer
How to get contents between two strings using same number of repeated characters?
3 votes

Seems like your question wasn't clear and you got various types of solution. To get only content surrounded by == with GNU sed (syntax/feature might vary with other implementations): $ sed -n '/^==$/,/...

View answer
Bash - remove dashes and new-lines before replacing new-lines with spaces
3 votes

Another perl solution (assuming the last line doesn't end with - character): perl -pe 's/-\n//; s/\n/ / if !eof' Unlike sed or awk, the record separator can be directly manipulated using perl. So, it ...

View answer
How can I group by the country in column two and then sort by the resulting sum?
2 votes

With GNU awk: $ awk 'BEGIN{PROCINFO["sorted_in"] = "@val_num_desc"} {a[$2]+=$1} END{for(k in a) print a[k], k}' ip.txt 8 UA 6 US 2 GB See gawk manual: Using Predefined ...

View answer
Merge files line by line
3 votes

Here's an alternate to paste (tested with GNU pr and a shell that supports ANSI-C quoting) pr -mts$'\n' <files> One advantage is that -s option will work for multicharacter separator as well. ...

View answer
Find files with matched whole lines from a file
4 votes

Assuming your search string doesn't have multiple trailing newlines or ASCII NUL characters (see pitfalls of reading file into shell variable for details) and you are okay with using ripgrep: rg -lUF &...

View answer
Print count number of unique pattern occurences after each occurence
3 votes

With perl perl -lne '$h{$_}++ if /Exception/; END{print "$_ $h{$_}" for sort {$h{$a} <=> $h{$b}} keys %h}' sort {$h{$a} <=> $h{$b}} keys %h will sort based on hash ...

View answer
Replace (one or) two different patterns in a file with regexp
Accepted answer
2 votes

With lookarounds, you can check if the strings around | are same or not. For example: $ cat ip.txt [[foo>a|d]] [[foo>b|e]] [[foo>c|f]] # same as: rg -NP '\[\[([^>]+)>([^|]+)\|(?!\2])([...

View answer
sed to insert all necessary bash escapes into a filepath
Accepted answer
2 votes

printf '%q\n' 'My Books/Pride & Prejudice.pdf' will give your desired output. You can use a glob pattern, for example, printf '%q\n' * to get the results for multiple files. Depending on your use ...

View answer
How to replace every digit after special character '$' with '*'
2 votes

With perl $ perl -pe 's/\$\K\d+/$&=~s|.|*|gr/ge' ip.txt axbnx $** ghjas $**$ axbnx $*** ghjas $ axbnx $*** ghjas $$*** \$\K\d+ match digits preceded by $ character (\K will prevent $ from being ...

View answer
Replace all characters except the first four characters
5 votes

With perl if GNU sed isn't available: md5sum input.txt | perl -pe 's/^.{4}(*SKIP)(*F)|./x/g' ^.{4}(*SKIP)(*F) will prevent replacement of first four characters |. specifies the alternate pattern that ...

View answer
Replacing regex matches in one file with lines from another file
3 votes

Another awk solution: $ awk -F"'" -v OFS="'" '(getline line < "file2")==1{$4=line} 1' file1 'text_clear' => 'emptied', 'text_search' => 'lost', '...

View answer
Grep the data only (-o) and the first column
5 votes

$ cat script.awk NR == FNR { a[$0] next } { for (i = 2; i <= NF; i++) { for (k in a) { if ($i == k) { a[k] = a[k] ? a[k] "," $1 : $1 } } } } END { ...

View answer
Sort while neglecting regex pattern
4 votes

With perl alone: perl -e 'print sort {$a=~s/^(www|m)\.//r cmp $b=~s/^(www|m)\.//r} <>' $a and $b are special variables in Perl to allow specifying custom sort comparisons s/^(www|m)\.//r will ...

View answer
How to identify a pattern containing nested parameters within it
1 votes

Using perl and recursive matching: $ cat ip.txt length(bill_cycle) length(some(somethiing)) $ perl -pe 's/length(\(((?:[^()]++|(?1))++)\))/length(cast($2 as string))/' ip.txt length(cast(bill_cycle ...

View answer
awk gensub regex matching group between square brackets
2 votes

$ s='-0.005 Tc 0.005 Tw [(T)-8.5(o)-3.2(p)-15.3(ik)]TJ' $ # if you want to delete []TJ $ echo "$s" | awk '{print gensub(/\[([^]]+)]TJ/, "\\1", "g")}' -0.005 Tc 0.005 Tw (...

View answer
Changing content format
3 votes

# if you need the space after comma $ seq 10 | perl -pe 's/.+/"$&"/; s/\n/, / if !eof' "1", "2", "3", "4", "5", "6", "7&...

View answer
sed to extract lines following a starting group pattern
4 votes

Another solution with awk: awk 'p2~/^A/ && p1~/^B/ && /^C/{print p2 RS p1 RS $0} {p2=p1; p1=$0}' With perl and slurping entire input as single string: perl -0777 -ne 'print /^A.*\nB.*\...

View answer
Split Column into multiple column when there is blank column
2 votes

$ csplit -zsf file ip.txt /^$/+1 {*} $ paste -d, file* | head -n -1 A,1,Q B,2,W C,,A From manual: csplit - split a file into sections determined by context lines -z, --elide-empty-files remove empty ...

View answer
Print matched pattern and previous lines until a linebreak or a space, using sed or awk
1 votes

If you are okay with matching up to first TMP. Empty RS will result in paragraph mode, where two or more consecutive \n characters will be used as the record separator $ # sub is used to remove ...

View answer
Extract multiple patterns from line regardless of order
Accepted answer
8 votes

With awk (tested with GNU awk, not sure if it works with other implementations) $ cat kv.awk /appID/ { for (i = 1; i <= NF; i++) { $i ~ /^port=/ && (a = $i) $i ~ /^...

View answer
How to grep for a specific word and grab text
Accepted answer
8 votes

You could use -B1 to print previous line as well and then grab only the first line: $ grep -B1 'Minion' ip.txt T5F6Z12: Minion did not return. [Not connected] $ grep -B1 'Minion' ip.txt | head -n1 ...

View answer
Extract data fields from multiple lines
1 votes

Both awk and python can be a good fit here. They both support associative arrays (or dictionaries) and both have regex built-in. There's perhaps some generic way to approach them, but I tend to ...

View answer
grep does not colorize properly
0 votes

Since strace output is on the standard error stream, use strace a.out 2>&1 | grep --color=always "mmap" or, if you're using bash, strace a.out |& grep --color=always "mmap&...

View answer
1
2 3 4 5
10