Tag Info

New answers tagged

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

Here are a few solutions: grep. The pattern searches for lines that begin with either > followed by 2 sequences of non-spaces ([^ ]+ [^ ]+) or any characters (.+). The -o cause grep to only print the matched part of each line: $ grep -oP '^(>[^ ]+ [^ ]+|.+)' file.fa >YAL003W EFB1 MASTDFSKIETLKQLNASLADKSYIEGTAVSQADVTVFKAFQSAYPEFSRWFNHIASKAD ...


1

awk solution $ awk '/>/ { print $1, $2; next } { print }' aa >YAL003W EFB1 MASTDFSKIETLKQLNASLADKSYIEGTAVSQADVTVFKAFQSAYPEFSRWFNHIASKAD EFDSFPAASAAAAEEEEDDDVDLFGSDDEEADAEAEKLKAERIAAYNAKKAAKPAKPAAK SIVTLDVKPWDDETNLEEMVANVKAIEMEGLTWGAHQFIPIGFGIKKLQINCVVEDDKVS LDDLQQSIEEDEDHVQSTDIAAMQKL* />/ search for a > in a line next ; do not read any further ...


0

Use comm! comm -23 /etc/remotedomains remove From the man page: Compare sorted files FILE1 and FILE2 line by line. With no options, produce three-column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files. Options -1, -2 and -3 disable respective ...


1

Another answer, a little simpler than the others: #!/bin/bash fields=$(sed -r -e 's/-1/ /g' -e 's/,/ FS /g' \ -e 's/([0-9]+)/\$\1/g' control_file.txt) awk -F, "{print ${fields}}" $1 The first command converts control_file.txt into a suitable awk command: $1 FS $3 FS $5 FS FS $8 FS FS $4 to run it: $ ./script.sh input.csv col1,col3,col5,,col8,,col4 ...


0

the awk file BEGIN { sp="-1" ; FS="," ; } FILENAME == "control.txt" { for (i=1;i<=NF;i++) col[i]=$i ; next ;} FILENAME != "control.txt" { comma="" ; for (i=1;i<NF;i++) { c=col[i] ; if (col[i]!=-1) printf "%s%s",comma,$c ; else printf "%s%s",comma,sp ; comma="," ; } printf "\n" ; ...


0

echo '1,2,3,4,5,6,7,8' | sed 's/[^,]*//7;s///6;s///2;s/,,/,/ s/\([^,]*\),\([^,]*\),,.*,\(.*\)/\2,,\3,,\1/' That removes the contents of fields 7,6,2. Next it removes the first blank field. Then it swaps the last three nonempty fields like 2,,3,,1. OUTPUT 1,3,5,,8,,4 And run on your sample data: BP ID,CurrentMonetary balance ,Provider contract ...


3

Does the order of fileA matter? Can you have multiple lines in fileB with that pattern? This will for example parse fileA and search for each pattern in fileB: while read i; do grep "$i" fileB; done < fileA But you need to define the problem better to get a solution with more performance. For example it is sufficient to get the whole line, you don't ...


11

You can just use grep: grep -Fwf fileA fileB From man grep: -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) -f FILE, --file=FILE Obtain patterns from FILE, one per line. The empty file ...


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


4

A job for perl: perl -pe 's/\bname\b\K/"_".++$n/ge'


0

In Simple Bash Script #!/bin/bash count=0 search="linux" while read line do for word in ${line} do if [[ $word == $search ]] then printf "${word}_${count} " ((count++)) else printf "$word " fi done echo done < input_file


1

