Tag Info

New answers tagged

-1

Replacement of one substring by another is clearly work for sed. If you want it in awk, I assume you wish to do some additional processing by awk. To do so, you should just use the regex field separator: awk -F' (** |)' '{.you code here.; print $10}'


2

No idea why people are using cat to pipe the file into awk, and the tr answer only translates one character into another, so any * is converted to a space. Simple awk solution awk -F"**" '$1=$1' OFS="\t" file For tab between all fields awk 'sub(/\*\*/," "){$1=$1}1' OFS="\t" file And one more awk 'gsub(/(*| )+/,"\t")' file Sed command sed 's/[* ...


0

cat text.txt |awk '{ split($9,a , "**"); printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",$1,$2,$3,$4,$5,$6,$7,$8, a[1], a[2],$10); }' Output is: chrXV 234346 234546 snR81 + SNR81 chrXV 234357 0.0003015891774815342 0.131826816475 + chrXV 234346 234546 snR81 + SNR81 chrXV 234385 0.0002208827994288481 0.0118547789578 + ...


0

Does it have to be awk? Why not cat file | tr '**' ' ' Just replacing the **'s with three spaces


0

@jimmij's answer is good from the top options point of view, though it looks like the format of the output from top can't be relied upon in a portable manner. You mention you don't have the free utility, but this is just a wrapper around the /proc/meminfo interface, which you almost certainly do have. Why not just get the information you need directly from ...


1

Try this: top -bn1 | awk '$1=="Mem:" {print $6" "$7}'


1

An awk solution: $ awk -F',' '$2 == 2 {$4 = "-"$4}1' OFS=',' file abc,0,def,1234,ijk xyz,2,def,-2345,abc ijk,2,def,-5678,ijl A perl equivalent: $ perl -F',' -anle '$F[3] = "-".$F[3] if $F[1] == 2; print join ",", @F' file


1

The simple case you show is trivial. Your sequences are never more than a single line so you can simply use grep to search for each of your IDs and the line after them: grep -Fwf list.txt -A 1 seq.fasta | grep -v '^--$' > out.fasta The grep -v '^--$' simply filters out the lines with -- that grep adds between groups of output lines when using the -A ...


2

Try to use system() function: awk '{printf("%s ",$1); system("d2h " $2)}' file In your case system will call d2h 247808 and then append output of this command to printf output: Mike 3C800 EDIT: as system uses sh instead of bash I can't find a way to access .bashrc. But you can still use functions from your current bash script: #!/bin/bash d2h() { ...


3

With join and sort: join -t , <(sort given.csv) <(sort family.csv) Output: 123,John,Lennon 246,Paul,McCartney 369,George,Harrison 987,Ringo,Starr With grep: #!/bin/bash F1="given.csv" F2="family.csv" D="," # delimiter while IFS="$D" read FIRST_COLUMN REST; do T="$FIRST_COLUMN$D$REST" T+="$(grep -oP "^$FIRST_COLUMN\K$D.*" ...


2

Try: $ awk '$3 ~ /^r/ {$2 = $3}1' file 223 898 teeth 223 899 mouth 223 r9 r9 445 round round This combines the searching and the printing into a single awk command, rather than have to make use of grep & awk. The above $3 ~ /^r/ will find only lines where the 3rd field begins with a r. In those situations awk will substitute the 3rd field for the 2nd ...


2

Try: awk -F'[|] |,' '{getline p; split(p,H," "); print $2,H[2]}' file Which would rely on no additional commas in the | separated fields. A bit more robust in that sense would be: awk -F'[|] *' '{getline p; split($5,Q,","); split(p,H," "); print Q[1],H[2]}' file A sed alternative: sed 'N; s/.*| *//; s/,.*\n#//; s/ hits found//' file


2

Assuming that your input file is consistent: awk -F'[|,]' '/genome/ {printf "%s ", $5; next} {print substr($1,3,1)}' input.txt > output.txt Chlamydomonas eugametos genome 0 Pedinomonas minor genome 1


0

The first thing you need to do is remove the unwanted lines from the output of showq -r, i.e. lines that say active jobs, etc ..which can be done using showq -r | sed '1,2d' |sed '$d', or you can use grep with a regular expression that describes part of the data row - e.g. grep "----" in this case may work if value of EFFIC is always ----. Once you've got a ...


0

At first store the following script and chmod +x to it.suppose name it job_processor.awk: #!/usr/bin/gawk -f ## $7 username ## $8 groupname ## $10 procs { if ( $10 == 1 ) printf ("username: %s and groupname: %s are serial!\n",$7,$8); else if ( $10 > 1 ) printf ("username: %s and groupname: %s are parallel!\n",$7,$8); printf ("username: %s ...


5

