Tag Info

New answers tagged

0

As an alternative to the other answers, you can use the graphical tool GPRename. It can replace characters, truncate filenames, etc. The advantage is that there's a built-in preview function to check the new filenames before renaming them. But since it works on one directory at a time, it will be inconvenient to use it with numerous folders in sub-folders. ...


0

Here is an awk solution: $ awk -F '/|:' ' $3 == "8013765024" {flag = 1} $0 == ",11:1" && flag {$2 = 2;flag = 0} 1 ' OFS=':' file


6

With the Perl rename tool (which is called rename on Debian and friends including Ubuntu, it may be prename elsewhere): rename -n 's/(?<!\.)jpg$/.jpg/' * # -n makes it show you what it'll do, # but not actually do it. Remove the -n to # actually rename To break down that patter: the ...


3

While sed is a very useful and versatile tool, you're not using it properly. It's best used to match and substitute strings in text files; it can't directly rename files on the filesystem. This task is better suited for a bash one-liner (assuming that's your shell). To rename something like . filejpg to file.jpg, use this: find . -name '. *' -print0 | ...


0

this may seem helpful if you want just extract the number part of 50.0'C , you can also use cut. it's may seem simple than using regular expression. echo "temp=50.0'C" | cut -d= -f2 | cut -d\' -f1


0

