How to remove the leading and trailing space from each line of a file using shell script? - text-processing

I have a file with some leading and trailing spaces. Here is the file
val1=22
val2=23
val4=34
How can I remove both the leading and trailing white space from it? The white space could be a 'tab' too. Is there a single command to do it?

You have a very wide variety of options to achieve the desired result. One of them is simply to use Notepad++, one of the best text editors around:
Open your file in Notepad++,
Press Ctrl+H to open the "Replace" dialog box,
Insert the (^[\s\t]+)|([\s\t]+$) in the "Find what" text box,
Leave the "Replace with" text box blank,
Select the "Regular expression" in the "Search Mode" group at the bottom of the dialog box.
Press the "Replace All" button and you're done.
The code inserted in the "Find what" text box is a regular expression that instructs the finder to find the leading (^[\s\t]+) or trailing ([\s\t]+$) spaces or tabs.

Here is the command I found to remove both the leading and trailing spaces and tabs from every line of a file. It works for me.
sed -i 's/^[ \t]*//;s/[ \t]*$//' "filename"
Where
s/ : Substitute command ~ replacement for pattern (^[ \t]*) on each addressed line
^[ \t]* : Search pattern ( ^ – start of the line; [ \t]* match one or more blank spaces including tab)
// : Replace (delete) all matched pattern
Credit goes to this link

Related

Specify anything in brackets - vscode

I have a file that contains texts and inside each text there is a number in parentheses
Is there a way in vscode to select all the numbers in parentheses and delete or replace them in an easy way, I can't do it manually because the scripts exceed 5000
image
You can use the search function in VS Code.
Click on the magnifying glass, in the sidebar.
toggle search details by clicking on the 3 dots in the search pane
name your file in the files to include
enable regular expression
use a regular expression that fits your case, i.e \(\d+\)
enter a replace text (or leave it empty to remove), in the second input and click replace all
You could also use sed to do that.
sed -E 's/\([0-9]+\)/(something else)/g' file.txt > newfile.txt

How to wrap text in Notepad++, but leave paragraph lines intact?

How can I wrap text in Notepad++, but leave paragraph lines intact?
Example:
The original text looks like this:
This
is
paragraph
one.
This
is
paragraph
two.
I would like the text to look like this:
This is paragraph one.
This is paragraph two.
But currently, when I go to Edit > Blank Operations > Remove Unnecessary Blank and EOL, the text ends up with no paragraph line in between, like this:
This is paragraph one. This is paragraph two.
How can I fix this?
Thank you.
You may try the following find and replace, in regex mode:
Find: ([^.\s])\r?\n
Replace: $1[ ]
Demo
The regex pattern ([^.\s])\r?\n will match and capture any letter which appears before a CR?LF character, so long as that character is not a full stop. It then replaces with that same character, followed by just a single space, thereby removing/replacing the original CR?LF.
This will replace every single linebreak with a space.
Ctrl+H
Find what: [^\r\n]+\K\R(?!\R)
Replace with: # a space
CHECK Wrap around
CHECK Regular expression
Replace all
Explanation:
[^\r\n]+ # 1 or more non linebreak
\K # forget them
\R # any kind of linebreak (i.e. \r, \n, \r\n)
(?!\R) # negative lookahead, make sure we haven't another linebreak after
Screenshot (before):
Screenshot (after):

sublime text / ms word delete misc line breaks in code

