Tag Info

New answers tagged

2

Another sed: $ sed -e '/X/{ $!N /\n.*Y/!D }' file 31 X 32 Y 33 X 34 Y 36 X 37 Y


3

Using sed: sed -n ':b /X/ { h; n; /Y/! b b; H; x; p; }' Output: 31 X 32 Y 33 X 34 Y 36 X 37 Y


1

There are various tools that can do this. If you have pcregrep (it should be available in your distribution's repository), you can do: $ pcregrep -M 'X\n[^\n]+Y' file 31 X 32 Y 33 X 34 Y 36 X 37 Y The -M switch allows pattern to match across newline characters and the regular expression matches an X, followed by a newline, then any non-newline characters ...


1

If I understand you correctly, you only want lines containing X if they are then followed by a line containing Y, and then you want both lines. grep -A1 X filename |grep --no-group-separator -B1 Y


3

You can compare file timestamps directly in bash/zsh/ksh with the test operator -nt (newer-than). You can also use touch to set a time on a file, eg 0:00 for midnight today. So simply: touch -d '0:00' /tmp/midnight if [ file -nt /tmp/midnight ] ...


0

if [[ $(date -d$(date +%D) +%s) -gt $(date +%s -r file.txt) ]]; then echo "Not Today" else echo "Today" fi This will compare time elapsed since epoch at 00:00 today and modification date since epoch of the file.txt.


2

If you have GNU find installed, the following line will return the filename if it was last modified earlier than today find yourfile -prune -daystart -mtime +0 -print


1

If on a GNU system, you can use this : #!/bin/bash last_mod="$(date -r "$1" '+%Y%m%d')" current="$(date '+%Y%m%d')" if [ "$last_mod" -eq "$current" ]; then ## Do something elif [ "$last_mod" -gt "$current" ]; then ## Do something else ## Do something fi Insert the file name as first argument to the script. last_mod will contain the last ...


2

sed -e's/ *[^ 0-9]*/&\n/6;:n' \ -e's/\(\n[^ ]*\)[^ ]/\1 /;tn' \ -e's/\n//' <infile Here's some sed to do it. The first thing we do is replace the 6th occurrence of one or more spaces followed by a sequence of zero or more [^ 0-9] not-space or numeric characters with itself followed by a newline. Basically this means that if the 7th ...


3

As you want to preserve your columns you can change output field separator to be tab not space for example and it will be easier for further processing if you rely on columns count. So you can use following awk: awk 'BEGIN { OFS = "\t"; }; { if ($7 ~ "^[0-9]*$") $7 = " "; else $7 = $7; }; 1' In BEGIN section we are changing output field separator(OFS) to ...


2

awk '{gsub("^[0-9]*$"," ",$7);$7=$7;OFS="\t";print}' file.txt


0

Perl to the rescue: #!/usr/bin/perl use warnings; use strict; while (<>) { # Process line by line. my @F = split /(\s+)/; # Split the line into @F, keep whitespace as members, too. $F[12] =~ s/./ /g # Replace any character with space if $F[12] =~ /^[0-9]+$/; # if there are just digits. print ...


2

This example assumes, all the csv contents are lying in a file named a.csv...you can change it to use stdout stream instead of file stream and out of laziness, i put longitude, latitude as sub-elements.. you can put them as attributes as well from xml.etree.ElementTree import Element, SubElement, Comment, tostring top = Element('markers') f = ...


0

sed is the more simple and efficient tool for this editor grade modification: sed 's-,-ctrlVtab-g' customers You can also very easily achieve the same modification with ex, vi or emacs.


3

As some different variants awk awk '{$3=A[split($3,A,"-")]}1' file sed sed -r 's/((\S+\s+){2})[^- ]+-/\1/' file


0

Using awk: < input awk 'BEGIN {FS=OFS=" "} {gsub(/[^-]*-/, "", $3); print}'


4

Perl to the rescue: perl -lane 'BEGIN { $, = "\t" } $F[2] =~ s/.*-//; print @F' < file -l appends newlines to print -n reads the input line by line -a splits each line on whitespace and populates the @F array $, separates list members when printed, set it to tab s/.*-// substitutes everything up to a dash with nothing, it's bound to the third column ...


0

Since the file is sorted and as per your comment lines starting with chr10 are always at the start of the file, using Perl: < cov.txt perl -pe '!/chr10/&&exit' > subset.txt This way the script will exit at the first occurence of a non-match. A test of this run on a 1000000-matching-lines file stored in memory (obtained by appending the ...


0

I forgot to update this thread. Found the solution same day by using following mysql -u$SQLUSER -p$SQLPASS -e "use $DB; select creationdate,login,package,expirydate,mobile from users WHERE creationdate >= NOW() - INTERVAL 5 MINUTE;"


2

More efficiently, do it with egrep: egrep '^chr10{space or tab}' cov.txt Or if the contents are all similar to what you have shown, grep -w chr10 cov.txt


1

Using sed: sed -n '/^chr10[^0-9]/ { p; b; }; q' cov.txt > subset.txt This still assumes the chr10 group is at the very beginning of the file.


2

Try this, seemed a little quicker in my rudimentary test. Avoids doing regular expression processing. [root@localhost tmp]# wc -l cov.txt 34970568 cov.txt [root@localhost tmp]# time awk '$1 ~ /^chr10$/{print}; $1 !~ /^chr10$/{exit}' cov.txt > subset.txt real 0m23.897s user 0m22.031s sys 0m1.556s [root@localhost tmp]# time awk '{ if($1 == ...


3

awk '$1=="chr10"{print; next}{exit}' cov.txt > subset.txt Tests: Redirected to /dev/null for 12,947,909 chr10 records plus a few more chr11, chr12 and more to a total of 99,063,774 lines - outputs are all identical (same md5sum). The output line count = 12,947,909 -- ordered fastest to slowest: steve: awk '{ if($1 == "chr10") { print } else { exit ...


0

This command stops the processes: sudo ps o pgid,args | grep mininet: | sudo awk '{system("sudo kill --signal SIGSTOP -"$1)}' - In awk you can use system("program ") taking the advice to use pgrep this works too: sudo pgrep -f mininet: | sudo awk '{system("sudo kill --signal SIGCONT -"$1)}' -


3

You can use redirection (it is specified by POSIX, so should be available with other awks). A simple example: $ awk '$1 % 2 {print > "odd"; next} {print > "even"}' <(seq 1 10) $ tail -n +1 odd even ==> odd <== 1 3 5 7 9 ==> even <== 2 4 6 8 10 So, you could create an array with 29 ...


0

A couple of things to consider re the following gawk script: Seeing that your input date does not show the century, you need to consider that format in realation to what date defaults to. The provided sample output shows a decimal time, but your description says seconds. The data is in seconds resolution, so this script outputs integer seconds ...


4

The following command yields the requested output: cut -d ' ' -f 1,3-10 file1


0

Your code isn't checking the result of match. The following fixes it. RSTART is set by invoking the match function. Its value is the position of the string where the matched substring starts, or zero if no match was found. if(RSTART){ user = substr($0, RSTART + 12, RLENGTH - 13) c[d[1] OFS user]++ }


0

Do you mean: BEGIN { flag=0; FS="[^a-zA-Z]+" } { for (i=1; i<=NF; i++) { word = tolower($i) if ($0 ~ /hello/ ) { if (flag) { sub ("hello", "hi", word ); } flag= !flag } words[word]++ } } END { for (w in words) printf("%3d %s\n", ...


-3

You can do it with: sed -e's/ testhost / testhost testhost1 testhost2 testhost3 /; s/ testhost$/ testhost testhost1 testhost2 testhost3/' /etc/hosts


3

The way awk works is that by default it steps through each line of text and splits every space-separated item into fields, in this case we have fields $2, $3,and $4 of interest. Now what if we could store each $2 field of each line into a list and then print it ? That's where arrays can help. Treating the body of an awk code as one big while loop, we can ...


2

You could use the -exec sh -c '...' construct with find: find . -type f -exec sh -c 'file --brief --mime-type "$0" | \ grep -q ^image/ && identify -format "%[fx:w*h] %i\n" "$0"' {} \; or with exiftool: exiftool -q -if '$mimetype =~ /image/' -p '$megapixels $directory/$filename' -r .


2

As steeldriver pointed out, your problem is not awk, it's file. There is no NUL in the input you are giving to awk because file ate it. I would do this whole thing in the shell instead: find . -type f -print0 | while IFS= read -r -d '' file; do file --mime-type "$file" | grep -qP "\bimage/" && printf '%s %s\0' $(identify -format ...


4

I think your best bet will be to use a shell loop instead of xargs: Then you can control how commands are sent the filename argument. find . -type f -print0 | while IFS= read -rd "" filename; do type=$( file --brief "$filename" ) if [[ $type == *image* ]]; then identify -format "%[fx:w*h] %i\n" "$filename" fi done


1

Another POSIX answer: paste -d'>\n' /dev/null - - <infile It gets: >Sunshine This is a sunny day. >Darkness A cave is a dark place.


2

POSIXly: sed 's/^/>/;n' < file.in > file.out


2

By default, awk considers any sequence of whitespace to be a column separator. It can't guess that you want that 7th column to span until the end of the line including embedded whitespace, you need to tell it. Since you have variable amounts of whitespace between columns, keep the whitespace-based definition of columns. But instead of using $7 for the last ...


2

This is a bit too complex to be legible as a one liner so here's a commented gawk script: #!/usr/bin/gawk -f ## Save the data in array data: data[M][INS]=dinucleotide NR==FNR{ data[$2][$1]=$3; next } ## Save the groups in array groups: groups[GRN][INS] { groups[$1][$2]++ } ## Now that everything is stored in memory, analyze END{ ## Get ...


3

If your data is at fixed columns, you can ask awk to split at those column widths by setting FIELDWIDTHS. eg: awk ' BEGIN{ FIELDWIDTHS="4 10 8 10 8 6 99" } { printf "1=%s 2=%s 7=%s\n", $1, $2, $7 } ' gives 1=0 2=PATRAC_1 7=Not configure


10

Your problem is that by filtering on raw bytes in a UTF-8 character stream, you're eating part of a unicode sequence in a UTF-8 file, resulting in an invalid byte sequence. That can't work. Instead, you need to use a tool that understands UTF-8, and apply a filter on the unicode data, rather than the raw bytes. Since I don't know which implementation of awk ...


1

Using awk you can do it as: awk '{for(i=1; i<=length;i++) if(substr($0,i,1)>="\xS_INDEX" && substr($0,i,1)<="\xE_INDEX"){printf substr($0,i,1);f=1;} if(f)printf "\n"; f=0}' filename Here S_INDEX and E_INDEX are the starting and ending index of ascii in hex. For input: 1243 135 dgfsdaa 125 sdg124 sdf34 Selecting only digits: S_INDEX = ...


2

Except the IP2Dec and dec2ip script, which need to be performed only on part of the file, it should work. Here an example of dec2ip which applies the function only on the first entry of the line. #!/bin/bash dec2ip () { local ip dec=$@ delim="" for e in {3..0} do ((octet = dec / (256 ** e) )) ...


1

User FloHimself seemed curious about a TXR solution. Here is one using the embedded TXR Lisp: (defvar splits 4) (defvar name "data") (let* ((fi (open-file name "r")) ;; input stream (rc (tuples 4 (get-lines fi))) ;; lazy list of 4-tuples (sz (/ (prop (stat name) :size) splits)) ;; split size (i 1) ...


2

If your goal is to extract those six numbers into shell variables, it is probably more convenient to put them in a bash array like this: $ data=($(awk '/^ *(par|concurrent)/{printf "%s %s ",$5+0,$7+0}' file)) You can verify that the array has the correct values using declare: $ declare -p data declare -a data='([0]="131008" [1]="0" [2]="131072" ...


1

Just for fun, perl: perl -MPath::Class -lne ' BEGIN {@codes = map {[split]} file("file2")->slurp; $, = " "} $val = $_; ($code) = grep {$_->[0] <= $val && $val <= $_->[1]} @codes; print $val, $code->[2]; ' file1


1

How about a sed script with gotos (shock, horror). This could be simplified if you describe your data more precisely. sed -n ' :start /Server .* Health Check/{ n /Date - Count/n :loop1 / : /{p; n; b loop1 } b start } /Errors caused by/{ n :loop2 /Server/n /^[0-9]/{p; n; b loop2 } b ...


0

This is fairly simple with AWK: File1 into File2 before pattern = "Pointer" First load the contents of File1 into a variable f1="$(<File1)" then do the insertion awk -vf1="$f1" '/Pointer/{print f1;print;next}1' file2 (Or, if you want to insert File1 after "Pointer") awk -vf1="$f1" '/Pointer/{print;print f1;next}1' file2


1

Well, if you have GNU tools you can do: for match in \ Server\ Health\ Check\ 1 Server\ Health\ Check\ 2 Errors\ caused\ by\ X do grep -Fxm1 "$match" case $match in (S*) sed -nEu '/^[0-9/:]+/!q;p';; (*) sed -u '4q;3d;1d';; esac;done <file The assumption here is that there is some intervening data between the sections you're ...


3

You can try following awk: awk 'NR == FNR { x[$1] = $1+0; next; } { for (i in x) { if (x[i] > $1+0 && x[i] < $2+0) { print x[i], $3; } } }' file1 file2 Result should be: 4 Alpha 2 Alpha 7 Bravo


3

Problem So many things wrong here #!/bin/bash myarr = ( has got a space between it meaning nothing is assigned if it even runs at all. cat 25.6.2015_test.txt | awk Awk can open its own files no need for cat -F 'fafafafa' '$1~/^[a-z0-9*]+$/ -F is the field separator not record, so all this is doing is removing the text fafafafa, it's still reading ...



Top 50 recent answers are included