Menu
  • HOME
  • TAGS

Get last saturday date of a particular month and year

linux,date,awk,cut

You need to extract the year and month from the filename to be able to ask for the last Saturday. So, just get the day back and compose it back with the year and month you already extracted: #! /bin/bash filename=SOURCE_FILE_042014.CSV date=${filename##*_} date=${date%.CSV} month=${date:0:2} year=${date:2} day=$(cal $month $year | awk...

How to split a branch in two with Git?

git,split,branch,rebase,cut

The first step is git checkout -b feature-test <sha1 split here> But you also need to reset feature to <sha1 split here>: git checkout feature git reset --hard <sha1 split here> Note that if you already pushed feature, you will need to do a git push --force. And that might...

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

bash,command-line,awk,sed,cut

It's very simple with grep -o '...$': cat /etc/passwd | grep -o '...$' ash /sh /sh /sh ync /sh /sh /sh Or better yer: N=3; grep -o ".\{$N\}$" </etc/passwd ash /sh /sh /sh ync /sh /sh That way you can adjust your N for whatever value you like....

integrate call to a jar file | cut | awk and a java program into one unified process

java,bash,awk,cut,synthesize

I looked at the source code for reverb and I think it's very easy to adapt it to produce the output you want. If you look at the reverb class CommandLineReverb.java, it has the following two methods: private void extractFromSentReader(ChunkedSentenceReader reader) throws ExtractorException { long start; ChunkedSentenceIterator sentenceIt = reader.iterator();...

NAs introduced by coercion when labeling breaks in cut function

r,label,cut,na

As the others already stated in the comments, your "NAs introduced by coercion" is not reproducible. But let me just give you a hint on how to make the code more "scalable" and readable: x <- c(1890, 1899,1900,2001,2012,1999,1943,1944,1950,1988,1981,1988,1997,2014) brk <- seq(1890, 2020, by=10) # breaks cut(x, breaks=brk, right=FALSE, labels=paste(brk[-length(brk)], "s",...

Exclude the last-but-one field [closed]

bash,awk,sed,cut,tr

Not sure what your problem is, but this: var=1 echo "list $var M" gives list 1 M To get list 1M, use this: var=1 echo "list ${var}M" list 1M Using {...} makes sure the M is not part of the variable name....

Bash while loop + cut slow

bash,cut

The bottleneck is likely that you spawn several processes for every line of data. As for a replacement, this awk should be equivalent: awk '{ split($0, a, "\""); print $2, $3, a[20] }' TEST.log > IDS.log ...

Extract IPs before string [closed]

linux,bash,awk,cut

Through grep, $ grep -oP '(?:\d{1,3}\.){3}\d{1,3}(?=\(mgn\))' file 222.22.2.221 222.22.2.222 222.22.2.223 Through sed, $ sed 's/.*\b\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\)(mgn).*/\1/g' file 222.22.2.221 222.22.2.222 222.22.2.223 ...

shell script linux,bash : moving files according to name field-1 to the specific path in field-2 using `cut`

linux,bash,shell,cut

Maybe this script is you want to do. #!/bin/bash while read -r line do file=$(echo $line | cut -d' ' -f1) path=$(echo $line | cut -d' ' -f3) ## If file exists, then move to path [[ -f $file ]] && mv $file $path done < deleted_files.txt By the way,...

python pattern cutting of strings in a list

python,string,parsing,pattern-matching,cut

import this at the beginning import re now use this line between dictValue and dictMerged new_dict_value = [re.sub(r'\d.*', '', x) for x in dictValue] and then use new_dict_value in the next line...

Remove e-mail string from the beginning of each row in a file

linux,shell,command-line-interface,cut

You can use sed: sed 's/^[^[:blank:]@]\[email protected][^[:blank:]]\+[[:blank:]]*//' file > file.out some string with no set width another string yet another string!! shortstring gnu sed will work with this: sed 's/^[^\[email protected]]\[email protected]\S\+\s*//' file > file.out ...

Extract minor version from kernel to bash variable

linux,bash,sed,cut

The problem is you are using -c to cut. Don't do that. Use the -f and -d flags instead to control the delimiter and fields to output. Or use awk -F . '{print $2}' <<< "$(uname -r)". Or use IFS=. read -r _ minor _rest <<< "$(uname -r)"; echo "$minor"...

Cut and Awk command in linux

linux,awk,cut