I have a csv file that has random line breaks throughout the file. (probably load errors when the file was created where the loader somehow managed to put a carriage return into the field)
How do I go in and remove all carriage returns / line breaks where the last character is not "
I have word and sublime text available for text editors
I have tried ^p with a letter infront and find and replace, but that doesnt seem to work for some of the lines for some reason
Example
"3203","Shelving Units
",".033"
instead of
"3203","Shelving Units",".033"
and
"3206","Broom
","1.00"
instead of
"3206","Broom","1.00"
Menu > Find > Replace... or Ctrl+H
Select "Regular Expression" (probably a .* icon in the bottom left, depending on your theme).
Use \n to select newlines (LF) or \r\n (CRLF).
As #GerardRoche said you can use search and replace in Sublime Text. Open it via ctrl+h and press alt+r to toggle regex to enable it. (You may want to create a backup of your file before doing such changes.)
Search for (?<=[^"\n])\n+ and replace it with nothing, press Replace All or ctrl+alt+enter to replace it.
The regex just mean: search for alt least one (+) newlines (\n), that are preceded by something different than a quotation mark or a newline (?<=[^"\n]).
You don't need to worry about carriage returns, because ST only uses them when reading and writing the file and not in the editor.

Libreoffice writer adds a line at the bottom on pressing multiple hypen and enter

in libreoffice writer if I type hypen (-) consecutively many times like -------------- and then press enter then a line appears at the bottom of line
How to remove this line and keep hypen as is?
Taken from here:
If you start a new line in a Writer text document by typing three or more hyphen characters and press the Enter key, the characters are removed and the previous paragraph gets a line as a bottom border.
To create a single line, type three or more hyphens (-), or underscores ( _ ), and then press Enter. To create a double line, type three or more equal signs (=), asterisks (*), tildes (~), or hash marks (#), and then press Enter.
To remove an automatically drawn border, choose Format - Paragraph - Borders and select no border.
To undo an automatic border replacement once, choose Edit - Undo.
To disable the automatic borders, choose Tools - AutoCorrect Options - Options and clear Apply border.

Surround with quotation marks

How is it possible in Eclipse JDT to convert a multiline selection to String. Like the following
From:
xxxx
yyyy
zzz
To:
"xxxx " +
"yyyy " +
"zzz"
I tried the following template
"${line_selection}${cursor}"+
but that way I only get the whole block surrounded not each line separately. How can I achieve a multiline processing like commenting the selected block?
Maybe this is not what you mean but...
If I'm on a line in Eclipse and I enter double quotation marks, then inside that paste a multiline selection (like your xyz example) it will paste out like this:
"xxxx\n" +
"yyyy\n" +
"zzz"
Then you could just find/replace in a selection for "\n" to "", if you didn't intend the newlines.
I think the option to enable this is in Window/Preferences, under Java/Editor/Typing/, check the box next to "Escape text when pasting into a string literal". (Eclipse 3.4 Ganymede)
Find/Replace with the regex option turned on. Find:
^(.*)$
Replace with:
"$1" +
Well, the last line will have a surplus +, you have to delete it manually.
I would go with a Find/Replace eclipse in regexp mode:
Find:
^((?:\s(?)\S?)((?:\s(?![\r\n])))
Replace with
\1"\2"\3 +
Will preserve exactly whatever space or tabs you have before and after each string, and will surround them with the needed double-quotes. (last '+' needs to be removed)
This may not be exactly the answer you're looking for. You can easily achieve what you're asking by using the sed stream editor. This is available on all flavors of Unix, and also on Windows, by downloading a toolkit like cygwin. On the Unix shell command line run the command
sed 's/^/"/;s/$/"+/'
and paste the text you want to convert. On its output you'll obtain the converted text. The argument passed to sed says substitute (s) the beginning of a line (^) with a quote, and substitute (s) the end of each line ($) with a quote and a plus.
If the text you want to convert is large you may want to redirect sed's input and output through files. In such a case run something like
sed 's/^/"/;s/$/"+/' <inputfile >outputfile
On Windows you can also use the winclip command of the Outwit tool suite to directly change what's in the clipboard. Simply run
winclip -p | sed 's/^/"/;s/$/"+/' | winclip -c
The above command will paste the clipboard's contents into sed and the result back into the clipboard.
Finally, if you're often using this command, it makes sense placing it into a shell script file, so that you can easily run it. You can then even assign an Eclipse keyboard shortcut to it.