Tag Info

Hot answers tagged

6

Ed is the standard editor, because you can use it to develop ed scripts and let it do its work, as you tested before, interactively. For small files like .bashrc or any code text, ed is performant because it reads the whole file in one rush and works with the buffer. For the same reason you should not use ed for big files like log files. But with an ed ...


5

sed -e 's/\(..\)\/\(..\)\/\(....\),\(.....\),\(.*\)/\3-\1-\2 \4:00,\5/' Edited to include the input from the comments below: sed -e 's#\(..\).\(..\).\(....\),\(.....\),#\3-\1-\2 \4:00,#'


5

Using GNU grep built with recent PCRE support: echo 'st Hello world! et' | grep -oP "st\K.*?(?=et)" The key here is to use the look-ahead and look-behind zero-length assertions. \K ==> Look-behind assertion (?=et) ==> Look-ahead assertion For more info, you could read here.


4

You may want to use sed: sed -n 's/.*st\(.*\)et.*/\1/p'


4

If you use ksh (or bash with extended globbing activated, or zsh with ksh globs enabled) you can achieve the desired function using only file globbing patterns: ls -d -- !(*bak) With grep, to get a simple solution, just use the negation -v: ls | grep -v 'bak$'


3

Regular languages (i.e. "this can be matched with a RE") are closed under complement, so it's possible, but it's not very useful for practical purposes: what you start out with is the condition last letter is k AND letter before that is a AND letter before that is b (let me write s[-1]=='k' and s[-2]=='a' and s[-3]=='b' in a pythonesque fashion) so a ...


3

Using find: find . -maxdepth 1 -type f ! -name "*bak" .: asserts to search in the current working directory -maxdepth 1: asserts to search only one level below the specified directory (i.e. only the current working directory) -type f: asserts to search only for files ! -name "*bak": asserts to search only for filenames not ending in bak However, if you ...


3

The most reliable way to test whether a crontab line is valid is to ask the crontab utility. Most crontab utilities don't have an option to only validate and not change the crontab. You can call crontab -e to set the crontab, and if this succeeds restore the previous content, but this is not robust: if the input is valid, a job that it contains could be ...


3

This matches ( and ) literally. () is used as capturing groups in Extended Regular Expressions (as used by r's grep() or the grep command with the -E option) or Perl Compatible Regular Expression which extend Extended Regular Expressions (as used by r's grep(..., perl=TRUE) or some grep commands with the -P option). Hence to match () literally, \ is used.


3

sed 'y|/|-| s/,*\(.....\)-*\([^,]*\)/\2-\1/ s// \1:00/2 ' <infile OUTPUT: 1998-01-02 09:30:00,0.4571,0.4613,0.4529,0.4592,6042175 1998-01-02 09:45:00,0.4592,0.4613,0.4529,0.4571,9956023 1998-01-02 10:00:00,0.4571,0.4613,0.455,0.4613,8939555 1998-01-02 10:15:00,0.4613,0.4697,0.4571,0.4697,12823627 1998-01-02 ...


3

That worked for me: sed -r 's/([0-9]{2})\/([0-9]{2})\/([0-9]{4}),([0-9:]{5})/\3-\1-\2 \4:00/g' Match 2 digits (([0-9]{2})), slash, 2 digits (([0-9]{2})), slash, 4 digits (([0-9]{4})), and then digits and : (([0-9:]{5})). Replace it with the order you wish: \3-\1-\2 \4:00 (year-month-day hour:minute:00).


3