Using awk actually gawk awk '{match($0,/PASSWORD=(.*==)/,a); print a[1];}' input.txt Using cut you can try, I'm not sure if it works with your file cut -d"=" -s -f2,3 --output-delimiter="==" input.txt ...

Using cut on stdout with tabs

bash,cut

What is happening is that you did not quote $line when reading the file. Then, the original tab-delimited format was lost and instead of tabs, spaces show in between words. And since cut's default delimiter is a TAB, it does not find any and it prints the whole line. So...

how to remove first two words of a strings output

bash,printing,awk,echo,cut

Please, do the things properly : for servers in /data/field/*; do string=$(cut -d" " -f3- /data/field/$servers/time) echo "$string" done backticks are deprecated in 2014 in favor of the form $( ) don't parse ls output, use glob instead like I do with data/field/* Check http://mywiki.wooledge.org/BashFAQ for various subjects...

split file into multiple files (by columns)

bash,awk,split,cut

To summarise my comments, I suggest something like this (untested as I have no sample file): NM=$(awk 'NR==1{print NF-2}' file.txt) echo $NM for (( i=1; i <= $NM; i++ )) do echo $i awk '{print $'$i'}' file.txt > tmpgrid_0${i}.dat done ...

Cut specific words matching a pattern from a text file

shell,grep,cut

You can do the following: grep -P -o 'text=\S+ id=\S+' The -P flag for grep enables the perl regular expression. \S+ will match all non blank space characters, -o outputs only the matched portion. Assuming you need to get the fields "text" and "id" values. Modify the regular expression as...

Continuous to discrete cut quartile in R from for loop

r,loops,for-loop,cut,quartile

This question is more about being organized and neat with your data. There are many ways to do this. I would recommend separating out the data you want to bin into its own data.frame. x=dataset[, 50:60] then bin those columns into new columns by making a function with the parameters...

Shell file doesn't extract value properly [grep/cut] from file [bash]

bash,shell,grep,sh,cut

If I understood your question correctly, the following Bash script should do the trick: #!/bin/bash IFS="=" while read k v ; do test -z "$k" && continue # skip empty lines declare $k=$v done <test.txt echo $Name echo $Age echo $Place Why is that working? Most information can be retrieved...

How do I use cut to output initial line in different format?

linux,bash,awk,sed,cut

One way with awk: $ awk '{printf "%s:%s:%s:",$2,$4,$6;for(i=7;i<NF;i++)printf "%s-",$i;print $NF}' file 1.2.3.4:xxx:a:Q-W 5.6.7.8:yyy:b:X-Y 9.10.11.12:zzz:c:L-N-X Explanation: The script will run for every line in the file: printf "%s:%s:%s:",$2,$4,$6; # print the 2nd, 4th, 6th field separated by a : for(i=7;i<NF;i++) # from the 7th field to the penultimate field printf "%s-",$i;...

SED AWK to strip data from log file

bash,awk,sed,cut

This GNU sed could work sed -n -r '/Denied:/{N; s/^.*name="([^"]*)".*$/\1/; p}' file n is skip printing lines r using extended regular expressions, used for grouping here, to not escape () characters N is reading next line and adding it to pattern space s/input/output/ is substitution ^ is start of line,...

R cut() results in odd handling of zero

r,cut

Dealing with floating point numbers is a notoriously messy problem in computer science. Since computer store numbers in base 2 rather than base 10, certain numbers that we commonly use in base 10 simply cannot be expressed succinctly in base 10. I'd recommend doing as much of the work as...

Getting the total size of a directory as a number with du

string,bash,cut

That's probably because it's tab delimited (which is the default delimiter of cut): ~$ du -c foo | grep total | cut -f1 4 ~$ du -c foo | grep total | cut -d' ' -f1 4 to insert a tab, use Ctrl+v, then TAB Alternatively, you could use awk...

Javascript trim multiline string without cutting words

javascript,regex,string,trim,cut

You can try: infotext = infotext.replace(/^([\s\S]{10}\S*)[\s\S]*/, "$1"); Problem is your use of [\s\S]*{10} JSFiddle...

Translate cut code to grep regex code for locating and returning up to the first hyphen

regex,command-line,grep,cut

You can use the extended flavour, use anchor to the beginning of the line and match every character until if finds a dash, like: grep -oE '^[^-]*' infile It yields: NAME ANOTHER NAME THIRD FOURTH FIFTH NAME ...