Here are a few approaches, but none will approach the speed of the cut and nl solution above: awk awk -F, '{$1=NR;print $1","$2;}' file.csv > newfile.csv Perl perl -pe 's/[^,]+/$./' file.csv > newfile.csv or perl -F, -ane '$F[0]=$.; print join ",", @F' file.csv Shell (but I don't recommend it for a 200G file, it will take ages) i=1; while ...


10

Not near a terminal to test, but how about the oft-overlooked nl command? Something like: cut -f 2 -d , original.csv | nl -w 1 -p -s , > numbered.csv


2

You could also use sed: sed -e :1 -e 's|^\([^:]*[ /]\)\([0-9][/:]\)|\10\2|;t1' Inserts a 0 before any single digit in between a space or / and / or : in the part leading to the first :.


2

You specify the width and zero padding using printf modifiers in awk: $ awk -F'[/:]| +' '{ printf "%s %02d/%02d/%02d %02d:%02d:%02d %s %-13s %s %s %s %s %s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15 } ' input_file AA 09/07/2014 02:30:38 PM AA0000011111 08 Y A Jane, Doe AA 09/07/2014 11:30:31 AM AA0000011112 09 ...


3

If you have the GNU implementation of awk or mawk 1.3.4-20121129 or newer, try: $ awk ' { split($2,a,"/"); split($3,b,":"); split(strftime("%m/%d/%Y %H:%M:%S",mktime(a[3]" "a[1]" "a[2]" "b[1]" "b[2]" "b[3])),c); $2 = c[1]; $3 = c[2]; print; } ' file AA 09/07/2014 02:30:38 PM AA0000011111 08 Y A Jane, Doe AA 09/07/2014 11:30:31 AM ...


0

With GNU awk you can set the record separator RS to the word to be replaced delimited by word boundaries. Then it is a case of setting the record separator on the output to the replacement word for the first k records while retaining the original record separator for the remainder awk -vRS='\\ylinux\\y' -vreplacement=unix -vlimit=50 \ '{printf "%s%s", $0, ...


1

Whats wrong with the join command ? join file1 file2 Gives the required result


1

A short alternative in Perl: perl -pe 'BEGIN{$n=3} 1 while s/old/new/ && ++$i < $n' your_file Change the value of `$n$ to your liking. How it works: For every line, it keeps trying to substitute new for old (s/old/new/) and whenever it can, it increments the variable $i (++$i). It keeps working on the line (1 while ...) as long as it has ...


1

I would do this in Perl: #!/usr/bin/env perl use strict; my (%file1,%file2); ## Open the 1st file open(A,"file1"); while(<A>){ ## Remove trailing newlines chomp; ## Split the current line on tabs into the @F array. my @F=split(/\t/); ## This is the tricky part. It adds fields 2-last ## to the hash $file1. The value of this ...


3

Say you want to replace only the first three instances of a string... seq 11 100 311 | sed -e 's/1/\ &/g' \ #s/match string/\nmatch string/globally -e :t \ #define label t -e '/\n/{ x' \ #newlines must match - exchange hold and pattern spaces -e '/.\{3\}/!{' \ #if not 3 characters in hold space do -e 's/$/./' \ ...


2

Using awk USING FUNCTIONS Legible: awk 'function get(file,x,y) { while ( (getline < file) > 0) {if ($1==x)y,substr($0,index($0," ")+1)} close(file) } ARGV[1]==FILENAME{get(ARGV[2],$1,$0)}' file1 file2 Single Line: awk 'function g(f,x,y){while((getline <f)>0)if($1==x){print y,substr($0,index($0," ...


1

A simple, but not very fast solution is to loop over commands described in http://stackoverflow.com/questions/148451/how-to-use-sed-to-replace-only-the-first-occurrence-in-a-file for i in $(seq 50) ; do sed -i -e "0,/oldword/s//newword/" file.txt ; done This particular sed command probably works only for GNU sed and if newword is not part of oldword. ...


3

Using Awk The awk commands can be used to replace the first N occurrences of the word with the replacement. The commands will only replace if the word is a complete match. In the examples below i am replacing the first 27 occurrences of old with new Using sub awk '{for(i=1;i<=NF;i++){if(x<27&&$i=="old"){x++;sub("old","new",$i)}}}1' file ...


3

The first section belows describes using sed to change the first k-occurrences on a line. The second section extends this approach to change only the first k-occurrences in a file, regardless of what line they appear on. Line-oriented solution With standard sed, there is a command to replace the k-th occurrance of a word on a line. If k is 3, for ...


0

Yet another way: pr -a7 -l1 -s";" filename which is short for pr --columns=7 --across --length=1 --separator=";" filename


4

Do you specifically require an awk solution?  join file1 file2 > file3 will do exactly what you want.


5

Try: $ awk 'FNR==NR{a[$1]=$2;next}{$1 = $1"\t"a[$1]}1' OFS='\t' file1 file2 1 today alot 1 today sometimes 2 tomorrow at work 2 tomorrow at home 2 tomorrow sometimes 3 red new


2

awk '{ ORS=NR%7?",":"\n" }1' is another way with awk.


5

If you're not stuck on sed or awk, use paste echo "R1 R2 R3 R4 R5 R6 R7 R1 R2 R3 R4 R5 R6 R7" | paste -d, - - - - - - - R1,R2,R3,R4,R5,R6,R7 R1,R2,R3,R4,R5,R6,R7 There's no difficulty with your 2nd sample, just have to quote the semicolon so the shell doesn't see it: paste -d ';' - - - - - - - <filename 00000;ND00000056888;Doe, Jane J;F;99 ...


4

try awk '{printf "%s%s",$0,NR%7?",":"\n" ; }' the big part NR%7?",":"\n" : if-then-else : if (NR%7) : NR, (Number of Record) % (modulo) 7 (divide by 7 != 0 ) then printf a ',' else new line. $0 is the whole line


3

Try: $ awk -F'[:}]' '$(NF-1) >= 1334575352456 && $(NF-1) <= 1334575353456' file {"a":"1", "b":"2", "c":"4", "time":1334575352456} -F'[:}]' set fields delimiter is : or }, so you can access the the time value by accessing second from last field, using $(NF-1). To save ouput, use: $ awk -F'[:}]' '$(NF-1) >= 1334575352456 && ...


0

pythonpy (https://github.com/russell91/pythonpy) works well for this sort of thing: cat file.txt | py --ji -l 'min(l), max(l), numpy.median(l), numpy.mean(l)'


1

I also throw perl at everything but will sometimes add sed for flavor: $ sed 's/^\w/\n&/' file | perl -000ne 'print if /apollo/' com.apollo.apollonetworkcheck 1026ms running, 88 wakeups 88 alarms: flg=0x14 The sed will add an extra newline between each record. It simply looks for lines that begin with an alphanumeric character (^\w) and the ...


2

Your idea to use a regex record separator is elegant, but remember that awk will consume the corresponding text, which in your case will be the first non-whitespace character of the following record. If your system has the GNU version of awk, you can access the most recently matched RS via the RT variable, however you would still need to save the result in ...


3

Nice markup: awk '!/^#/ && NF { split($2,a,"-") printf "%s - - [%s/%s/%s:%s] \"%s %s\" %s %s\n", $1, a[3], a[2], a[1], $3, $4, $5, $6, $7 }' file If your input is just the lines starting with 10.000... this makes it: $ awk '{printf "%s - - [%s:%s] \"%s %s\" %s %s\n", $1, $2, $3, $4, $5, $6, $7}' file 10.000.000.000 - - ...


1

with a awk and a parameter (file filter.awk) BEGIN { p=0 ; } # no printing { if ( (substr($0,1,1) != " ") && (substr($0,1,1) != "\t" ) ) p=0 ; # if no blank stop printing if ( index($0,name) > 1 ) p=1 ; # pattern found ? if (p) print ; } $0 is the entire line substr($0,1,1) is first caracter of line and the result awk -v name=apollo ...


3

I always throw perl at everything :-) perl -ne 'if (/^\s/) { $x.=$_ }else{print $x if $x=~/apollo/; $x=$_} END {print $x if $x=~/apollo/}' file.txt Edit: explanation of the one-liner: -n implies a loop over the input, not printing by default at the end of each loop (-p is the same but does print the line by default). -e specifies an expression, or code ...


1

sed '/^[>0-9]/h;s/.*>\(.*[0-9]\).*\*/[\1 ]P /p $s/.*//;/^[>0-9[]/d;g;s/ .*/ 1+pc/ ' <<\DATA | dc >Cluster 423 0 56aa, >HWI-ST1448:257:C3V2HACXX:1:1106:19087:2550.1... at 92.86% 1 64aa, >HWI-ST1448:257:C3V2HACXX:1:1106:15943:81371.1... * 2 41aa, >HWI-ST1448:257:C3V2HACXX:1:1106:12438:91360.3... at 90.24% 3 45aa, ...


0

grep "\*" file.txt |grep -E "(?<=>)[\w+\s\W]+"|sed 's/\.\.\.\*//'


1

perl -F'\n' -lan00e 'print "$1\t$#F" if />(.*)\.{3} \*$/m'


1

Here’s a somewhat rough cut (with no error handling): awk '/\*$/ { save_id = substr($3, 2, length($3)-4) } /^[0-9]/ { save_num = $1 } NR > 1 && /^>/ {print save_id, save_num+1 } END {print save_id, save_num+1 } ' data_file On a line that ends with * (i.e., that matches /*$/), extract the group ID from the third word, ...


6

This is not answering your question, but it solves the problem you are trying to solve, in a completely different way: The full command is this (see below for example output): ps -o comm,%cpu,%mem --sort -%cpu -A | head -6 I will describe the parts of it: using ps to have more control about the output Printing only the three columns we need with -o ...


5

top command | awk 'BEGIN { print header; count=5} NR>=8 { if ($0 ~ /your top regex/) next; print fields; if (--count == 0) exit}' On every line starting with #8, if it matches top, ignore it. Otherwise, print the portion of it that you want. The fifth time you print a line (that doesn’t match top), exit.


1

Arguably simpler than glenn’s answer (although it does require typing “Start of script” twice): tac logfile | awk '/End of script/,/Start of script/{print} /Start of script/{exit}' | tac or tac logfile | sed -n '/End of script/,/Start of script/p; /Start of script/q' | tac


3

Perhaps a little state machine: tac file | awk '/End of script/ {p=1} p {print} p && /Start of script/ {exit}' | tac



Top 50 recent answers are included