Tag Info

Hot answers tagged

6

Just make use of FILENAME built-in variable: awk -F, '$32 > 3000{print FILENAME; nextfile}' *


6

With GNU awk 4.1 or above: find . -type f -exec awk ' @load "inplace" BEGINFILE { inplace_begin(FILENAME, "") print "line1\nline2" } {print} ENDFILE { print "line3\nline4" inplace_end(FILENAME, "") }' {} +


5

Use Here String redirection <<< together with Command Substitution $() and don't forget to put double quotes around your variables: a=$(awk "NR==$t" <<< "$c")


5

perl -ane 'print if grep {$_ >= 16} ($F[5] =~ /(\d+)S/g)' file outputs c 256 gene3 55 0 6S27M17S * d 16 gene4 110 9 19S25M6S * e 272 gene5 141 9 23S21M6S * f 272 gene6 139 9 24S20M6S * That finds all the digits followed by "S" in the 6th field. If any are greater than or equal to 16, it prints the line. Look ...


5

Remove while loop and make use of shell brace expansion and also FNR, a built-in awk variable: awk 'FNR==2{print $0 > "output.dat"}' file{1..80}.dat


4

Assuming your input files are tab-delimited: $ join -a 1 -e 0 -t $'\t' -o 1.1,2.2 F1.txt F2.txt A 0.5 B 0 C 0 D 0 E 1 F 0 G 0 H 0.5 I 0 J 1 join the two files, ensure all lines in the 1st file are present (-a), if any fields are null then use the value "0" (-e), take the first field from the first file and the 2nd field from the 2nd ...


4

The first set of quotes is eaten up by the command line for su, and the second set by the command line for ssh, so that the quoted { print $1} is actually seen as three separate arguments by awk. Escape the quotes (and $, and any other special character you may use): su - admin -c "ssh $i awk -F: \'{ print \$1}\' /etc/passwd" Or: su - admin -c "ssh $i ...


4

Note that you don't have to read the file beforehand, sed has the r command that can read a file: $ printf -v var "%s\n" "s1random stuff" "s2 more random stuff" "s1 final random stuff" $ echo "$var" s1random stuff s2 more random stuff s1 final random stuff $ sed '/^s2/r file.txt' <<< "$var" s1random stuff s2 more random stuff line 1 line 2 s1 ...


4

Yes, you can do it in bash but I have no idea why you would want to. Here's a pure bash solution: $ while read -r mon day time host threat date time crit count sugg out exip \ in inip tcp port export cat vuln; do printf "%s | " "$mon $day $time" "$host" "$threat" "$date $time" \ "$crit $count $sugg" "$out $exip" ...


4

awk '{print $NF}' sed 's/[[:blank:]]*$//;s/.*[[:blank:]]//' That would still print an empty line for every blank line. To avoid it: awk 'NF{print $NF}' sed 's/[[:blank:]]*$//;s/.*[[:blank:]]//;/./!d'


4

You can use this command: awk '{print $1}' filename > newfile where filename is the name of the original big file and newfileis the file that will get the results.


3

You get an error because you're attempting to do arithmetic equality with string values. Here are 2 ways to check whether the elements of dfArray are in dsmArray set -A dfArray / /usr /var /tmp ... set -A dsmArray /home /opt /usr ... for a in "${dfArray[@]}"; do in=false for b in "${dsmArray[@]}"; do if [[ $a == $b ]]; then echo "$a is in ...


3

sed would be enough: sed -sn 2p file{1..80}.dat > output.dat -s option is needed to print 2nd line from each file, otherwise only 2nd line of first file will be printed.


3

with awk awk '/P1/{a=1};a;/P2/{exit}' file something P1 something content1 content2 something P2 something


3

In sed: sed -n '/P1/,/P2/p; /P2/q' -n suppresses the default printing, and you print lines between the matching address ranges using the p command. Normally this would match both the sections, so you quit (q) when the first P2 matches. This will fail if a P2 comes before P1. To handle that case, try: sed -n '/P1/,/P2/{p; /P2/q}'


3

