Hot answers tagged

7

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


5

User csvsql from csvkit: csvsql -HS -d' ' --query 'select max(a) from file where b<400' file For tab separated content, use -t instead of -d' ' or awk: awk ' $2<400 && $1>max1{max1=$1} $2>400 && $1>max2{max2=$1} END {printf "%s (second column value < 400)\n%s (second column value > 400)\n",max1,max2} ' ...


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


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.


3

Using awk and assuming that the first line of show version is a blank line (making it three header lines): awk ' NR<4{ print; next } # print 3 header lines NF>1{ # if number of fields > 1 sub($4, $4 "%s" ) # append `%s` to 4th field fmtstr=$0 # save modified line in `fmtstr` next ...


3

Tested and worked fine command awk 'BEGIN{sum=0}($2 < 400 && $1 > sum){sum=$1}END{print sum}' filename;awk 'BEGIN{sum=0}($2 > 400 && $1 > sum){sum=$1}END{print sum}' filename output 35.7136 35.7337


3

Your pattern will never match because it's looking for a space in field two, but as fields are separated by spaces these characters can never be in a field. If you're looking for field two to contain only digits, you can use this $2 ~ /^[[:digit:]]+$/ If you want to guarantee it has space following it, you can check that field three is non-zero $2 ~ /^[[:...


3

The issue is that $4 in the awk program is part of a double quoted string. Since it is in double quotes, the (local) shell will expand it before calling ssh. In the shell, $4 is the 4th positional parameter, which most likely is empty. The means that the $4 in the awk code will be replaced by an empty string, producing an invalid awk program. In this ...


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

Using Miller $ mlr --nidx --repifs filter '$2 < 400' then stats1 -a max -f 1 data 35.713600 $ mlr --nidx --repifs filter '$2 > 400' then stats1 -a max -f 1 data 35.733700


2

This will work using any awk in any shell on every UNIX box: $ awk '/^ *\[\.\//{title=$1} /^ *input =/{print FILENAME ":", title, "=", $NF}' */*/assessment 01/to/assessment: [./input_file_01] = 'input_file_01.i' 02/to/assessment: [./input_file_02] = 'input_file_02.i' 02/to/assessment: [./input_file_02b] = 'input_file_02b.i'


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

This will work using any awk in any shell on all UNIX boxes: $ isubsCount=0.052 $ awk -v val="$isubsCount" 'BEGIN{exit !(val >= 1)}' $ echo $? 1 $ if awk -v val="$isubsCount" 'BEGIN{exit !(val >= 1)}'; then echo "yes"; else echo "no"; fi no $ isubsCount=1 $ awk -v val="$isubsCount" 'BEGIN{exit !(val >= 1)}' $ echo $? 0 $ if awk -v val="$isubsCount"...


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

$ cat tst.awk NR==FNR { map[$1,$2]=$5; next } ($1,$4) in map { $2=map[$1,$4]; print } $ awk -f tst.awk FS=',' marker-info FS=' ' output11.bim 1 rs2980300 0 775852 T C 1 rs4245756 0 1120590 C A or if you prefer to set FS to the 2 separate values within the script: $ cat tst.awk BEGIN { FS="," } NR==FNR { map[$1,$2]=$5; next } FNR==1 { FS=" "; $0=$0 } ($1,$...


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

If you three or more as you could use: a{3,}. For example: $ echo a | grep -E 'a{3,}' $ echo aa | grep -E 'a{3,}' $ echo aaa | grep -E 'a{3,}' aaa $ echo aaaa | grep -E 'a{3,}' aaaa $ echo aaaaaaaaaa | grep -E 'a{3,}' aaaaaaaaaa If you want 3 or more as followed by something that's not a t, you could use a{3,}[^t]. For example: $ echo aaa | grep -E 'a{3,...


1

For scenario1 Below command will work, Tested and worked fine awk 'NR==FNR{a[$0];next}!($0 in a){print $0}' file1 file2 > diff output 5639166|99|COVER, DOME TRAY 12X20 PCRBN|COVER, CLEAR SIDE CU|T OUT W/ HINGED DOOR LID|A


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