7

With GNU sed, replacing all spaces at eol by underscore: sed ':x;s/ \( *\)$/_\1/;tx'


4

More efficient to use perl: perl -lpe 's/(\s+)$/"_" x length($1)/e' input.txt which only has to do one substitution per line with trailing whitespace, instead of looping.


4

One possible way (may not be the best) awk -F'|' ' BEGIN{OFS=FS} NR==1 { for(i=1;i<=NF;i++) if($i=="*") a[i] } { for(i=1;i<=NF;i++) if($i=="*" && !(i in a)) $i="-" } 1 ' file a|s|d|f|g|*|A|*|*|g|c|a|*|A|* a|-|-|f|g|*|-|*|*|g|c|a|*|A|* -|s|-|f|g|*|a|t|*|g|c|a|*|A|* a|s|d|-|g|*|T|*|C|g|c|a|a|A|T


2

$ awk 'NF==5{ if ($2==$4) $2 = $2 OFS 11 OFS $4; else if ($1==$3) $2 = $2 OFS $1 OFS 5 } 1' file 3 1 11 1 10 1 6 9 4 11 4 2 4 1 9 2 11 2 2 2 1 2 4 2 5 2 2 2 9 4 9 2 9 1 2


2

Is this what you're asking for help with? $ cat tst.awk /^ / { sub(/.*]/,"") fnames[$1] next } { if (NR>1) prt(); key = $2 } END { prt() } function prt() { printf "%s", key for (fname in fnames) { printf " %s", fname } print "" delete fnames } $ awk -f tst.awk file accountBrand 20200211204415-create-accountBrand....


2

Your log function is the culprit. What it does is: read one line¹, and if the line is not empty, print a timestamp and then the line content. That's all it does: once it's processed one line, it returns. When cp emits a first error message, the log function reads it and processes it. Since the log function then returns, the process at the right-hand side of ...


2

awk is not the only tool in the toolbox, of course. Here's Miller in action: % mlr --ixtab --ips : --opprint cat << END Student Name: abc Roll Num: 123 Student Name: xyz Roll Num: 124 END Student Name Roll Num abc 123 xyz 124 % You are doing a conversion from XTAB format (-ixtab) to PPRINT format (-opprint).


2

With perl: $ perl -lane 'push @{$vals{$F[4]}}, @F[0..3]; END { $, = " "; for $v (sort keys %vals) { print @{$vals{$v}}, $v } }' input.txt 9 4 2 4 9 2 2 2 1 3 1 10 1 6 With awk: $ awk '{vals[$5] = vals[$5] $1 " " $2 " " $3 " " $4 " "} END { for (v in vals) print vals[v] v }' input.txt 9 4 2 4 9 2 2 2 1 3 1 10 1 6


2

The line of headers needs to be scanned to find all "not *". That a column "has not" an * could be stored in an array a[]. For all next lines, only the columns that exist in a[] may need change. That could be implemented as: awk -F'|' 'BEGIN{OFS=FS} NR==1 { for(i=1;i<=NF;i++) if( $i != "*" ) a[i] } ...


1

Your code fails due to never reading the file. The special BEGIN block is executed before the first input file named on the command line is even opened for reading (and the END block after the last). You also don't have any output from the code. Others have given their own solutions to the issue, so I'll take your code and modify it slightly: Run the code ...


1

try: gawk '{ for (i=2; i<=NF; i++) { delete arr; split($(i-1) "\n" $i, arr); asort(arr); s[arr[1] FS arr[2]]++ }; } END { for(x in s) print s[x], x }' infile |sort -nr 3 is this 2 my name 1 our text 1 is text 1 is our 1 is name split() function adds pair of strings (separated by whitespaces (tab/space)) into an ...


1

sed -n '1h;/Average.*all/{H;x;s/.*(\([^)]*\)).*(\([^)]*\))\n.*all/\1 \2/p;}' /tmp/jeg should work in any version of sed. The h command on the first line copies its content to the "hold space". Then on the last line (the one matching /Average.*all/, not $ because the file may contain extra empty lines at the end), H;x; (append to the hold space + exchange ...


1

A (mostly-)sed solution: cat "$@" | tr -cs -- '._[:alpha:]' '[\n*]' | sed -n -e 'h; :ms' \ -e 'p; :ss' \ -e 's/\([[:lower:]]\)[[:upper:]][[:lower:]]*$/\1/p; t ss' \ -e 's/\([[:lower:]]\)[[:upper:]][[:upper:]]*$/\1/p; t ss' \ -e 's/\([[:upper:]]\)[[:upper:]][[:lower:]]\+$/\1/p; t ss' \ ...


1

Take your pick: $ awk -v RS= -F': |\n' -v OFS='\t' 'NR==1{print $1, $3} {print $2, $4}' file Student Name Roll Num abc 123 xyz 124 $ awk -v RS= -F': |\n' -v OFS='\t' 'NR==1{print $1, $3} {print $2, $4}' file | column -s$'\t' -t Student Name Roll Num abc 123 xyz 124 $ awk -v RS= -F': |\n' -v fmt='%-13s %-13s\n' 'NR==1{printf ...


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