Tag Info

Hot answers tagged

8

Why use awk at all? date +"%b %d %Y" gives you the values without the hassle.


7

You need to use matching operator ~, not subtraction operator -: $ awk '$2 ~ /^2$/' file or use equality operator == like @glenn jackman's answer. But let take a look at your previous solution, to explain that why you got all the lines. awk '$2 - /^2$/ {print}' numbers.txt Here, with each line input, if expression $2 - /^2$/ is true, you will print ...


7

x is your awk variable, $x is something different.... $0 corresponds to the input string contents of the line just read, while $1...$n correspond to the breakdown fields of $0 contents using the default separator. awk lets you to programmatically access the fields using a variable, i.e., if the variable MyValue contains 5, $MyValue is the same as $5. What ...


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 ...


6

I'm not sure this is better than doing it in memory, but with a sed that reads out its infile for every line in its infile and another on the other side of a pipe alternating Hold space with input lines... cat <<\IN >/tmp/tmp Row1,10 Row2,20 Row3,30 Row4,40 IN </tmp/tmp sed -e 'i\ ' -e 'r /tmp/tmp' | sed -n '/./!n;h;N;/\n$/D;G;s/\n/ /;P;D' ...


5

Here's how to do it in awk so that it doesn't have to store the whole file in an array. This is basically the same algorithm as terdon's. If you like, you can even give it multiple filenames on the command line and it will process each file independently, concatenating the results together. #!/usr/bin/awk -f #Cartesian product of records { file = ...


5

Try piping the output from the date command instead, like so: $ date | awk '{print $2, $3, $6}' Dec 1 2014 If you truly want to take the output from date using a command similar to yours then you'll need to redirect it using a HERESTRING, aka. (<<<) (assuming a zsh shell or a fairly recent version of ksh93 or bash). $ awk '{print $2, $3, $6}' ...


5

You could do it by using a simple join: join A.txt B.txt But, in order to work both files must be sorted on the join key (here the first (blank separated) field). To do it, just use sort -b filename.


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.


4

Well, you could always do it in your shell: while read i; do while read k; do echo "$i $k"; done < sample.txt done < sample.txt It is a good deal slower than your awk solution (on my machine, it took ~11 seconds for 1000 lines, versus ~0.3 seconds in awk) but at least it never holds more than a couple of lines in memory. The loop above ...


4

Try: $ awk '1;/PPP/{exit}' file AAA BBB JJJ OOO 345 211 BBB OOO OOO PPP


4

You could do something like this: awk -v low=24 -v high=655 'NR >= low && NR <= high{if (!a[$0]++) print; next};{print}' file or better still awk -v low=100 -v high=200 'NR < low || NR > high || !a[$0]++' test.file


4

You can use awk to split the first column: ~$ awk '{split($1,a,";"); $1="";for (i in a){print a[i],$0}}' myfile 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 0 0 0 0 36 0 0 52 0 0 0 6 0 You split the first column on ; (split($1,a,";")) and then you ...


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 ...


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

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: ...


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 ...


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 }'


4

Here is another approach: awk -F '[ \t*]' -v OFS='\t' 'NR==1{$2="coord\t"$2;print;next}{$1=$1}1' 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.


3

~ is the pattern match operator, not -. But if you're testing for equality, use the equality operator == awk '$2 == 2' numbers.txt


3

You can do it using the sub function in awk: awk 'sub(/./,"& ",$2)1;' file If you want tab-separated output, you can use: awk -v OFS="\t" 'sub(/./,"&\t",$2)1;' file Or in a variety of other tools: Perl perl -alne '$F[1]=~s/./$& /; print "@F"' file Or, for tab-separated output: perl -alne '$F[1]=~s/./$&\t/; print join "\t",@F' file ...


3

awk -F ' *\t' '{print $1 ","}' Or: sed $'s/ *\t.*/,/'


3

Try: $ awk ' { gsub(/[^[:alpha:] ]/,""); for(i=1;i<=NF;i++) { a[$i] = a[$i] ? a[$i]", "FNR : FNR; } } END { for (i in a) { print i": "a[i]; } }' file | sort A: 3, 4 Are: 5, 6, 8 B: 6, 10 BFG: 5 C: 1, 7, 8 CB: 2 How: 5 You: 5


3

Variable assignment in shell syntax is not allowed to have spaces. last_char = ... is a command called last_char with the arguments = and ..... last_char=... is an assignment that stores ... in the shell variable last_char. This is a much more efficient and direct way to do what you want: case $2 in */) ap_src_path=$2;; *) ap_src_path=$2/;; esac ...


3

Since you have also tagged awk: awk 'FNR == NR {a[$1] = $0; next}; {print a[$1]}' A.txt B.txt I don't think a single grep can do this, but a combination of xargs and grep: xargs -I{} grep -Fw -- {} A.txt < B.txt


3

This should do what you want. Basically, we put the first field (the keys) in $k and the rest of the fields in $f by splitting on \t and limiting the split to the first 2 fields. Then we split the keys on ; and print each key along with the remaining fields on its own line. perl -nle '($k, $f) = split "\t", $_, 2; print "$_\t$f" for split ";", $k' -n ...


3

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


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 ...


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 ...



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