I have a file as below.
Hi this is first line
this is second line
this is third line
Expected is:
Hi this is first line
this is second line
this is third line
What i used is
cat file.txt | sed 's/ //g'
returns,
Hi this is first line
this is second line
this is third line
For a portable sed command use this:
sed 's/^[[:blank:]]*//' file
[[:blank:]] matches space or tab.
EDIT: To remove all spaces using awk:
awk '{$1=$1}1' OFS= file
OR sed:
sed 's/[[:blank:]]*//g' file
sed in your example will replace all the spaces
sed 's/^[ \t]*//' file.txt
cat file.txt | sed -e 's/^[ \t]*//'
OR
sed 's/^[ \t]*//' file.txt
OR if you want to modify file.txt and remove the white spaces in the beginning of the line :
sed -i 's/^[ \t]*//' file.txt
try this line
sed -r 's/^\s*//' file
to remove all spaces(tabs): sed -r 's/\s//g' file
kent$ echo "Hi this is first line
this is second line
this is third line"|sed -r 's/\s//g'
Hithisisfirstline
thisissecondline
thisisthirdline
The most efficient way to remove all blanks from your input is probably not using sed at all, but
tr -d '[:blank:]' < file.txt
That's different from what you have originally asked for, though (removing initial whitespace only).
Related
The test.txt file contains 3 lines:
STREET=main
PHONE=123
EMAIL=abc#xyz.com
To remove two lines with the STREET and EMAIL I run the sed twice in a row:
sed -i -- 's/STREET=.*//' test.txt
sed -i -- 's/EMAIL=.*//' test.txt
Instead of using the sed command twice I would rather remove both lines with a single sed command. How to do it?
To delete (d) lines which contain STREET= or EMAIL=.
sed -i -- '/STREET=/d; /EMAIL=/d' file
sed -i -- 's/STREET=.*//;s/EMAIL=.*//' test.txt
The following sed one-liners show how to remove/empty the target lines:
Empty the target lines:
kent$ sed 's/^\(EMAIL\|STREET\)=.*//' file
PHONE=123
Remove the target lines:
kent$ sed '/^\(EMAIL\|STREET\)=/d' file
PHONE=123
Using pattern ^\(EMAIL\|STREET\)= will avoid to touch lines like USER_EMAIL=... or SOME_STREET=
I am trying to understand the below command. Looked many tutorials and references. Can anyone help What actually it does.
sed -e '$d' -e '1,1d' File1 >File2
$d wil delete the last line
To delete lines between n and m use n,md. ie. 1,1d is same as 1d, which deletes the first line
So sed -e '$d' -e '1,1d' File1 >File2 will remove first and last line from File1 and send to file2
I could do the following in python to clean and strip unwanted whitespaces, but can it be done just through the terminal by other means like sed , grep or something?
outfile = open('textstripped.txt','w+','utf8')
for i in open('textfile.txt','r','utf8'):
print>>outfile, i.strip()
Using perl on the command line:
perl -lpe 's/^\s+//; s/\s+$//' file.txt > stripped.txt
This solution is based on sed man page:
sed 'y/\t/ /;s/^ *//;s/ *$//' input > output
http://www.gnu.org/software/sed/manual/sed.html#Centering-lines
Description:
y\t/ / replaces tabs with spaces
s/^ *// removes leading spaces
s/ *$// removes trailing spaces
$ cat input.txt | sed 's/^[ \t]*//;s/[ \t]*$//' > output.txt
This gets rid of the leading and trailing white spaces..
EDIT: sed -e "s/^[ \t]+//; s/[ \t]+$//" -i .bk input.txt
This does in place file editing, and saves backup to input.txt.bk (and saves a process as some suggested)
sed -E "s/(^[ \t]+|[ \t]+$)//" < input > output
Or if you have a GNU-compliant version of SED:
sed -E "s/^\s+|\s+$//g" < in > out
If you have a Mac, I recommed getting homebrew and installing gnu-sed.
Then, alias sed=gsed.
How i can delete whitespace in each line of file, using bash
For instance, file1.txt. Before:
gg g
gg g
t ttt
after:
gg g
gg g
t ttt
sed -i 's/ //g' your_file will do it, modifying the file inplace.
To delete only the whitespaces at the beginning of one single line, use sed -i 's/^ *//' your_file
In the first expression, we replace all spaces with nothing.
In the second one, we replace at the beginning using the ^ keyword
tr(delete all whitespaces):
$ tr -d ' ' <input.txt >output.txt
$ mv output.txt input.txt
sed(delete leading whitespaces)
$ sed -i 's/^ *//' input.txt
use can use perl -i for in place replacement.
perl -p -e 's/^ *//' file
To delete the white spaces before start of the line if the pattern matches. Use the following command.
For example your foo.in has pattern like this
This is a test
Lolll
blaahhh
This is a testtt
After issuing following command
sed -e '/This/s/ *//' < foo.in > foo.out
The foo.out will be
This is a test
Lolll
blaahhh
This is a testtt
"Whitespace" can include both spaces AND tabs. The solutions presented to date will only match and operate successfully on spaces; they will fail if the whitespace takes the form of a tab.
The below has been tested on the OP's specimen data set with both spaces AND tabs, matching successfully & operating on both:
sed 's/^[[:blank:]]*//g' yourFile
After testing, supply the -i switch to sed to make the changes persistent-
File1:
hello
world
How would one delete the leading/trailing blank spaces within this file using sed - using one command (no intermediate files)?
I've currently got:
sed -e 's/^[ \t]*//' a > b
For leading spaces.
sed 's/ *$//' b > c
And this for trailing spaces.
You almost got it:
sed -e 's/^[ \t]*//;s/[ \t]*$//' a > c
Moreover on some flavours of sed, there is also an option for editing inline:
sed -i -e 's/^[ \t]*//;s/[ \t]*$//' a
easier way, using awk
awk '{$1=$1}1' file
or
awk '{gsub(/^ +| +$/,"")}1' file
perl -lape 's/^\s+|\s+$//g'
Honestly, I know perl regexps the best, so I find perl -lape much easier to use than sed -e.
Also, to answer the original question, you can have sed execute multiple operations like this:
sed -e 's/something/something else/' -e 's/another substitution/another replacement/'
Apparently you can also put the two substitutions in one string and separate them with a semicolon, as indicated in another answer.
Note that in the more general case of applying several filters in a row to an input file without using intermediate files, the solution is to use pipes:
sed -e 's/^[ \t]*//' a | sed -e 's/ *$//' > c
Obviously they are not required here because one invocation of sed is sufficient, but if the second sed command was something different, like uniq or sort, then this pattern is the right one.