Delete a specific string with tr
In your example above the cut command would suffice. Example: echo ‘1.1231’ | cut -d ‘.’ -f 2 would return 1231. For more information on cut, just type man cut.
In your example above the cut command would suffice. Example: echo ‘1.1231’ | cut -d ‘.’ -f 2 would return 1231. For more information on cut, just type man cut.
grep -i turned out to be significantly slower than translating to lowers before grepping, so I ended up using a variation of #2. Thanks @mike-w for reminding me that a simple test goes a long way.
A simpler solution than the accepted one: truncate -s -1 <<file>> From the truncate man page (man truncate): -s, –size=SIZE set or adjust the file size by SIZE SIZE may also be prefixed by one of the following modifying characters: ‘+’ extend by, ‘-‘ reduce by, ‘<‘ at most, ‘>’ at least, “https://stackoverflow.com/” round down … Read more
From Useful one-line scripts for sed: # Delete all leading blank lines at top of file (only). sed ‘/./,$!d’ file # Delete all trailing blank lines at end of file (only). sed -e :a -e ‘/^\n*$/{$d;N;};/\n$/ba’ file Therefore, to remove both leading and trailing blank lines from a file, you can combine the above commands … Read more
No, tr is specifically intended to replace single characters by single characters (or, depending on command-line options, to delete characters or replace runs of a single character by one occurrence.). sed is probably the best tool for this particular job: $ echo “asdlksad ~ adlkajsd ~ 12345” | sed ‘s/~/~\n/g’ asdlksad ~ adlkajsd ~ 12345 … Read more
See string.translate import string “abc”.translate(string.maketrans(“abc”, “def”)) # => “def” Note the doc’s comments about subtleties in the translation of unicode strings. And for Python 3, you can use directly: str.translate(str.maketrans(“abc”, “def”)) Edit: Since tr is a bit more advanced, also consider using re.sub.
No need to use an echo + a pipe + sed. A simple substitution variable is enough and faster: echo ${DATE//\//\\/} #> 04\/Jun\/2014:15:54:26
Here’s how to do it with sed: sed ‘s/\\n/\n/g’ Example usage: To replace all occurrences of \n in a file in-place: sed -i ‘s/\\n/\n/g’ input_filename To replace all occurrences of \n through a pipe, and save into another file cat file1 file2 file3 file4 | sed ‘s/\\n/\n/g’ > output_file
Best guess is you are on windows and your line ending settings are set for windows. See this topic: How to change line-ending settings or use: tr ‘\r\n’ ‘ ‘
Use tr ‘\n’ ‘ ‘ to translate all newline characters to spaces: $ grep pattern file | tr ‘\n’ ‘ ‘ Note: grep reads files, cat concatenates files. Don’t cat file | grep! Edit: tr can only handle single character translations. You could use awk to change the output record separator like: $ grep pattern … Read more