Because this involves arithmetic, this is not a good problem for sed. However, awk can handle it: awk '{for (i=1;i<=NF;i++) { if ($i=="name") {c++;$i=$i"_"c} }} 1' For example: $ echo a name b name name c d name | awk '{for (i=1;i<=NF;i++) { if ($i=="name") {c++;$i=$i"_"c} }} 1' a name_1 b name_2 name_3 c d name_4 Explanation: {for ...


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


4

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


1

If I understand your question correctly, this will do what you want: awk -F, 'NR!=1 { if (max_NF < NF) max_NF = NF; for (i=1; i<=NF; i++) if (max[i] < length($i)) max[i] = length($i) } END { for (i=1; i<=max_NF; i++) printf "%-2d | %d\n", i, max[i] }'


0

I don't see your link for the sample file but you can do this by using awk command. if can specify what ever delimiter you may have and exactly what field you need to count. awk '{ FS = "," } ; { if(NR!=1) gsub(/"/, "", $2) ; print NR "|" length($2) } ' test.csv You can redirect this output to any file 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


0

Use egrep instead of grep for extended functionality. $ last | egrep "(user1|user2)" If your output is long you can make it scroll-able for easy viewing with the "less" pager: $ last | egrep "(user1|user2)" | less


1

You can use something like $ last | grep -E -c 'user1|user2' The -E allows to leave out the \ character that you would otherwise need before the |. If you have many user names listed in a file, one per line, you can read them from the file directly: $ last | grep -F -c -f userlist.txt The -F means the lines in the files are strings that should match ...


2

This is a companion program for less. It is used internally in some configurations (determined at compile time) to call a shell from commands where you can specify a wildcard pattern that stands for a list of file names: the “examine” (:e) command and a few others (-o, -O, -T). The weird quoting is something that less parses internally. The point of using ...


0

For the general case of matching N times: $ perl -ple '$N=3;s/(\S+ ){$N}\K/\n/g' tmp word1 word2 word3 word4 word5 word6 word7


0

Another way in sed: $ sed 's/ /\x1&\x2/g; s/\([^\x1]*[\x1][^\x2]*[\x2][^\x1]*\)[\x1][^\x2]*[\x2]/\1\ /g; s/[\x1\x2]//g'


2

Sorry, seems like I figured it just after posting. It needs to be sed -e 's/\(word. \)\{2\}/&\n/g' tmp It seems the brackets are needed to let sed apply {2} condition on the entire pattern word. and not just preceding space.


0

Pure shell: while read a b rest; do b=$(printf "%05.2f" $b); echo "$a $b $rest"; done < file.txt


5

With awk: $ awk '$2 = sprintf("%05.2f",$2)' file num 00.12 num num num 25.53 num num num 07.82 num num


3

What you are after is probably something like: sed 's/ \([0-9]\.\)/0\1/' It matches a space followed by one digit from the range 0 to 9 followed by a dot and replaces the space by the 0. The parentheses \([0-9]\.\) mark a group which is referenced by \1 in the replacement part of the expression.


2

What if you use awk? $ awk '$2>0 && $2<10 {$2="0"$2}1' file num 00.12 num num num 25.53 num num num 07.82 num num


-1

Or you can do the following: vi filename.txt Esc (to make sure you're in NORMAL mode), and then enter the following command: :1,$ s/^/mystring


0

One trick for things like this – particularly if you're not too handy with scripting – is to generate a script with a spreadsheet. This isn't good practice if you're trying to build a script for repeated use, but it can be handy for a one-off job for someone who hasn't had the time to learn to do scripting another way. It looks like the rename (or link) ...


2

try printf "%-2s%18s%s\n" $acc_type " " $acc_no >> out.csv format string is %2s : two char wide column, left aligned %18s : 18 char wide, will be blank %s a string.


2

I'm assuming you want to search for strings that are present in file2.txt using the strings from file1.txt? If so you can use grep's -f switch to accomplish this. excerpt from grep's man page -f FILE, --file=FILE Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by ...


0

{ echo '>1'; tr -dc '[:alpha:]'; } <infile >outfile You dont need to get the first line out of the file - just echo a header. And if you delete everything but alphabetic characters then you just automatically get what you want.


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


2

The simplest way is to just print the 1st line and then all the other lines of the file that don't contain i) any spaces character (they have no business being in fasta files) and ii) a fasta header line (>): head -n 1 file.fa > newfile.fa; grep -P '^[^> ]+$' >> newfile.fa The head prints the 1st line, and the grep regular expression looks ...


3

awk ' /^>/ { # print the first header if (c++ == 0) {print; print ""} next } /^$/ {next} {printf "%s", $0} END {print ""} ' a.fasta > b.fasta contents of b.fasta >1 AAATTTTGGGGCCCACCCCGGGTTT..........ATGCCCCCCCCCC


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

Another sed: sed '1,/^$/d;/\.ENDS/q' Though - just judging by the data provided, it maybe the .ENDS address is unnecessary. I suppose you could also do: sed '/^\..*COMPARATOR/,/^\./!d'


3

Try this one: sed -n '/\.SUBCKT\ C032_THS_COMPARATOR/,/\.ENDS/p' filename HTH, Cheers,


2

You can use awk: awk '/\.SUBCKT C032_THS_COMPARATOR/{p=1};p;/\.ENDS/{p=0}' file Explanation If we see .SUBCKT C032_THS_COMPARATOR, set p = 1. If we see .ENDS, set p = 0. Depending on p value, we can track current position is inside or outside range, If in, p is 1, meaning true in boolean context, causing awk default action to print the whole input line. ...



Top 50 recent answers are included