Tag Info

Hot answers tagged

8

Use the modifier form of if: perl -ne 's/stuff/changed/, print if /patternmatch/' or, you can use and and or for flow control: perl -ne '/patternmatch/ and s/stuff/changed/, print'


7

sed 's/=\(+[0-9]\{1,3\}\)/(\1)=\1/' To address your problem (as I understood): Patterns that need to be memorized in sed are to be enclosed in parentheses - their appearance defines their index number. E.g.: sed 's/\(<memorized_pattern_1>\)<not_memorized>\(<memorized_pattern_2>\)/\2\1/' would swap patterns 1 and 2 and delete the ...


6

Given the format of your example this should work for any number of comma separated strings after the initial large space(if it's a tab just change the spaces in the second s/// to \t sed ':;h;s/,.*//;p;x;s/ [^,]*,/ /;t;d' file 3923 001 L05 LV 3923 001 L05 RM 3923 002 L12 RA 3923 002 L12 LA 3923 003 I06 ALL 3923 004 G04 RV 3923 ...


6

Using Perl compatible regular expression in grep: grep -Po '..(?==Y)' <file Result: p2 p6


5

This works. ^ denotes start of line, plus the {32} you already had, then a $ for end of line. $ cat fileA fileB 12345678901234567890123456789012 123456789012345678901234567890123 12345678901234567890123456789012 123456789012345678901234567890124 $ grep -E "^.{32}$" fileA fileB fileA:12345678901234567890123456789012 fileB:12345678901234567890123456789012 $ ...


4

You can do it like this: sed -e's/ \([^ ][^ ]\)/\n\1/g' \ -e's/\([^ ][^ ]\) /\1\n/g' \ -e's/ //g;y/\n/ / ' <<\IN I have a source text file containing text where some words are l e t t e r s p a c e d like the word "letterspaced" in this question (i.e., there is a space character between the letters of the word. IN The idea ...


3

Use that: sed 's/=\(+[0-9]\+\)/(\1)=\1/' file It searches for =+ followed by at least one digit ([0-9]\+) and replaces all with the desired format ((\1)=\1).


3

Try this: echo 'p1=X||p2=Y||p3=X||p4=X||p5=X||p6=Y||p7=X' | grep -o '[^|]*=Y' | cut -d= -f1 | sed -e 'N;s/\n/,/g' Output: p2,p6


3

A Perl approach that mostly works: perl -C -lpe 's/(?:^|\P{L})\K\p{L}(?:\s\p{L})+(?=\P{L}|$)/$&=~s{\s}{}rgo/goe' This assumes a version of Perl recent enough to know about the /r flag in replacements. Proof of concept: $ echo 'Do I like «ł é t t ê r s p ä c è đ» text?' | perl -C -lpe ...


3

The code below will allow you to perform some action on text files starting with "NA", only having alphanumeric characters, and ending in ".txt". for f in /path/to/your/folder/*; do if [[ ${f##*/} =~ NA[a-zA-Z0-9]*.txt ]]; then # perform some action on $f fi done If you want to allow your file to start with "N" or "A" use this: for f in ...


3

Just for fun: python -c 'import sys,fileinput,re;sys.stdout.writelines(re.sub("stuff", "changed", l, 1) for l in fileinput.input() if re.search("patternmatch", l))' file Don't do it:) Use sed/perl/awk


3

One option is to search with prefixed spaces, such as: / alias, where there are two spaces before the word "alias" to prevent false positive matches where "alias" appears as part of another sentence. You can also ground the match to the beginning of the line if you know exactly how many spaces there are: /^ alias for example. However your expressions ...


3

It is possible using sed. Pipe the input to the below. | sed 's/\(^.\{12\}\)\([^,]\+\),\([^,]\+\)/\1\2\n\1\3/' output 3923 001 L05 LV 3923 001 L05 RM 3923 002 L12 RA 3923 002 L12 LA 3923 003 I06 ALL 3923 004 G04 RV 3923 004 Z09 ALL Here is the source to handle input with multiple "," For original input without tab, use | perl ...


3

You can't use capture groups from the regexp in the command to execute. If you use find -regex to restrict matches, you'll have to do some extra matching in the command. You can do that by invoking a shell and using its own pattern matching constructs. For example, if foo and bar are constant strings and regex1 can't match bar: find … -exec sh -c ' ...


