Using SED to remove line from textfile - bash

I had a look around the internet for a line of code which would delete the whole line it found a match on.. So if it found file in the file below it would delete the whole file line.. i.e '13382748 | /root/file', which it does in the console...
/root/file:
13382748 | /root/file
13382749 | /root/test
The command below works in the console (as stated above) but does not work when running the script.
sed --in-place '/$number/d' /root/file
It is the last piece to finally complete my script. The line of code simply does nothing..
Any ideas?

The single quotes prevent variable expansion. Say:
sed --in-place "/$number/d" /root/file

Related

Remove first two characters from a column in a text file excluding the headers

I want to remove the first two characters of a column in a text file.
I am using the below but this is also truncating the headers.
sed -i 's/^..//' file1.txt
Below is my file:
FileName,Age
./Acct_Bal_Tgt.txt,7229
./IDQ_HB1.txt,5367
./IDQ_HB_LOGC.txt,5367
./IDQ_HB.txt,5367
./IGC_IDQ.txt,5448
./JobSchedule.txt,3851
I want the ./ to be removed from each line in the file name.
Transferring comments to an answer, as requested.
Modify your script to:
sed -e '2,$s/^..//' file1.txt
The 2,$ prefix limits the change to lines 2 to the end of the file, leaving line 1 unchanged.
An alternative is to remove . and / as the first two characters on a line:
sed -e 's%^[.]/%%' file1.txt
I tend to use -e to specify that the script option follows; it isn't necessary unless you split the script over several arguments (so it isn't necessary here where there's just one argument for the script). You could use \. instead of [.]; I'm allergic to backslashes (as you would be if you ever spent time working out whether you needed 8 or 16 consecutive backslashes to get the right result in a troff document).
Advice: Don't use the -i option until you've got your script working correctly. It overwrites your file with the incorrect output just as happily as it will with the correct output. Consequently, if you're asking about how to write a sed script on SO, it isn't safe to be using the -i option. Also note that the -i option is non-standard and behaves differently with different versions of sed (when it is supported at all). Specifically, on macOS, the BSD sed requires a suffix specified; if you don't want a backup, you have to use two arguments: -i ''.
Use this Perl one-liner:
perl -pe 's{^[.]/}{}' file1.txt > output.txt
The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.
s{^[.]/}{} : Replace a literal dot ([.]) followed by a slash ('/'), found at the beginning of the line (^), with nothing (delete them). This does not modify the header since it does not match the regex.
If you prefer to modify the file in-place, you can use this:
perl -i.bak -pe 's{^[.]/}{}' file1.txt
This creates the backup file file1.txt.bak.
SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
perldoc perlrequick: Perl regular expressions quick start

Why running sed command cutting my path first letter?

sed -i s/CUSTOMER_UNIT=".*"/CUSTOMER_UNIT="Test" core/src/main/java/com/appname/core/AppConstants.kt
I am running this sed command as a result I get this
sed: ore/src/main/java/com/appname/core/AppConstants.kt: No such file or directory
It remove the first letter of core -> ore
but if I just run command find ore/src/main/java/com/appname/core/AppConstants.kt it this file exists
I am not sure if you are trying to have the results = core or ore
but when i replicate I am able to get core. One thing I noticed is in your example you are missing the terminating / for your sed command after "Test"
sed -i s/CUSTOMER_UNIT=".*"/CUSTOMER_UNIT="Test"/ core/src/main/java/com/appname/core/AppConstants.kt
sed: can't read core/src/main/java/com/appname/core/AppConstants.kt: No such file or directory
if you are trying to cut the C from core and you can manually validate the path exists I would be sure that you have proper permissions
The command should be enclosed in quotes and be ended with "/".
sed -i 's/CUSTOMER_UNIT=".*"/CUSTOMER_UNIT="Test"/' core/src/main/java/com/appname/core/AppConstants.kt

What is wrong with this sed command?

I am facing a strange problem. An answer to what I want to do already exists Here. I am trying to remove trailing commas from each line of a file containing thousands of lines. Like this -
This is my command -
sed -i 's/,*$//g' file_name.csv
However, the output I get is exactly the same as the image above and the trailing commas are not removed.
I think SED is not matching the pattern and thus failing to replace the commas. To check if there are any hidden characters in the file, I used VIM's :set list option -
There are only $ at the end of each line which is just what is expected.
I can't understand why the command is failing.
I can suggest you two options:
First One is my favorite.
dos2unix file
#####will work for Huge File also
then try to run the command.
Other way to do this:
cat file | tr -d '\r' > file
###may not work for huge file
then run the command.
tr -d '\r' < file > file.tmp ; mv file.tmp file
##will work for Huge File also
Thanks to #Nahuel for suggesting last command.

Create polymorphic bash without die trying. Sed replacing

I'm creating a bash script which in one point needs to modify itself in order to make persistent a change (only one line) of the script needs to change.
I know sed -i is what I need to do this. The problem is my sed command is replacing the line where the command is stored instead of the line I want. So I guess I need to include an exclusion while replacing. Let's check the snippet code stuff:
#!/bin/bash
echo "blah,blah,blah"
echo "more code here, not matters"
sed -i "s/#Awesome line to be replaced/#New line here/" "/path/to/my/script" 2> /dev/null
#Awesome line to be replaced
echo "blah,blah,blah, more code blah"
The problem here is the replaced line is not the line with only #Awesome line to be replaced. It is replaced the line where the sed command is.
This is a reduced example but the script is polymorphic and maybe the line numbers change, so it can't be based on line numbers. And there will be more sed commands like this... so I thought It could be nice to have some piece of text which always could be in the sed command lines in order to use it as excluding pattern, and yeah! that piece of text is /dev/null which always will be in sed command lines and never in the line which I want to replace.
How can achieve this using sed -i? Thanks in advance.
EDIT Forgot to say the order of appearance (offset) can't be used neither because of the polymorphic thing.
EDIT2 Beginning chars before #Awesome line to be replaced can't be used because they could change too. Sorry for who already answered based on this. Is complicated to write a polymorphic snippet considering all the possibilities.
This hack can work:
sed -i "s/#[A]wesome line to be replaced/#New line here/" "/path/to/my/script" 2> /dev/null
I think it is self-explanatory, why it will not match the sed line itself.
Anchor your expression by starting your sed line with :
sed -i "s/^$'\t'*#Awesome (rest of command goes here)
This will make sure sed only matches if the text found is at the beginning of the line with zero or more tabs, and will not match the line with the actual sed command.

Write output of command to specific line

I need to write the output of a command to a specific line in a document. I can not just append it like so COMMAND | cat >> file, I need it to be added between two lines without replacing one or the other. I'm sure you must be able to do this via sed.
The following solution works when the output of COMMAND is only 1 line (inserting to line 4):
COMMAND | sed -i "4i \`cat` FILE"
Use that command:
command | sed -i '3r /dev/stdin' file
That inserts text after the 3rd line and reads from stdin (all output from command).

Resources