Tag Info

New answers tagged

4

Not 100% the same output as you asked, but hopefully close enough: function percent(value, total) { return sprintf("%.2f", 100 * value / total); } { label[NR] = $1 for (i = 2; i <= NF; ++i) { sum[i] += col[i][NR] = $i } } END { title = label[1] for (i = 2; i <= length(col) + 1; ++i) { title = title "\t" ...


1

The following just uses nl mainly, though tr gens a bunch of blank lines and sed prepends the REQ bit... IFS=\ read -r l r <file tr \\0 \\n </dev/zero| nl -ba -w4 -nrz -s" $r"| sed "s/^/${l%Q*}Q/;4096q" First it saves the two halves in $l and $r with read. Then it translates 4096 nuls into \newlines - which nl counts and appends $r to, and last ...


1

This prints the whole line by substituting a new value for field 2 and should work regardless of how many fields may appear later in the line. awk -F 'REQ| ' '$1 == "(REF-" {field1 = $1; $1 = ""; num = $2; for (i = 0; i < 4096; i++) {$2 = sprintf("%sREQ%04d", field1, num + i); line = $0; sub("^ ", "", line); print line}; next} {print}' inputfile Broken ...


3

perl would be good for this: perl -ne 'for $n (1..4096) {s/\d+/ sprintf "%04d", $n /e; print}' File > File2


1

I'm not sure if BOF and EOF are parts of the file, but anyhow you can try the following awk code: awk 'BEGIN{FS="REQ| "}/[[:digit:]]/{for(i=0;i<4096;i++){printf($1"REQ%04d "$3"\n",$2+i)};next}1'


2

In awk: awk -F '' -vOFS='\t' '{$1=$1}1' file > new_file Borrowed the idiom {$1=$1}1 from an answer to one of your other questions. This sets the field separator to nothing (-F ''), which means that each record is read character-by-character. The output field separator is set to a TAB character (-vOFS='\t') and the idiom {$1=$1}1 is (as far as I can ...


0

I think you should give fold a go: tr \\n \\r <infile | fold -w1 | tr '\r\n' '\n\t' >outfile I preprocess fold's input w/ tr by replacing the instream \newline characters w/ \returns. fold is printing a \newline character for every column in input - each of your capital letters - but it resets its counter on \returns and so when tr does the final ...


5

If I have understood your intention correctly then this does it: sed -e 's/./&\t/g' -e $'s/\t$//' file The second replacement deletes the tab at the end of the line.


2

Try doing this using perl: perl -ne 'print join "\t", split //' file > new_file


4

Is the use of awk an absolute requirement?  This seems more like a job for sed: sed '1s/gene/& coord/;2,$s/\*/ /' which is just about self-explanatory: 1s/gene/& coord/ – on the first line, change “gene” to “gene    coord”. 2,$s/\*/ / – on the second line through the end of the file, change literal “*” to white space.


0

The next awk statement will skip the current line, that is useful if you have to match multiple blocks in your script. awk ' /^#/ {next} / pattern 1 / { } / pattern 2 / { } ' filename


1

Using grep: grep -vE "^#" or grep -E "^[^#]"


-1

sed 's/#.*//' This gets rid of comments, even if they don't start at the first column.


1

awk -F: '/^[^#]/ { print $2 }' /etc/oratab | uniq


4

Here is another approach: awk -F '[ \t*]' -v OFS='\t' 'NR==1{$2="coord\t"$2;print;next}{$1=$1}1' file


4

The header line is a special case, so you can handle it as a special case. awk -F '*' -v OFS='\t' \ 'NR == 1 { sub(/^gene/, "&" OFS "coord"); print; next } { print $1, $2 }'


0

Note that I used your "raw" input without your whitespace modifications: you don't actually specify that your input field separator is a tab, so awk will use "whitespace" as the field separator -- thus is doesn't matter that you inserted some extra tabs or whatever. awk -v OFS="\t" ' NR == FNR {ref3[$2] = $3; ref4[$2] = $4; next} $1 in ref3 { ...


3

Awk way with one file open(for completeness) awk '{a[NR]=$0;x+=(b[NR]=$2)}END{while(++i<=NR)print a[i]"\t"100*b[i]/x"%"}' file foo 10 10% bar 20 20% oof 50 50% rab 20 20% This will use more memory than the others but should be faster This reads the line into array a and field two into array b. Then increments x by ...


7

To create the table with a single call to awk: $ awk 'FNR==NR{s+=$2;next;} {printf "%s\t%s\t%s%%\n",$1,$2,100*$2/s}' data data foo 10 10% bar 20 20% oof 50 50% rab 20 20% How it works The file data is provided as an argument to awk twice. Consequently, it will be read twice, the first time to get the total, which is ...


2

The "simple" way of doing this would be to call awk twice: once to get the total, another time to calculate the ratios. $ total=$(awk 'BEGIN{ total=0 } { total=total+$2 } END{ printf total }' data) $ awk -v total=$total '{ print $1 "\t" $2 "\t" 100*$2/total "%" }' data Now I'm sure someone will come up with a one-liner somehow...


0

Since this command returns a JSON string, I'd use a JSON parser, jq url="https://api.github.com/users/USER/repos" curl -s "$url" | jq '.[]|.ssh_url' | tr -d '"' That jq script means, for each element of the array, give me the value of the "ssh_url" member. The use tr to delete double quotes.


1

sed -n '/ssh_url/p' prints all lines which contain the string ssh_url. That's not what sed is usually used for. This would be done with grep (and with awk being used, too, awk can do that itself): grep ssh_url awk -F\" '/ssh_url/ {print $(NF-1)}' -F\" means that awk separates the content of a line at " chars. NF is the number of fields i.e. there are ...


1

This line strikes me as having two problems: grep "$2" | awk '/[0-9]{4},[0-9]{4}.*/' dcmResults.txt >> ~/export/"$1"/tagResults.txt You can do the whole thing in grep: grep -e "$2" -e '([0-9]\{4\},[0-9]\{4\})' dcmResults.txt >> ~/export/"$1"/tagResults.txt Where is grep getting its input, and where is the output of grep going to? Since awk ...


2

Gnu Awk only started including interval expressions (your {4} qualifying the [0-9]) in 4.0: Interval expressions were not traditionally available in awk. They were added as part of the POSIX standard to make awk and egrep consistent with each other. Initially, because old programs may use ‘{’ and ‘}’ in regexp constants, gawk did not match interval ...


4

Suppose you have the file: ~$ cat f 1,AA,2 2,BB,2 3, AA, 1 C, BA, 1 You can simply use grep: ~$ grep "^[^,], *A" f 1,AA,2 3, AA, 1 But it is possible with awk with the same regexp: ~$ awk '/^[^,], *A/' f 1,AA,2 3, AA, 1 and a better way is to use the field separator (-F',') and test the second field: ~$ awk -F',' '$2~/^ *A/' f 1,AA,2 3, AA, 1 ...


1

Through awk and keeping middle column: awk '{printf("%.f ", ($1/65)+0.5)}1' infile > outfile 24 1533 C_P.doc 11 691 C_M.doc 14 905 G_S.doc 15 945 J_Z.doc 24 1549 J_M.doc 27 1701 L_B.doc Through awk and without middle column: awk '{printf("%.f", ($1/65)+0.5); $1=""}1' infile > outfile 24 C_P.doc 11 C_M.doc 14 G_S.doc 15 J_Z.doc 24 J_M.doc 27 L_B.doc ...


0

If you don't mind using python in shell and assuming that a.txt is your file: [sreeraj@server ~]$ cat a.txt 1533 C_P.doc 691 C_M.doc 905 G_S.doc 945 J_Z.doc 1549 J_M.doc 1701 L_B.doc [sreeraj@server ~]$ for i in $(awk -v c=65 '{ print $1/c }' a.txt) ; do python -c 'print int(round('$i',0))' ; done >> b.txt [sreeraj@server ~]$ paste b.txt a.txt > ...


1

You can use perl: $ perl -MPOSIX=ceil -anle '$F[0] = ceil($F[0]/65);print "@F"' file 24 C_P.doc 11 C_M.doc 14 G_S.doc 15 J_Z.doc 24 J_M.doc 27 L_B.doc


0

I know your question is about the difference in both the commands you had tried, but if you are not insistent about using awk, and for the benefit of anyone else who may stumble upon this post, another method would be to use pgrep -f. kill $(pgrep -f wget) This will kill all the process that is using wget. Use the signal that you wish to use with kill (if ...


1

For improvement, see @Hauke Laging's answer. For the difference between yours solution, the first approach does not run kill command, it just refer to four variables sudo, kill, STOP and $2. At least in gawk, when you refer to variables with no action in a statement, it do nothing. You can easily use dgawk to see that: $ dgawk -f test.awk dgawk> trace ...


3

There is a lot to be improved about your approach: On most systems (if /proc is not mounted with hidepid) you don't need root privilege for ps. There is no need for two grep instances just to get rid of the first in the process list. Do this instead: grep '[w]get' There is no use in grep filtering input for awk. awk can do that pretty well on its own: awk ...


1

Others have provided general answers for your question which demonstrate good ways of parsing json however I, like you, were looking for a way to extract an aws instance id using a core tool like awk or sed without depending on other packages. To accomplish this you can pass the "--output=text" argument to your aws command which will give you an awk parsable ...


1

For simplistic scenarios involving fixed string search and space-separated words in my_text.txt, GNU awk might work, although output order may not match that of words_of_interest.txt awk 'NR == FNR{a[$0]; next}; $0 in a{b[$0]++}; END{for (k in b) print k, b[k]}' words_of_interest.txt RS='[[:space:]]+' my_text.txt hi 2 joe 1


2

If you previous sort your files, you could do in a simple way: $ join file2 file1 ENSG00000223116 0 0 AL157931.1 ENSG00000233440 1.71449394 50 HMGA1P6 To sort your files just do the following: sort file1.txt > file1 sort file2.txt > file2


1

You could get the information from the source (on Linux): awk '/^Cached:/{print $2}' /proc/meminfo This does get the value in kB (for me), so you could convert it for MB: awk '/^Cached:/{print $2/1024}' /proc/meminfo


2

Try doing this for memory cached value : LANG=C /usr/bin/free -m | awk 'NR==2{print $7}'


2

The confusion may be caused by the fact that the original author used the same variable name for the shell: benchmark=30;gr... as he/she did inside awk: awk -vbenchmark=$benchmark which may or may not assign the value of the shell variable benchmark to the awk variable benchmark. Once awk has this value (30 in this case), awk checks that the 6th ...


2

If you do: awk -v benchmark=30 '...' That is the same as: awk 'BEGIN{ benchmark = 30 } ... ' This is used to set an initial value for that variable. Though I don't see why the author does: benchmark=30; ... | awk -v benchmark=$benchmark .. They might as well do: ... | awk -v benchmark=30 ..


4

You could use perl (get the file content and substitute pattern with pattern+file content): perl -pe '$text=`cat insert.txt`; chomp($text); s/PAT/$&$text/' file.txt add -i to edit in place; g to append after each PAT (pattern) occurrence, e.g.: perl -i -pe '$text=`cat insert.txt`; chomp($text); s/PAT/$&$text/g' file.txt Another way, using ed: ...


2

This feels like it could be done in a simpler way, but the best I can come up with after an hour of head-scratching is this python script: #! /usr/bin/env python3 import sys, os class Block: block_id = '' source1 = '' source2 = '' mixtures = [] def __init__(self, block_id = '', source1 = '', source2 = '', mixtures = []): ...


4

So it would be a little tricky to make this work portably in sed - you should be looking to cut and/or paste with some regex precursor generating their script in that context - and this is because sed will always insert a \newline before the output of a read. Still, w/ GNU sed: sed '/First/{x;s/.*/cat file/e;H;x;s/\n//}' <<\IN First Second Third IN ...


4

In your linked question there is already good awk answer, just modify it a little bit by using printf instead of print to insert the content without newline: awk '/First/ { printf $0; getline < "File1.txt" }1' infile.txt Result: Some Text here FirstThis is text to be inserted into the File. Second Some Text here You may want to add space or other ...


0

Slow day at work, so here's a python solution python3 -c 'import sys x = (l.partition("\t") for l in sys.stdin) x = ((l[0].split(";"), l[1] + l[2]) for l in x) for a in x: print(*a[0], sep=a[1], end=a[1]) ' <file


2

I know, old topic, but I could not resist - here's yet another short and simple way to do this: $ paste -sd, input.txt EN1,EN2,EN3,EN4,EN5 $ Works on Linux and Solaris, maybe even on other platforms.


0

$ awk '{printf /Employee Id|Employee Name|Buz Code/? $NF" " : " "}' file Prints last field $NF if found any pattern that matched like Employee Id or| ... else? print single space" ". Result in column order: ABC123, Vivek 2098 GGG67, Kumar 2176 OR awk '/Employee Id|Employee Name|Buz Code/ {if(++i%4==0) printf RS;printf $NF FS }' file Note that ...


1

It would seem less like a homework assignment if you showed what you've already tried. That said, here's a start - you need to grab the fields by matching on the field names, but not output them until you hit the next record. It would be convenient if there's a "#" between all records as you show... $ awk -F: -f t.awk t ABC123, Vivek GGG67, Kumar ...


0

For just the minimum awk '{for (;NF-1;NF--) if ($1>$NF) $1=$NF} 1' RS= Example


0

Longer Perl version, that allows you to access the data per key and further process if required. This does also not use capturing from regular expressions. #!/usr/bin/perl use warnings; use strict; my %hash; while ( my $line = <> ){ #splitting the line into two parts at the first TAB. my ($keys, $value) = split (/\t/ , $line, 2); ...


1

Another Perl one-liner. $ perl -pe 's/^([^;]+);([^;]+);(\S+)\s+(.*)/$1 $4\n$2 $4\n$3 $4/' file K00001 0 0 34 0 0 0 0 0 0 0 0 0 0 0 0 0 36 0 0 52 0 0 0 6 0 K00004 0 0 34 0 0 0 0 0 0 0 0 0 0 0 0 0 36 0 0 52 0 0 0 6 0 K00008 0 0 34 0 0 0 0 0 0 0 0 0 ...


3

With sed: sed 's/^\([^[:blank:];]*\);\([^[:blank:]]*\)\(.*\)/\1\3\ \2\3/;P;D'



Top 50 recent answers are included