Tag Info

Hot answers tagged

25

How about cut : $ cut -d' ' -f1-4,6- file.txt File is not updated and will be removed System will shut down within 10 seconds Please save your work or copy to other location Kindly cooperate with us -d' ' sets the delimiter as space -f1-4,6- selects the first to 4th field (word), leaving the 5th one and then continue printing from 6th to the rest. ...


11

A solution with cut: cut -d ' ' -f1-4 -f6- FILE


8

With sed: sed '$!N;/remove/!P;D' infile This pulls the Next line into pattern space (if not ! on la$t line) and checks if pattern space matches remove. If it doesn't (means none of the two lines in the pattern space contains the string remove) it Prints up to the first \newline character (i.e. it prints the first line). Then it Deletes up to the first ...


7

If your input all looks like that, then the most efficient solution is most likely: cut -d\# -f4- <<\IN null###F01|54646466|00K4234001|IFD|1990101 null###F02|54646499|00K4234001|TFS|1990101 null###F03|03232432|00RWEREW01|ZAI|1990101 IN I use a heredocument above to demonstrate, but you can just use cut -d\# -f4- <infile >outfile. You could ...


6

awk: remove the 5th field awk '{for (i=5; i<NF; i++) $i = $(i+1); NF--};1' file If you want to save the file in-place: http://stackoverflow.com/q/16529716/7552 You could just erase the contents of the 5th field, but that leaves 2 consecutive output field separators: awk '{$5 = ""};1' file


6

sort -n -k2 -k1.3 file | awk '{$2!=a?x=1:x++} {print > "file"x; a=$2}' First , we need to sort the file correctly. -n sorts the file numerically, -k2 sorts according to the second field (the marks 2-6), -k1.3 then sorts within this order the first field starting from the 3rd character numerically (irgnoring the leading Q.). Now awk splits the output ...


5

tr \; \\n <in >out ...is very likely the most efficient means of going from your sample input to your sample output.


5

You could pass the whole pattern to awk letter=a awk -v pattern="Task .* $letter" -v RS='-+' ' $0 ~ pattern ' text.txt or construct the pattern as a string in awk letter=a awk -v ltr="$letter" -v RS='-+' ' BEGIN {pattern = "Task .* " ltr} $0 ~ pattern ' text.txt Since awk variables are not prefixed with $, you can't embed them inside a ...


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


4

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


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


4

tolower(title) is handled as regular expression: C++ matches the character C literally (case sensitive) Quantifier: ++ Between one and unlimited times, as many times as possible, without giving back [possessive] C matches the character C literally (case sensitive) C## matches the characters C## literally (case sensitive) To get the right result for ...


4

With POSIX sed: sed -e 's/[^[:alnum:]_][[:alnum:]_][[:alnum:]_]*//4' <file


4

awk: awk '{print (NF>1) ? $1 : ""}' file If the number of fields is more than 1, print the first field, otherwise print an empty line. A couple of extra thoughts: If your data is tab-separated, then awk -F '\t' '{print $1}' file If you want to extract the first 8 characters awk '{print substr($0,1,8)}' file


4

awk ' !/remove/ && NR > 1 && prev !~ /remove/ {print prev} {prev = $0} END {if (!/remove/) print} ' Input.txt


4

If your files are not too large to fit in memory, you could use perl to slurp the file: perl -0777pe 's/.*?PAT[^\n]*\n?//s' file Just change PAT to whatever pattern you're after. For example, given these two input files and the pattern 5: $ cat file 1 2 3 4 5 11 12 13 14 15 $ cat file1 foo bar $ perl -0777pe 's/.*?5[^\n]*\n?//s' file 11 12 13 14 15 $ ...


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


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


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


3

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


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


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


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


3

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


3

With join and a shell smart enough to know how to deal with <(...): join <(sort file1) <(sort file2) | join - <(sort file3) | join - <(sort file4) Output: Bm1_00085|Bm1_22625 0.263974289 0 4 1


3

man page for gawk reads RS The input record separator, by default a newline. So changes the record separator from being just a newline to being a newline immediately followed by "20". Example below. $ cat log.1 test foo Job-157625 20 Job-157625 $ gawk -vRS="\n20" '/Job-157625/' log.1 test foo Job-157625 Job-157625 $ gawk '/Job-157625/' ...


3

You can do it with: awk '{print NF}' filename


3

IIUC, you want to replace every instance of ; with a newline. You can use sed: sed -i 's,;,\n,g' <FILE> If your sed does not support -i: sed 's,;,\n,g' <FILE> > <OUTPUT_FILE>


3

Your best choice maybe passing variable through environment: letter=a p="Task: *$letter" awk -v RS='-+' '$0 ~ ENVIRON["p"]' <file or: p="Task: *a" awk -v RS='-+' '$0 ~ ENVIRON["p"]' <file Using -v var=value, awk will expand escape sequences in value. If you want to pass data as-is to awk from shell, -v var="$shell_var" is not reliable. Using ...


3

I would write awk -F, -v OFS=, '{ n=0 sum=0 for (i=3; i<=NF; i++) { split( substr( $i, 2, length($i)-2 ), a, /:/) sum += a[1]*a[2] n += a[2] } print $1, $2, sum/n }' file which produces 6708.1717,07/15/2015,6708.3 3352.130000000,07/15/2015,3352 6708.1717,07/15/2015,6708.31



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