Assuming your input is wellformed, file with list and number as parameter, this should work in PHP: <?php $count = 0; foreach(explode("\n",file_get_contents($argv[1])) as $line) foreach(explode(",",$line) as $cols) { $data = split(',',$cols); if(((count($data)>0)&&($data[0]==$argv[2])) || ...


0

You could match any initial space-separated sequence of pairs of hex digits into a substitution group, and then just re-substitute that group, e.g. sed -r 's/^(([[:xdigit:]]{2}\s+)+).*$/\1/'


0

sed -e '/^precalculated/!s/^\(.\{47\}\).*$/\1/' < h This keeps first 47 characters on any line not matching precalculated at the beginning. Lines matching are just copied.


0

I found this pattern: sed -e 's/^\(..\s.\{45\}\).*/\1/'


0

This example uses function match. awk -F ',' '{num = 348; i = 0; while(i <= NF) {i++; match($i,/([0-9]+)-?([0-9]*)/,arr); if(arr[1] == num || (arr[1] <= num && num <= arr[2])){count++}}} END {print count}' file


1

With sed you can combine conditional addressing and backreferences like this: sed "/temp=\([0-9.]*\)'C/!d;s//=\n\1\n/;s/.*=\n//;P;D" It is admittedly little more than an expansion on godlygeek's answer, but written with an entire file input in mind. It prints only the sequence of digits and .dots occurring between the two strings temp= and 'C - even if ...


2

This answer will provide the fields that contain the specified number, not just the lines, if you are after that level of detail (and if the ranges in your data might contain overlaps): awk -v num=348 -F, '{ for (i=1; i<=NF; i++) { if ($i == num || (split($i, a, /-/) == 2 && (a[1] <= num && num <= a[2]))) { print $i ...


4

A GNU awk solution that treats , or \n as a record separator and - as a field separator. An equality check or a range check is applied depending on number of fields awk -v num=348 -v RS=',|\n' -F'-' 'NF == 2 && $1 <= num && $2 >= num{c++}; NF == 1 && $0 == num{c++}; END{print c+0}' file 2


3

If you can use perl: $ perl -F',' -anle ' for (@F) { ($l,$h) = split "-"; $count++ if $l == 348 || ($l < 348 and $h >= 348); } END {print $count} ' file 2


0

Possible method to approach the problem (as there are I am sure many ways to get this done) is to simplify the checks for the number. Use nested if statements to move through the logic, naturally splitting the 'values' to check based on a comma delimeter. If the value has a "-" then for the check, split the two numbers at the "-". Then it is a simple ...


0

You can group the expression and use \1 to point to the group. E.g sed 's/^\($RELEASE.*=\).*/\1 '"'1234'"'/' config.ini


2

I don't have any problem with [[:space:]]. Here's a really silly little example showing the mixed-replacement of spaces and tabs: $ echo -e 'A \t \t B' | sed 's/A[[:space:]]*B/WORKED/' WORKED You can also use \s which is often preferable with big sed strings because it's much shorter: $ echo -e 'A \t \t B' | sed 's/A\s*B/WORKED/' WORKED Anyway, I ...


2

The portable way to do this - and the more efficient way - is with addresses. You can do this: printf %s\\n cat dog pear banana cat dog | sed '/cat/!{/dog/!b};cBear' In this way if the line does not contain the string cat and does not contain the string dog sed branches out of the script, autoprints its current line and pulls in the next to begin the next ...


5

By default sed uses POSIX Basic Regular Expressions, which don't include the | alternation operator. Many versions of sed, including GNU and FreeBSD, support switching into Extended Regular Expressions, which do include | alternation. How you do that varies: GNU sed uses -r, while FreeBSD, NetBSD, OpenBSD, and OS X sed use -E. Other versions mostly don't ...


3

This happends because (a|b) is a regular expression use -r option to deal with this: echo 'cat dog pear banana cat dog'|sed -r 's/cat|dog/Bear/g' from sed manpage: -r, --regexp-extended use extended regular expressions in the script.


3

The syntax \t for a tab character in sed is not standard. That escape is a GNU sed extension. You find a lot of examples online that use it because a lot of people use GNU sed (it's the sed implementation on non-embedded Linux). But OS X sed, like other *BSD sed, doesn't support \t for tab and instead treats \t as meaning backslash followed by t. There are ...


0

find /etc | xargs -I{} grep -l ‘1-[0-9]\{3\}-[0-9]\{3\}-[0-9]\{4\}’ {} | sort -nr > ~/etcphone.txt


0

egrep -l "1(-[[:digit:]]{3}){3}[[:digit:]]" /etc/* 2>/dev/null


1

Use a Bash specific quoting which lets you use strings like in C, so that a real tab character is passed to sed, not an escape sequence: sed -i.bak -E $'s/\t/ /' file.txt


0

As noted, not all sed implementations support the notation of \t as a horizontal tab. You can easily achieve your substitution with: perl -pi.old -e 's{\t+}{ }g' file.txt This performs an in situ replacment which preserves your original file as "*.old". Perl allows alternate delimiters for the classic / making the expression much more readable (i.e. ...


0

If you want a more powerful sed (supporting \t and more) than the one on OS X, install GNU sed.


0

Thanks for the tips everyone I ended up doing this: w3m -dump -T text/html "$thread" | grep -i -E -o 'File\:+([[:print:]]*)\.(jpg|png|webm|gif)' w3m cleans the code and then i can grep for the file names. (I need the literal "File:" part to distinguish a linked file from its title). I do need [[:print:]] because it catches most whitespace, unicode ...


3

Can you do this only using POSIX sed? Yes: sed -e 's/.^H//g' < data where ^H is just a literal backspace character. POSIX sed uses POSIX basic regular expressions, which are defined over bytes - printing characters or not, they don't care, so this behaves the same as if ^H were a letter. There are no extensions involved here. Note that all you really ...


11

Generally, you only have to escape one time to make special character considered literal. Sometime you have to do it twice, because your pattern is used by more than one program. Let disscuss your example: man gcc | grep \\. This command is interpreted by two programs, bash interpreter and grep. The first escape causes bash knows \ is literal, so the ...


2

Your regular expression is target="_blank">([[:graph:]]*)\.(jpg|png|gif|webm) This matches the literal text target="_blank">, followed by any number of non-whitespace characters, with one of the four strings .jpg, .png, .gif or .webm at the end. For example, the grep command would output the bold parts of the following lines: <a … ...


3

You could also use perl, which should support \b on all platforms. Assuming your list of replacements is in the format you show (separated by ->), you could do: perl -F"->" -ane 'chomp;$rep{$F[0]}=${$F[1]}; END{open(A,"file"); while(<A>){ s/\b$_\b/$rep{$_}/g for keys(%rep); ...


3

If you want more portable, you can use \< and \>: sed -i "s/\<$SEARCH\>/$REPLACE/g" file \< and \> work in gsed, ssed, sed15, sed16, sedmod. \b and \B work in gsed only. In Mac OSX, you must use this syntax: sed -i '' -e "/[[:<:]]$SEARCH[[:>:]]/$REPLACE/g" file


4

If you're using GNU sed (which bare -i suggests you are), there is a "word boundary" escape \b: sed -i "s/\b$SEARCH\b/$REPLACE/g" \b matches exactly on a word boundary: the character to one side is a "word" character, and the character to the other is not. It is a zero-width match, so you don't need to use capturing subgroups to keep the value with \1 and ...


4

The best strategy would be to use a proper html parser that can spit out the value of all <a> tags. Here, xmlstarlet is specifically an XML parser, and your HTML may not be well-formed XML, but you might get the idea: echo '<html> <a href="000000.jpg" title="image name.jpg" target="_blank">Image name.jpg</a> </html>' | ...


3

$ egrep -l "\b1-[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}\b" \ /etc/* 2>/dev/null | sort > ~/etcphone.txt The \b escape sequence matches a word boundary. This will prevent if from matching something like 1231-123-123-1234.


1

I was actually able to figure this out, and I figure I'd add it here for the next googler who bangs their head against the same wall. I had a grep alias and GREP_OPTIONS set. This caused color highlighting to remain on in the script, even when piping to another command. That usually doesn't play nicely with sed. Here's my .alias and options: alias ...


3

* means 0 or more, so effecively 0 or more s characters. There's the documentation here, that says For example, ph*' applies the*' symbol to the preceding h' and looks for matches of onep' followed by any number of h's. This also matches justp' if no `h's are present. In your case, you're doing opens* while you're probably expecting something like ...


1

The pattern in the substitute command is: [:;]\([^:;]*\)\([:;].*\)[;:]\1. Notice the \1 on the end. This means whatever text matches the first group, \([^:;]*\) must also occur at the end of the pattern. Your pattern space is initially 0000:1111:2222:3333:4444:1111:2222:3333:0000. The pattern matches :1111:2222:3333:4444:1111, and 1111 matches the first ...


1

Pcmanfm allow you to filter the view. Try to press ctrl + e then if you write *.tex you see only tex file like when you use the ls command.


5

This is really quite simple. As long as the last field, the year, does not contain any whitespace (this is not clear from your question but I am assuming it is the case), all you need to do is remove the last field. For example: $ cat movies Casablanca 1942 Eternal Sunshine of the Spotless Mind 2004 He Died with a Felafel in His Hand ...


1

This should remove the last numeric characters and the tabs and spaces before it: sed -e 's#[\t ]*[0-9]*$##' movies.txt


7

Using gawk and assuming that the year always ends the record: awk -F"[0-9]{4}$" '{print $1}' movies


6

bash: while read -r line; do if [[ $line =~ (.*)[[:blank:]]+[0-9]{4}$ ]]; then echo "${BASH_REMATCH[1]}" fi done < data sed: sed 's/[[:blank:]]\+[0-9]\{4\}$//' < data


3

I assume the movie data will look something like below. cat movies one flew over the cuckoo's nest 1975 taxi driver 1976 the shining 1980 Now, I also assume the years in the movie data will always be 4 characters at the end. So, now if you use the commands as below, awk '{ gsub (" ", "", $0); print}' movies | rev | cut -c 5- | rev ...


6

The standard test command also known as [ doesn't have a =~ operator. Most shells have that command built-in nowadays. The Korn shell introduced a [[...]] construct (not a [[ command) with alternate syntax and different parsing rules. zsh and bash copied that to some extent with restrictions and many differences but that never was standardized, so ...


5

In bash old test [ does not support regex. You must use new test [[ instead: re="\/$" if [[ $1 =~ $re ]]; then echo "${ATTENTION_PREFIX}$1 DIRECTORY MAY NOT CONTAIN A \"/\" OR LITERAL SLASH!${ATTENTION_POSTFIX}" exit 1 fi You can see more here. You'll need to change your #!/bin/sh shebang line to #!/bin/bash, as well.


5

Change #!/bin/sh to #!/bin/bash, and use double brackets instead: if [[ $1 =~ $re ]]; then This is the extended test command, as opposed to the (regular) test command. =~ can only be used with the [[ ... ]] version, and requires Bash 3.0 or later.


3

You could do something like: $ echo 'aabiicaa' | perl -lne ' while (/aa|ii/g) {print substr($`,-3)."[$&]".substr($'\'',0,2)}' [aa]bi aab[ii]ca iic[aa]


1

A Perl ones: perl -nle 'print unless /\p{XPosixPunct}/' file This will match -!"#$%&'()*+,./:;<=>?@[\]^_`{|}~ which unicode consider Punctutation and Symbols. or: perl -nle 'print unless /\p{Punct}/' file \p{Punct} only matches -!"#%&'()*,./:;?@[\]_{}, missing $+<=>^`|~ which unicode consider Symbols. perl by default use POSIX locale. ...


1

In sed you can do something like: sed '/[[:punct:]]/!d' In awk you can do: awk '!/[[:punct:]]/'



Top 50 recent answers are included