What is the command to delete empty lines in a file using sed? - linux

What is the sed command to delete empty lines in a file.
What is the command (sed command?) to delete empty files in a folder?

You have to 'd' which is used to delete.
Ex:
sed -i '/^$/d' test
-i - is used to affect the file.
^ - is a beginning of line
$ - is a end of line
d - delete if there is a empty line
I hope this will help you.

/^$/d should do it.
for example
sed -i -e "/^$/d" myfile.txt
if you want to do this to all the files in a folder:
sed -i -e "/^$/d" *
-i means "edit in place" without it, the file will be edited and sent to standard output. The original file will be unmodified.

sed -e 's/#.*//;/^\s*$/d' /etc/lvm/lvm.conf

Related

How to remove the multiple lines in the file using sed

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=

How to replace string into numbers using sed?

I am trying to replace string into number from the file
So, I have variable &FLOW which need to change to 001, ex :
cat file.txt
This is file ' PISP &FLOW'
PISD.DATA.CYCLE&FLOW..ONE
desired output
This is file ' PISP 001'
PISD.DATA.CYCLE001.ONE
I tried below commands in a script :
for item in file.txt
do
sed 's/\&FLOW/\./001/g' $item
sed 's/\&FLOW/001/g' $item
done
It is giving error. The second sed command is working, but I need to run first the beginning sed command otherwise after running first the second sed command, it would ignore the beginning sed command.
Any help would be appreciated!
Use a single sed command and use -i to actually modify the file contents and you need to pass file.txt as the input for the sed command:
sed -i 's/&FLOW\.\{0,1\}/001/g' file.txt
See the online demo. If you are using it in Mac OS, you need sed -i '' 's/&FLOW\.\{0,1\}/001/g' file.txt. Also see sed edit file in place.
Pattern details
It is a POSIX BRE compliant pattern matching
&FLOW - a literal &FLOW substring
\.\{0,1\} - 0 or 1 occurrence of a . char.
try this:
for item in file.txt
do
sed 's/\&FLOW\./001/g' $item
sed 's/\&FLOW/001/g' $item
done
You had a redundant / in after FLOW
This might also work:
sed -i 's/\&FLOW[\.]?/001/g' file.txt

Linux command to replace set of lines for a group of files under a directory

I need to replace first 4 header lines of only selected 250 erlang files (with extension .erl), but there are 400 erlang files in total in the directory+subdirectories, I need to avoid modifying the files which doesn't need the change.
I've the list of file names that are to be modified, but don't know how to make my linux command to make use of them.
sed -i '1s#.*#%% This Source Code Form is subject to the terms of the Mozilla Public#' *.erl
sed -i '2s#.*#%% License, v. 2.0. If a copy of the MPL was not distributed with this file,#' *.erl
sed -i '3s#.*#%% You can obtain one at http://mozilla.org/MPL/2.0/.#' *.erl
sed -i '4s#.*##' *.erl
in the above commands instead of passing *.erl I want to pass those list of file names which I need to modify, doing that one by one will take me more than 3 days to complete it.
Is there any way to do this?
Iterate over the shortlisted file names using awk and use xargs to execute the sed. You can execute multiple sed commands to a file using -e option.
awk '{print $1}' your_shortlisted_file_lists | xargs sed -i -e first_sed -e second_sed $1
xargs gets the file name from awk in a $1 variable.
Try this:
< file_list.txt xargs -1 sed -i -e 'first_cmd' -e 'second_cmd' ...
Not answering your question but a suggestion for improvement. Four sed commands for replacing header is inefficient. I would instead write the new header into a file and do the following
sed -i -e '1,3d' -e '4{r header' -e 'd}' file
will replace the first four lines of the file with header.
Another concern with your current s### approach is you have to watch for special chars \, & and your delimiter # in the text you are replacing.
You can apply the sed c (for change) command to each file of your list :
while read file; do
sed -i '1,4 c\
%% This Source Code Form is subject to the terms of the Mozilla Public\
%% License, v. 2.0. If a copy of the MPL was not distributed with this file,\
%% You can obtain one at http://mozilla.org/MPL/2.0/.\
' "$file"
done < filelist
Let's say you have a file called file_list.txt with all file names as content:
file1.txt
file2.txt
file3.txt
file4.txt
You can simply read all lines into a variable (here: files) and then iterate through each one:
files=`cat file_list.txt`
for file in $files; do
echo "do something with $file"
done

for each line of a file, grep a specific string and make string substitution

I have a file containing more than 14000 records.
What I want to do is to process this file line by line and replace a String by anodher string returned by grep command.
For example:
Line :
/xxxxx/xxxxx/Class.java:67: Logger.w(TAG, "message");
My grep command to get Class.java string is (Class.java is juste an example):
grep -o '[a-zA-Z]*"*\.java"*'
I must, for each line, replace the TAG string by the class.java string return by grep command
You can use sed and do the following:
sed -r 's#(.*)/(.*)\.java(.*)(TAG)#\1\/\2\3\2#g'
Characters surrounded with parenthesis are groups that you can use in the second part to get their content.
In order to modify the file in-place, you should:
sed -ir 's#(.*)/(.*)\.java(.*)(TAG)#\1\/\2\3\2#g' your_file.txt
This is where sed comes in:
sed -i 's/TAG/class.java/g' Class.java
do it for all java files in current directory (assuming bash here):
sed -i 's/TAG/class.java/g' *.java
-i means in-place, so replacing takes place inside the file and is saved immediately. For the rest I suggest you google about sed.
You can use:
grep -rl 'old_string' ./ | xargs sed -i 's/old_string/Relacement_String/g'

How to remove a special character in a string in a file using linux commands

I need to remove the character : from a file. Ex: I have numbers in the following format:
b3:07:4d
I want them to be like:
b3074d
I am using the following command:
grep ':' source.txt | sed -e 's/://' > des.txt
I am new to Linux. The file is quite big & I want to make sure I'm using the write command.
You can do without the grep:
sed -e 's/://g' source.txt > des.txt
The -i option edits the file in place.
sed -i 's/://' source.txt
the first part isn't right as it'll completely omit lines which don't contain :
below is untested but should be right. The g at end of the regex is for global, means it should get them all.
sed -e 's/://g' source.txt > out.txt
updated to better syntax from Jon Lin's answer but you still want the /g I would think

Resources