Tag Info

Hot answers tagged

14

^F is vim telling you there is a non-printable character 0x06 there (F is the sixth letter of the alphabet, they range: '^@', '^A', '^B'... '^Y', '^Z'. '^[', '^\', '^]', '^^', '^_') I had no problem removing it graphically in vim, nano, joe… but if you prefer a command line approach, knowing that it's the character 0x06, you can use sed -i 's/\x06//g' ...


7

sed 's/\\n/\ /g' Notice the backslash just before hitting return in the replacement string.


7

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


7

Yes, sed is on all Unix versions. It is part of the POSIX standard: every "official" Unix includes it, and all Linux and BSD distributions include it as well. There are several different versions of the sed command, with different extensions. If you stick to the common subset of sed options and language described by POSIX then your command will work on all ...


7

You really are not going to be able to do this with a simplistic sed script. I’m assuming that you will want to reduce to “citation forms”, collapsing all inflections into a base form. That means that adjectives like protégé, protégés, protégée, protégées all count as the same thing, the base adjective/participle protégé. Similarly, all inflections of ...


6

That would be because the [...] matches on a character. sed would try and match characters against the range specified in [...]. In UTF-8 locales, you can only encounter \x8f as part of a multi-byte character. You'll notice that . doesn't match on it either (and that's a POSIX requirement). For instance: sed 's/[eé\xa9]//' would not make sense. é is a ...


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


5

You're using double quotes to delimit the string as well as inside the string itself, so the quoted string stops early and your internal quote characters aren't included: sed -i "s|"jdbc:mysql:... Ends here-^ You can escape each of the quotes inside the string: sed -i "s|\"jdbc:mysql://localhost/bajaj\",\"root\", ...


5

The substitution command of sed replaces all characters matched in first section with all character of second section, so you will need .* or similar and group only the part to save, like: echo " ytmti (192.188.2.3) jjggy" | sed 's:^.*(\([^)]*\).*$:\1:' Note that I use [^)]* that avoids to hardcode the IP and generalize it for any of them. It yields: ...


5

Commands for grep and sed It looks like the original question was formatted incorrectly. Hence, the simplest grep and sed commands are: egrep '[0-9]{2}$' /path/to/file and sed -rn '/[0-9]{2}$/p' /path/to/file I'll leave the explanations below, which include information on these commands, but also work with multiple strings on single lines. They will ...


5

try using double quotes "": grep -oP "FW_6.0.0, SUCCESS" file OR (Because it is a fixed string, not a pattern): grep -oF "FW_6.0.0, SUCCESS" file from grep man page: -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) ...


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 ...


5

If your system has the GNU version of sed, you can use the GNU extension r command: r filename As a GNU extension, this command accepts two addresses. Queue the contents of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that if filename cannot be read, it ...


5

$ awk -F '[\t,]' '{print $1, NF-1}' some_file where -F sets the field separator, i.e. either tab or comma $1 references the first field NF is a built-in variable that contains the number of fields in a record the awk statement is executed for each record (i.e. for each line)


5

Try: echo "/mnt/VPfig/Amer/AR4/Celtel/files/COM.txt" | sed 's|^/[^/]*||' which gave me: /VPfig/Amer/AR4/Celtel/files/COM.txt It looks for the first / followed by as many non-/s as possible, then replaces them with an empty string.


5

POSIX does not mandate sed -i because it is redundant, and also not in line with the original purpose of sed. sed was originally meant to edit streams (hence its name stream editor), not files. The correct tool in POSIX to edit files non-interactively is ed printf "/text/d\nw" | ed file.txt Sadly, many current systems think ed is obsolete and don't ...


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 ...


5

An ugly way of doing this (i.e. causing a function call in shell based on output from awk) could look like this: awk -F '\t' ' FNR < 2 {next} FNR == NR { for (i=2; i <= NF; i++) { if (($i == 1) || ($i == 4)) printf "retrieve %s\n", $i if (($i == 2) || ($i == 2)) printf "retrieve2 ...


4

sed plus awk should do the trick! sed 's/ /\n/g' file1 | awk '/[0-9][0-9]$/' If your file contains these strings line by line awk only should do the trick! awk '/[0-9][0-9]$/' file1 If you want to match strings that end with EXACTLY two numbers and not more, then use these commands instead: sed 's/ /\n/g' file1 | awk '/[^0-9][0-9][0-9]$/' and awk ...


4

Using the field seperator variable in awk: echo "ytmti (192.188.2.3) jjggy" | awk -F'[)(]' '{print $2}' 192.188.2.3


4

If you want to use awk: awk '/FW_6\.0\.0/ && /SUCCESS/' file


4

> awk -v OFS="'^'" -F"'\\\\^'" '{if(length($2)>20) $2=substr($2,1,20); print;}' file 'XYZ843141'^'ASDFSAFXYVFSHGDSDg s'^'BAAAR'^'YYY'^'..... and so on, further columns 'YYZ814384'^'ASfdEtRiuognfnseaFRE'^'foooobaaar'^'ZZZ'^'..... and so on, further columns


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

If the existing commented lines form a single contiguous block, then you could match from the first commented line instead, commenting-out only those lines up to and including your end pattern that are not already commented sed '/^#/,/dotan/ s/^[^#]/#&/' file If the existing comments are not contiguous, then due to the greedy nature of the sed range ...


3

Phew, after some time I got it. This is a naive solution. terdon's answer is more correct and you should use his though :). sed -Ei.bak "s/(.*<string[^>]*\")(.*)-(.*)/\1\2\&#8211;\3/g" strings.xml I am using Backreferences to refer back to a previously matched string. These are \1 \2 etc. In this case sed should match following groups: ...


3

With a recent (for \K and s///r) perl and assuming your <string> tags don't nest: perl -0777 -pi.bak -e's{<string.*?>\K.*?(?=</string>)}{$&=~s/-/&#8211;/rg}ges' file.xml -0777: slurp mode: handle the whole file at once (to allow <string> tags to span several lines). -p: sed mode -i.bak: in-place editing with .bak extension ...


3

This would do it i hope. Events go to events file. And messages go to stdout. Save this file to myprogram.awk (for example): #!/usr/bin/awk -f BEGIN { s=0; ### state. Active when parsing inside an event nevent=0; ### Current event number printf "" > "events" } # Start of event /^ *Data control raising event/ { s=1; dentries=0; ...


3

With awk this would be awk 'NR >= 1200 && NR <= 1300' with sed: sed -n '1200,1300 p' FILE with head and tail: head -n 1300 FILE | tail -n 100 so many options, so many answers on stackexchange :)


3

This should do it: sed 's/Complex(\([^,]*\), *\([^)]*\))/(\1 + i*\2)/g; s/Power(\([^,]*\), *\([^)]*\))/\1^\2/g' file This will not work for nested statements like Power(Power(a, b), c). Explanation To susbstitute patterns in sed, you use the substitution operator (s/pattern/replacement/). The g at the end means global, it causes the substitution ...


3

With sed you could do: sed -e :1 -e 's/_\([^+]*@\)/?\1/;t1' That is replace _ followed by a sequence of non-+ characters followed by @ with ? with the sequence of character, and repeat the process as long as it matches. Or to do it only between EMAIL+ and SHR: sed -e :1 -e 's/\(EMAIL+.*\)_\(.*SHR\)/\1?\2/;t1' If you want to only consider the lines ...



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