cut

Is there a cleaner way of getting the last N characters of every line?

夙愿已清 提交于 2019-12-21 04:36:11
问题 To simplify the discussion, let N = 3 . My current approach to extracting the last three characters of every line in a file or stream is to use sed to capture the last three characters in a group and replace the entire line with that group. sed 's/^.*\(.\{3\}\)/\1/' It works but it seems excessively verbose, especially when we compare to a method for getting the first three characters in a line. cut -c -3 Is there a cleaner way to extract the last N characters in every line? 回答1: It's very

Subset a file by row and column numbers

眉间皱痕 提交于 2019-12-20 09:53:20
问题 We want to subset a text file on rows and columns, where rows and columns numbers are read from a file. Excluding header (row 1) and rownames (col 1). inputFile.txt Tab delimited text file header 62 9 3 54 6 1 25 1 2 3 4 5 6 96 1 1 1 1 0 1 72 3 3 3 3 3 3 18 0 1 0 1 1 0 82 1 0 0 0 0 1 77 1 0 1 0 1 1 15 7 7 7 7 7 7 82 0 0 1 1 1 0 37 0 1 0 0 1 0 18 0 1 0 0 1 0 53 0 0 1 0 0 0 57 1 1 1 1 1 1 subsetCols.txt Comma separated with no spaces, one row, numbers ordered. In real data we have 500K columns,

bash script use cut command at variable and store result at another variable

帅比萌擦擦* 提交于 2019-12-20 09:39:17
问题 I have a config.txt file with IP addresses as content like this 10.10.10.1:80 10.10.10.13:8080 10.10.10.11:443 10.10.10.12:80 I want to ping every ip address in that file #!/bin/bash file=config.txt for line in `cat $file` do ##this line is not correct, should strip :port and store to ip var ip=$line|cut -d\: -f1 ping $ip done I'm a beginner, sorry for such a question but I couldn't find it out myself. 回答1: The awk solution is what I would use, but if you want to understand your problems with

Loop through a comma-separated shell variable

て烟熏妆下的殇ゞ 提交于 2019-12-20 08:06:09
问题 Suppose I have a Unix shell variable as below variable=abc,def,ghij I want to extract all the values ( abc , def and ghij ) using a for loop and pass each value into a procedure. The script should allow extracting arbitrary number of comma-separated values from $variable . 回答1: You can use the following script to dynamically traverse through your variable, no matter how many fields it has as long as it is only comma separated. variable=abc,def,ghij for i in $(echo $variable | sed "s/,/ /g")

How to handle more than multiple sets of data in R programming?

允我心安 提交于 2019-12-20 06:19:39
问题 Ca data <- cut(data$Time, breaks=seq(0, max(data$Time)+400, 400))  by(data$Oxytocin, cuts, mean) but this would only work for only one person's data....But I have ten people with their own Time and oxytocin data....How would I get their averages simultaneously? Also instead of having this type output : cuts: (0,400] [1] 0.7 ------------------------------------------------------------ cuts: (400,800] [1] 0.805 Is there a way I can get a list of those cuts? 回答1: Here's a solution using IRanges

pass wildcard to cut command in shell script and store it in a variable

纵然是瞬间 提交于 2019-12-20 05:23:18
问题 I am new to shell, I have a case where I am trying to evaluate a particular column unique values to check if they are valid in a shell script which will be invoked later. From my searches I think cut along with sort & unique is good to do it So my attempt is file=/filepath/*vendor.csv file_categories = `cut -d, -f1 $file |sort |unique` $file should hold file which has vendor in its filename but even after using command substitution (`) the $file is not getting replaced with the correct

Get the 4th Wednesday of each November in R

我只是一个虾纸丫 提交于 2019-12-19 04:04:44
问题 I have a time-indexed matrix (xts object) and I want only the fourth Wednesday of every November. require(quantmod) getSymbols("^GSPC", from="1900-01-01") #returns GSPC GSPC$WED <- weekdays(time(GSPC)) == "Wednesday" GSPC$NOV <- months(time(GSPC)) == "November" G <- GSPC[GSPC$WED==1 & GSPC$NOV==1] That's as far as I got in R. To solve my problem I punted up to bash. write.zoo(G, "wen_in_nov") I did the following hack: cat wen_in_nov | grep -v IND | cut -c 1-10 | sed 's/-/ /g' | awk '{if($3 >=

How to truncate STDIN line length?

风格不统一 提交于 2019-12-19 02:47:28
问题 I've been parsing through some log files and I've found that some of the lines are too long to display on one line so Terminal.app kindly wraps them onto the next line. However, I've been looking for a way to truncate a line after a certain number of characters so that Terminal doesn't wrap, making it much easier to spot patterns. I wrote a small Perl script to do this: #!/usr/bin/perl die("need max length\n") unless $#ARGV == 0; while (<STDIN>) { $_ = substr($_, 0, $ARGV[0]); chomp($_);

Bash: Delete characters until a certain character from String

 ̄綄美尐妖づ 提交于 2019-12-18 15:33:49
问题 how can I delete characters from a String until a certain character with bash? Example: "ananas1kiwi2apple1banana2tree" shall look like this: "apple1banana2tree" Thank you! 回答1: If you want to remove the substring upto 2 , using bash parameter expansion: ${var#*2} # does non-greedy match from left, use ## for greediness #*2 matches and discards upto first 2 from variable var Example: $ var='ananas1kiwi2apple1banana2tree' $ echo "${var#*2}" apple1banana2tree 回答2: Using pure bash shell

Cut and labels/breaks length conflict

故事扮演 提交于 2019-12-18 05:45:20
问题 I am working with the cut function to prep data for a barplot histogram but keep running into a seeming inconsistency between my labels and breaks: Error in cut.default(sample(1:1e+05, 500, T), breaks = sq, labels = sprintf("$%.0f", : labels/breaks length conflict Here is an example. I pretend that it is income data, using a sequence of 0 to $100,000 in bins of $10,000. I use the same variable to generate both breaks and labels, with minor formating on the label side. I thought they might for