sed '/P1/,/P2/!d;/P2/q' ...would do the job portably by deleting all lines which do !not fall within the range, then quitting the first time it encounters the end of the range. It does not fail for P2 preceding P1, and it does not require GNU specific syntax to write simply.


3

The awk variable $NF is the last field of every record; you can use it to print only the last fields of your file like so: awk '{print $NF}' file


2

The following script ought to do an outer join on column (field) 1 of all the tab-delimited files passed as arguments. It uses the join command, which does an outer join on sorted files, 2 files at a time. It will join every line in the files, including the header lines. If you want the headers to be excluded, change the two sort commands to something that ...


2

I believe that with the number of lines you present, you are probably swapping ... The perl script below is using a B-tree in disk (something very common in databases) for the array -- we are using almost no RAM memory. $ cat remdup #!/usr/bin/perl use DB_File; tie %seen, 'DB_File', "mydbfile.db", O_RDWR|O_CREAT|O_TRUNC ,0666, $DB_BTREE; ...


2

You can write it in much more readable form using awk: getval() { awk -F'=' '$1~/\<'"$1"'\>/{print $2}' testfile }


2

If you think that you can cause the event by a specific action or interaction, by far the simplest method is something like: watch -d -n1 "stty -F /dev/pts/106 -a | grep -Eo '.(icanon|ixon)'" Run this on a new terminal, the option to -F is the terminal you will run the program on (run tty to see what it is before starting it). Omit | grep .. if you want ...


2

What about ... head -n 2 input.dat | tail -n 1 | awk ...


2

Decision1: Use GNU sed instead of awk sed -i -e '1 i\line1\nline2' -e '$ a\line3\nline4' ./* Decision2: Use loop for for each file in directory for file in ./* do awk ' BEGIN { print "line1\nline2" } { print $0 } END { print "line3\nline4" } ' "$file" > "$file".tmp && mv -f "$file".tmp "$file" done


2

Try: $ awk 'NF{$NF = sprintf("%.6f", $NF*3)}1' file Data 9390.900391 10573.089844 80.000000 200.000000 8.100000 Data 17762.810547 18536.189453 85.000000 200.000000 8.100000 Change $NF to the $n where nis the field you want to change.


2

$ awk -v OFS='\t' '{for (i=3;i<=NF;i++)print $1,$2,$i}' file SRR959756.1081725 1 RNU2-54P:112:133:hsa-miR-1246 SRR959756.1162547 1 RNU2-54P:112:133:hsa-miR-1246 SRR959756.128602 2 RNU2-37P:85:108:hsa-miR-877-3p SRR959756.128602 2 RNU2-59P:99:122:hsa-miR-877-3p How it works -v OFS='\t' This sets the ...


2

sed -e :n -e '$!N;/\n.*\n/!{$!bn }; s/some text,\n* *something else\n* *another thing/this is completely\ different text/;P;D' <infile I fear you're going to have a difficult time coming up with a solution that suits you until you hammer out a concrete description of the problem - but that's what QA is best suited for, as I see it. Maybe this will give ...


2

You can try : sed 's/.* //' awk '{print $NF}'


2

You're almost there. Just specify the last word: sed 's/^.* \([^ ][^ ]*\)/\1/g' What it does: '^.* ' deletes everything within the start of the line and any spaces. '\(...)\' matches a pattern and returns it as \1. '[^ ]' matches anything without a space in it. (Edited to add better solution. Thanks Hildred!)


1

You need to replace the newlines in the variable with \newline. And the text to be inserted needs to be preceded by \newline. var=$(<file.txt) # Put backslash before newlines in $var var=${var// /\\ } printf "s1random stuff\ns2 more random stuff\ns1 final random stuff\n" | sed "/^s2/a \ $var"


1

In the first place: read "Please choose fruit [1-3]:" t ...will not work. It looks like you're trying to provide a prompt string to the shell builtin read, but read interprets its first argument as the name of a variable to assign the value of the line it reads from stdin unless it is handed options. read -p "Please choose fruit [1-3]:" t ...is an ...



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