using the cut command to print the right field

bash,cut

echo "$get_ip" | grep -v "entries" | awk '{print $2}' or with cut: echo "$get_ip" | tr -s " " | grep -v "entries" | cut -d " " -f 2 ...

bug in bash sort with different columns?

bash,sorting,multiple-columns,cut

The --key=1 option tells sort to use all "fields" from the first through the end of the line to sort the input. As @rici observed first, by default this is a locale-sensitive sort, and in many locales whitespace is ignored for collation purposes. That's what seems to be happening here....

linux command : extracting lines from a tab delim. file whose first column containing a certain value

shell,cut

This is pretty much what awk was made for: awk '$1 == "gene_biotype" {print $4, $6}' < input.txt Explanation: $N represents a field, by default separated by whitespace. Any whitespace. The equality check says "Execute the rest of the line only when the first field matches gene_biotype". Then the appropriate...

How to cut 2nd and 3rd column out of a textfile? python

python,bash,cut,tab-delimited

Use neither. Unless it proves to be too slow, use the csv module, which is far more readable. import csv with open('test.txt','r') as infile: column23 = [ cols[1:3] for cols in csv.reader(infile, delimiter="\t") ] ...

Removing extra space at start in bash

php,bash,awk,sed,cut

Use awk instead of cut: php -i | grep extension_dir | awk -F '=> ' '{print $3}' Even grep can be removed using awk: php -i | awk -F '=> ' '/extension_dir/{print $3}' ...

Can't cut column in Linux

cut

This is because there are multiple spaces and cut can just handle them one by one. You can start from the 5th position: $ cut -d' ' -f 1,5- file ATOM HD13 ILE 206 9.900 15.310 13.450 0.0196 1.4870 ATOM C ILE 206 10.870 16.560 17.500 0.8343 1.9080 ATOM OXT...

How to search through a string and extract the required value in unix

string,bash,unix,search,cut

Using awk A=`echo $QUERY_RESULT | awk '{ nreg=split($0,reg);for(i=1;i<=nreg;i++){split(reg[i],fld,"|");printf("%s%s",(i==1?"":","),fld[1]);}}'` echo $A 88371087,88354188,88319898 ...

Finding a string after grep

bash,awk,grep,cut

With GNU awk (for 3rd arg for match()): $ gawk 'match($0,/id="[^" ]+"/,a){ print $3, a[0] }' file 1234j12342134h id="y_123456" 1234j123421342 id="y_123458" 1234j123421346 id="y_123410" WIth other awks: $ awk 'match($0,/id="[^" ]+"/){ print $3, substr($0,RSTART,RLENGTH) }' file 1234j12342134h id="y_123456" 1234j123421342 id="y_123458" 1234j123421346 id="y_123410" or if you want to strip some of the...

Counting POST Attempts in apache log

sorting,search,awk,grep,cut

Try doing this : $ awk ' $6 ~ "POST" && $7 ~ "/wp-login\.php"{ips[$1]++} END{for (ip in ips) {print ip, ips[ip], "POSTs"}} ' /var/log/httpd/domains/domain.com.log ...

How to use sed/grep/cut to extract numericals between two brackets?

regex,bash,sed,grep,cut

You can use this grep: grep -oP '\(\K(\d+\s*)*(?=\))' file 2 2 4 4 4 4 4 4 4 2 2 4 4 4 4 4 4 42 2 2 4 4 4 4 4 4 29 2 2 4 4 4 4 4 4 11 2 2 4 4 4...

How to change horizontal input to vertical output

unix,grep,cut

Replace your line grep -i "$last_name" $1 | cut -f 1-7 -d ':' ;; with awk -F: -vnameMatch="$last_name" \ '$1==nameMatch{ printf("LastName:%s\nFirstName:%s\nCity:%s\nState:%s\nClass:%s\nSemester Enrolled:%s\nYear First Enrolled:%s\n\n", \ $1, $2, $3, $4, $5, $6, $7) }' $1 ;; It's pretty much the same idea in ksh. while IFS=: read c1 c2 c3 c4...

How to make this awk command more simple

linux,awk,cut,gawk

First one is like this: awk '/cpu MHz/ {print $4}' < /proc/cpuinfo | awk -F'.' 'NR==1 {print $1}' Considering you have a string like this: cpu MHz : 800.000 cpu MHz : 800.000 cpu MHz : 800.000 cpu MHz : 800.000 And you want the integer part of the number...