2

Let's do this using a simple example, consider for a file, we will replace each digit of a line with the string HELLO, if the line does not have any digit then leave it as it is : #!/usr/bin/env python2 import re with open('file.txt') as f: for line in f: if re.search(r'\d', line): print re.sub(r'\d', 'HELLO', line).rstrip('\n') ...


2

Possible solution with awk: awk -F" " '{ x = $3 " " $4 " " $5; split(x, a, ","); for (i in a) { print $1, $2 "\t" a[i]; } }' file Output should be: 3923 001 L05 LV 3923 001 L05 RM 3923 002 L12 RA 3923 002 L12 LA 3923 003 I06 ALL 3923 004 G04 RV 3923 004 Z09 ALL And if your input contains multiple , as in comments: 3923 001 L05 ...


2

sed 's/=\([^= ]*\) *$/(\1)&/' <in >out The above will just replace the last equals sign on a line and all characters which follow first with... A copy of those that follow and which are not space surrounded by two parens (in case there are any trailing spaces on a line) The whole matched pattern all over again. On the right-hand-side (the ...


2

Suppose all the data is there in a file named as file, then awk -F "=" '{print $1"("$2")="$2}' file


2

sed -e "s/^-\([0-9][0-9][0-9][0-9]\)/\1-/" file.csv The part between \( and \) is referenced in the replacement part by \1, allowing to replace by "whatever matched the search". Note that if you are using a csv file, this will only work if the column is really the first one (^ matches the beginning of the line). If the column is somewhere else, you might ...


2

awk can read records based on a regex delimiter of your choice. eg '[|\n]' It can also split records into fields on the delimeter of your choice. eg. '=' The ternary operator (condition)?: prevents a leading comma. awk -F= -vRS='[|\n]' '$2=="Y"{ printf (i?",":"")"%s", $1; i=1 }' output: p2,p6 If a trailing newline is needed, it can be appended in ...


2

How about: tr '|' '\n' | sed -n 's/=Y$//p'


2

^=* means search for a line starting with zero or more equal signs. If you want lines starting with = just use ^=


1

perl -nE 'say join(",",/(\w+)=Y/g)'


1

a simple awk awk -F\| '{for (i=1 ; i<= NF; i++) if ( $i ~/Y/ ) { split($i,A,"=") ; printf "in %d : %s\n",i,A[1] ;}}' where -F\| use | as separator {for (i=1 ; i<= NF; i++) scan through pattern if ( $i ~/Y/ ) if found { split($i,A,"=") ; printf "in %d : %s\n",i,A[1] ;} split it and print it output in 3 : p2 in 11 : p6 use printf ...


1

There are several implementations of locate, and the ones I'm aware of want either POSIX extended regexps, or POSIX basic regexps. Neither support lookaheads.


1

You can use awk and set the field separator to either spaces or comma. Then, loop in blocks of 2, printing the first and second fields together with blocks of two: $ awk -v FS='(\\s+|,)' '{for (i=3; i<=NF; i+=2) print $1, $2, $i, $(i+1)}' file 3923 001 L05 LV 3923 001 L05 RM 3923 002 L12 RA 3923 002 L12 LA 3923 003 I06 ALL 3923 004 G04 RV 3923 004 Z09 ...


1

Simplyfied variant of User112638726 answer sed ':1;s/\(\(.*\s\s\+\)[^,]\+\),/\1\n\2/;t1' sed -r ':1;s/((.+\s\s+)[^,]+),/\1\n\2/;t1' will choice repeated part as «any symbols before 2(or more) spaces». Then you can pipe output through uniq -s 10


1

According to the GNU Grep documentation: If the final byte of an input file is not a newline, grep silently supplies one. So, if you're using GNU grep, you should be able to use the beginning-of-line/end-of-line anchors (^ and $) as usual, even if you're not working on properly-formed plaintext files. grep '^.\{32\}$' fileA fileB


1

Here a fast hack: #!/bin/bash for i in *; do if [ $(echo $i|cut -c 1-2) == "NA" ]; then EXECUTE_COMMAND fi done Did I understand your question correct?


1

Simply: rm -rf /root/my-picture-2015-*/



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