Without markers the regexp (right part) can match any part of string. So your variant match 1234. To satisfy requirements you have to use markers: [[ "1234+5678" =~ ^[0-9]+(\s*(\-|\*)\s*[0-9]+)*$ ]] ; echo $? And shorter (if you'd like): [[ "1234+5678" =~ ^[0-9\ *-]+*$ ]] ; echo $?


2

Regular expressions can be anchored at the end of the line using $ (or at the beginning, using ^). If you want to replace anything matching the pattern [A-Za-z]* at the end of the line with something, then anchoring the pattern like this: [A-Za-z]*$ ...will force it to match at the end of the line and nowhere else. However, since [A-Za-z]*$ also matches ...


2

I would suggest taking a slightly different approach - parse the timestamp, then spit out a formatted timestamp. And I'd use perl for this: #!/usr/bin/perl use strict; use warnings; use Time::Piece; my $input_format = '%m/%d/%Y,%H:%M'; my $output_format = '%Y-%m-%d %H:%M:%S'; while (<>) { my ( $date, $time, @stuff ) = split(","); my ...


2

It works also for Cyrillic \v\k A bit more complicated and fails with Cyrillic \v(\c[0-9a-z_[=a=][=c=][=e=][=i=][=l=][=n=][=o=][=r=][=s=][=t=][=u=][=y=][=z=]]) Doc. Tested on Vim 7.4.


2

And possible awk solution: awk 'BEGIN { FS = OFS = ","; } { split($1, d, "/"); $2 = d[3] "-" d[1] "-" d[2] " " $2 ":00"; $1 = ""; } { for (i = 2; i < NF; i++) printf("%s", $i OFS); printf("%s", $NF ORS);}' file


2

Janis is right that you're wanting to match regex meta characters, so will need to escape them, What's been missed though is that you're in the R environment. In that case, you need fixed: grep(pattern = "c++", x = df[trow, "modeling"], fixed=TRUE) You do realize that there's help available. Try these: ?grep ?regexp


2

The default regex type for GNU find is emacs, which doesn't support intervals. You can specify different regex types, such as posix-egrep, which will solve your issue: find . -maxdepth 1 -type d -regextype posix-egrep -regex './[0-9]{6}'


2

I like to directly use proc for that grep -l '1[0-9][0-9]\|200' /proc/[1-9]*/comm|awk -F '/' '{print $3}' For the pid variant grep -l 'xclock' /proc/{1[0-9][0-9],200}/comm 2>/dev/null|awk -F '/' '{print $3}'


1

Using a grep version which supports PCREs (like e.g. GNU grep), to list all the matches: hostname="database.example.com.au" < inputfile grep -Po "$hostname\)\(Port = \K[0-9]+" To list only the first match: hostname="database.example.com.au" < inputfile grep -Po -m1 "$hostname\)\(Port = \K[0-9]+" Using sed, to list all the matches: < inputfile ...


1

I've never seen a formal description of tnsnames.ora, so I'm kind of guessing at the format. Here's my stab at a script to do what you want: #!/bin/bash HOST=$1 tr '()' '\n\n' < input | awk 'BEGIN {host=""; port=""} /[Hh][Oo][Ss][Tt]/ {host=$3} /[Pp][Oo][Rr][Tt]/ {port=$3} host && port { if (host == "'$HOST'") {print host, ...


1

If foo must match at the start of the basename: < inputfile grep '/foo[^/]*$' /foo[^/]*$: matches a /foo string followed by any number of any character not / at the end of the string if foo can match anywhere in the basename: < inputfile grep 'foo[^/]*$' foo[^/]*$: matches a foo string followed by any number of any character not / at the end ...


1

I would first extract candidates, then check for the maximum 777 later: egrep '^[0-7]?[0-9][0-9]?)' file | sed 's/^\([0-7]\?[0-9][0-9]\?\))/\1 )/' | awk '($1 < 778) {print $0}' | sed 's/^\([0-7]\?[0-9][0-9]\?\) ).*/\1,/' | tr --delete \\n | sed 's/,$/\n/' It works for my test files. (EDIT1: The last sed now gives the number only EDIT 2: removed ...


1

As some clarification iterations showed, only integer numbers and with values not larger than 777 shall be collected into a comma separated list. Here we go: awk -F ')' '$1~/^[0-9]+$/ && $1<=777 {print $1}' <datafile | paste -sd, Note: To match an integer range you can in awk also write: awk -F ')' '$1>=1 && $1<=777 {print ...


1

With POSIX BREs that have no alternation operator, you can use that \{0,1\} instead: LC_ALL=C grep '^\.\{0,2\}\(.*[^k]\)\{0,1\}\(.*[^a].\)\{0,1\}\(.*[^b]..\)\{0,1\}$'


1

If you want to grep for fixed strings use fgrep or grep -F instead. If you want to use grep the meta-characters like + needs to be escaped; there are various possibilities for that, e.g. c[+][+].


1

Try this: BATCH=AB1234 awk -v batch="#Batch Job.*${BATCH}" '($0 ~ batch),/#--.*/' filename


1

What you need is called the back-references. It means "put here whatever was selected in the match" and functions by: Using parenthesis in the matching regex to select a part of the string, Using a numbered back-reference (\1 for the first parenthesis, \2 for the second one, etc.). sed support this and this produces the following command (sed requires ...


1

sed -e 1,/^$/c\\ -e '' <infile >outfile ...would also work. As opposed to 1,/^$/d the c\<EOF> command doesn't delete the address range, exactly, but rather changes the whole block to a single string. And so this will not remove the first blank line from output - because it changes the whole first occurring text block in input (as delimited by ...


1

With a recent ksh (you need ksh93s or above), you could do: cat <example.txt <#""



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