How to cut varchar/text before n'th occurence of delimiter? PostgreSQL

sql,postgresql,split,delimiter,cut

you can try doing something based on this: select varcharColumnName, INSTR(varcharColumnName,'-',1,2), case when INSTR(varcharColumnName,'-',1,2) <> 0 THEN SUBSTR(varcharColumnName, 1, INSTR(varcharColumnName,'-',1,2) - 1) else '...' end from tableName; of course, you have to handle "else" the way you want. It works on postgres and oracle (tested), it should work on other...

how to get a substring with varied length?

bash,shell,substring,cut,substrings

Instead of cut, use dirname and basename: input=/path/to/foo dir=$(dirname "$input") file=$(basename "$input") Now $DIR is /path/to and $FILE is foo. dirname will also give you a valid directory for relative paths to the working directory (I mean that $(dirname file.txt) is .). This means, for example, that you can write...

Batch: Truncate String before saving to variable

string,batch-file,truncate,cut

Here is a script that performs all the computations in one call to PowerShell: @echo off setlocal :: Get free physical memory, measured in KB for /f "skip=1" %%A in ('wmic os get freephysicalmemory') do for %%B in (%%A) do set free_KB=%%B :: Get total physical memory, measured in B...

How to cut a string from a string

regex,string,bash,unix,cut

I would use awk: $ echo "/dir1/dir2/dir3.../importance/lib1/lib2/lib3/file" | awk -F"/importance/" '{print FS$2}' importance/lib1/lib2/lib3/file Which is the same as: $ awk -F"/importance/" '{print FS$2}' <<< "/dir1/dir2/dir3.../importance/lib1/lib2/lib3/file" importance/lib1/lib2/lib3/file That is, we set the field separator to /importance/, so that the first field is what comes before it and the 2nd one is...

print last two words of last line

awk,sed,cut

Just say: awk 'END {print $(NF-1), $NF}' "normal" awks store the last line (but not all of them!), so that it is still accessible by the time you reach the END block. Then, it is a matter of printing the penultimate and the last one. This can be done using...

cut text file and get the first field bash

grep,sh,cut

You could do ls -l . | awk '{print $1}', but you should follow the general advice advice to avoid parsing the output of ls. The usual way to avoid parsing the output of ls is to loop over the files to get the information you need. To get the...

Loop through a comma-separated shell variable

shell,unix,for-loop,cut

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") do # call your procedure/other scripts here below echo "$i" done Instead of...

Print line numbers from grep separated with a comma

bash,shell,grep,cut

Instead of cut -f1 -d: you can use awk: awk -F: '{printf "%s, ", $1} END {print ""}' ...

How to cut a variable to sub variable

shell,awk,cut

You can separate $result into the different variables you describe by using read: IFS=: read TITLE AUTHOR PRICE QUANTITY UNIT <<< "$result" Example: $ result="wrinkle in time:myauthor:50.00:20:50" $ IFS=: read TITLE AUTHOR PRICE QUANTITY UNIT <<< "$result" $ echo "$TITLE - by - $AUTHOR" wrinkle in time - by -...

Combing multiple variables into a new variable in R

r,if-statement,cut

d1 <- cbind(d1, o_all = apply(d1[, -1], 1, function(x) { i <- which.max(!is.na(x) & x > 0) if(x[i] == 0) 0 else i + 4 })) # ID o5 o6 o7 o_all #[1,] 1 1 NA 0 5 #[2,] 2 0 0 0 0 #[3,] 3 2 NA NA 5...

Keep the delimiter in the output of cut

bash,solaris,cut

try this : PLACE=$(grep foo flatfile.txt | cut -d '/' -f 1-6 | xargs -I "%" echo %/) ...

More robust way of extracting date field from string in shell script

regex,bash,date,cut

You could use grep, $ echo 'Password expires 1-4-2015 15:41:05' | grep -o '\b[0-9]\{1,2\}-[0-9]\{1,2\}-[0-9]\{4\}\b' 1-4-2015 $ echo 'Password expires 20-12-2015 15:41:05' | grep -o '\b[0-9]\{1,2\}-[0-9]\{1,2\}-[0-9]\{4\}\b' 20-12-2015 To grep only the year. $ echo 'Password expires 20-12-2015 15:41:05' | grep -oP '^(?:[^-]*-){2}\K\d{4}\b' 2015 To get only the day $ echo 'Password...

