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


13

sed creates a temporary file, writes the output into that file, and then renames the temporary file over the top of the original. You can watch what happens using strace: $ strace -e trace=file sed -i -e '' a execve("/usr/bin/sed", ["sed", "-i", "-e", "", "a"], [/* 34 vars */]) = 0 <...trimmed...> open("a", O_RDONLY) = 3 ...


10

grep will do a better job of this: grep -B 3 141.299.99.1 TESTFILE The -B 3 means to print the three lines before each match. This will print -- between each group of lines. To disable that, use --no-group-separator as well. The -B option is supported by GNU grep and most BSD versions as well (OSX, FreeBSD, OpenBSD, NetBSD), but it is technically not a ...


10

Backslash the +: $ echo "104_Fri" | sed 's/^\([0-9]\+\)_\([A-Za-z]\+\)$/\1;\2/' 104;Fri Note that + is not a standard basic regular expression metacharacter, and so doesn't have portable behaviour in sed even when backslashed. You should use sed -r or sed -E to enable extended regular expressions instead, in which you don't need to backslash any of these ...


9

Assuming that 20000-words.txt is already in the format of one word per line, do: grep -vFf 20000-words.txt 50000-lines.txt >50000-filtered-lines.txt The -f argument to grep tells it to read patterns from a file, one pattern per line, instead of taking them as command line arguments. The -F argument to grep tells it that the patterns should be used as ...


8

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


8

With sed you can do a sliding window. sed '1N;$!N;/141.299.99.1/P;D' That does it. But beware - bash's insane behavior of expanding ! even when quoted!!! into the command string from your command history might make it go a little crazy. Prefix the command with set +H;if you find this is the case. To then re-enable it (but why???) do set -H afterward. ...


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

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


7

You should be able to use sed -e :a -e '/\\$/N; s/\\\n//; ta' See Peter Krumins' Famous Sed One-Liners Explained, Part I, 39. Append a line to the next if it ends with a backslash "\".


7

You must escape plus symbol +, too: $ echo "104_Fri" | sed 's/^\([0-9]\+\)_\([A-Za-z]\+\)$/\1;\2/' 104;Fri


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


6

Sed can't do arithmetic¹. Use awk instead. awk ' $4 == "calc" {sub(/calc( |\t)/, sprintf("%-6.2f", $3 - $2))} 1' The 1 at the end means to print everything (after any preceding transformation). Instead of the text substitution with sub, you could assign to $4, but doing so replaces inter-column space (which can be any sequence of spaces and tabs) ...


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

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

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

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


5

You can save long string to bash variables, then use in sed command: string=[long1][long2] replace= [long3][long4] sed -e 's/'"$string"'/'"$replace"'/' file If you can use perl, you can break long pattern with x modifier: perl -e 's/ [long1] [long2] /[long3][long4]/x' file


5

sed 's'/\ '[long1]'\ '[long2]'\ '/'\ '[long3]'\ '[long4]'\ '/' file.txt Splitting on several lines with backslash does work if new lines are not indented. $ echo "a,b" | sed 's/\(.'\ > '\),\(.\)/\2-\1/' b-a Tested on Cygwin with GNU sed 4.2.2


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


4

GNU sed is bundled with releases newer than Solaris 10. Otherwise, you can easily build it from source or retrieve it from opencsw or other freeware repositories. Solaris 10 packages are listed in this pdf: http://docs.oracle.com/cd/E19253-01/pdf/817-0545.pdf



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