How to cut a specific character in a file in unix shell script

shell,unix,ip,character,cut

try: awk '{for(i=1; i<=NF; i++) sub(/^0+/,x,$i)}1' FS=: OFS=: file or perl -pe 's/(^|:)0+/$1/g' file ...

Why cut cannot work?

grep,cut,command-line-tool

I would have used awk here since you can do all with one command. readelf -S hello | awk '/data|bss/ {print $1,$2,$5,$6}' awk will work with any blank space a separator. One space, multiple space, tabs etc....

Awk with combination of cut and if

unix,if-statement,awk,cut

awk 'split($3,a,":"){print a[2]?$4:$4-1}' file or if you still want other field awk 'split($3,a,":"){$4=a[2]?$4:$4-1}1' file or even awk 'split($3,a,":")&&!a[2]{$4--}1' file ANOTHER Only $4 awk '{$0=$4-($3~/:0$/)}1' file Line awk '{$4-=($3~/:0$/)}1' file ...

Read a .txt file using batch and extract a string between ()

file,batch-file,token,cut,between

Like this : @echo off for /f "tokens=3,4 Delims=() " %%a in (test.log) do ( set "$Number=%%a" set "$Unit=%%b" ) echo Number : [%$Number%] echo Unit : [%$Unit%] I Used here a file named test.log for the test...

Bash remove part of a string

bash,awk,sed,grep,cut

The task can be accomplished by basename: $ basename https://twitter.com/username username ...

split a spreadsheet column into 3

csv,spreadsheet,break,cut

Use this script. cat merchent.csv | cut -d "," -f2 | while read; do IFS='. ' read -ra ADDR <<< "$REPLY" for i in "${ADDR[@]}"; do # process "$i" echo -ne "$i " done echo " " done Here I'm guessing your data is like business_name1,contact_name1(in the format Mr. John...

How to extract text from a string in Bash using Grep

regex,bash,grep,cut

grep Host: $FILE | tail -1 | grep -Po '.*Host: \K.*\)' The interesting part is the last grep: -P using perl regex -o output only matched part \K similar as look behind, but supports dynamic length .*\) match the part you need ...

How do I get the 2nd and 4th character from a string

regex,string,bash,substring,cut

with bash variables, you can do this: DOMAINNAME=abcdef CHAR2=${DOMAINNAME:1:1} CHAR4=${DOMAINNAME:3:1} echo "char2=$CHAR2, char4=$CHAR4" gives: char2=b, char4=d explanation the meaning of this: ${DOMAINNAME:3:1}means: take substring starting from the character at pos 3 (0-based, so the 4th character), and length = 1 character....

Analyze a control table by Shell Script

bash,shell,awk,cut

You don't say what your desired output is but this shows you the right approach: $ cat tst.awk NR==1 { print while ( match($0,/[^[:space:]]+[[:space:]]*/) ) { width[++i] = RLENGTH $0 = substr($0,RSTART+RLENGTH) } next } { i = 0 while ( (fld = substr($0,1,width[++i])) != "" ) { gsub(/^ +|...

Extracting Substring from File Name

string,awk,substring,extract,cut

I would use sed: $ echo "asdfasd_d20150616asdasd" | sed -r 's/^.*_d(.{8}).*$/\1/' 20150616 This gets a string and removes everything up to _d. Then, catches the following 8 characters and prints them back. sed -r is used to be able to catch groups with just () instead of \(\). ^.*_d(.{8}).*$ ^...

extract specific tag from html output of a python script

bash,awk,sed,grep,cut

You need to make a proper use of lookaround feature, your lookbehind is fine but lookahead is not. Try this: grep -Po "(?<=<cite>).*?(?=</cite>)" Ex: echo '<cite>www.site.com/sdds/ass</cite>A-"><div Class="sa_mc"><div class="sb_tlst"><h3><a href=' | grep -Po "(?<=<cite>).*?(?=</cite>)" www.site.com/sdds/ass Disclaimer: It's a bad practice to parse XML/HTML with regex. You should probably use a parser...

Bash remove line with zero value

bash,file,cut

Remove all zeros Use grep -v: -v, --invert-match Selected lines are those not matching any of the specified patterns. Command: grep -v -e "^0$" file The problem with this is that it will remove lines all '0' lines. Remove even lines awk 'NR % 2 != 0' file In this...