How to delete a particular word from a file using shell script - shell

I have a file contains
demo demo1 demo2 demo3 demo12 demo13 demo23
I just want to delete only one line contains demo with out delete other contents
Also I can't specify the line number. Line no: varies alternately

sed -e 's/\<wordtodelete\>//g' file.txt
This will also print the contents of the file to confirm the word is destroyed.
sed -e 's/\<wordtodelete\>//g' -i file.txt
Use -i if you want to overwrite the file in place.

Maybe these will help,
In awk you could use:
awk '{gsub("demo", "");print}' input
1 2 3 12 13 23
Using perl you could use:
perl -p -e 's/demo//g' input
1 2 3 12 13 23
Both the awk and perl use regex to match the word demo, then remove it from the line

sed -i '/\b**demo**\b/d' filename

Related

Sed Capturing Repeating Number Groups

I am trying to use sed to capture a group like these examples:
123123 (i would want the first group 123)
144144 (I would want the group 144)
however sed does not seem to realize what \1 is.
Is there any way to do this using sed? I want to replace the first group with a specific string afterwards.
([0-9]+)\1
I have tried using the above regex yet, sed does not seem to realize what I am trying to do.
also tried this:
~/Desktop$ cat file
123123
23231
12323
123231
12345
144144
~/Desktop$ sed -n 's/.*\b\([[:digit:]]\{1,\}\)\1\b.*/\1/p' file
~/Desktop$
~/Desktop$ sed -n -E 's/([0-9]+)\1/specificstring\1/p' file
specificstring12323
specificstring2323
specificstring12323
specificstring14444
~/Desktop$ sed -nE 's/^([0-9]+)\1([^0-9]|$)/\1/p' file
2323
12323
Use a BRE, and avoid using + since it is not a part of POSIX REs.
$ cat file
123123
23231
12323
123231
12345
144144
$
$ sed -n 's/^\([0-9]\{1,\}\)\1$/\1/p' file
123
144
I want to replace the first group with a specific string afterwards.
With GNU sed :
sed -n -E 's/([0-9]+)\1/specificstring\1/p' file
Takeaways
-n suppresses the output which we override using the print (p ) flag of the s command.
-E enables extended regular expressions.
Note
This doesn't, however, print the lines where there no identical groups the existence of which is not mentioned in the question.
Given that the file only contains 6 digit numbers and nothing else it could be done like this:
sed -n 's/\([0-9]\{3\}\)\1/\1/p' file

Delete all rows which does not start with at least 3 digit

Delete all rows which does not start with at least 3 digit.
I use below sed command but it removes title as well.
How can I start below command from line 2, so that title remains as it is.
sed -n '/^[0-9]\{3\}/p' my_file
I have used below as well but does not work.
sed -n '2,${/^[0-9]\{3\}/p}' my_file
You're almost there. All you need to do is:
sed -e '2,${/^[0-9]\{3\}/d}' my_file
This will begin executing the script beginning from the second line (excludes the first one, which I assume it's the title you're talking about).
Using awk:
$ cat foo
Title
11 test
222 test
test
$ awk '/^[0-9]{3}/ || NR==1' foo
Title
222 test
Edit: Title is first row. So this command should start from row number 2.
$ awk '/^[0-9]{3}/ || NR>1' foo
222 test
This might work for you (GNU sed):
sed -i '1b;/^[0-9]\{3\}/!d' file
Do not process the first line. If any line thereafter does not begin with at least 3 digits, delete it.
N.B. -i option replaces the contents of the old file with the amended file. If multiple files are placed following the sed commands, they will be treated separately i.e. each files header will be remain. To achieve the same effect without over writing the files use the -s option.
Another way:
sed '/^[0-9]\{3\}/b;1!d' file
For portability, try:
sed -n '1p;2,${/^[0-9][0-9][0-9]/p}' my_file
Or sed -in '1p;2,${/^[0-9][0-9][0-9]/p}' my_file to edit my_file directly.

remove lines based on file input pattern using sed

I have been trying to solve a simple sed line deletion problem.
Looked here and there. It didn't solve my problem.
My problem could simply be achieved by using sed -i'{/^1\|^2\|^3/d;}' infile.txt which deletes lines beginning with 1,2 and 3 from the infile.txt.
But what I want instead is to take the starting matching patterns from a file than manually feeding into the stream editor.
E.g: deletePattern
1
3
2
infile.txt
1 Line here
2 Line here
3 Line here
4 Line here
Desired output
4 Line here
Thank you in advance,
This grep should work:
grep -Fvf deletePattern infile.txt
4 Line here
But this will skip a line if patterns in deletePattern are found anywhere in the 2nd file.
More accurate results can be achieved by using this awk command:
awk 'FILENAME == ARGV[1] && FNR==NR{a[$1];next} !($1 in a)' deletePattern infile.txt
4 Line here
Putting together a quick command substitution combined with a character class will allow a relatively short oneliner:
$ sed -e "/^[$( while read -r ch; do a+=$ch; done <pattern.txt; echo "$a" )]/d" infile.txt
4 Line here
Of course, change the -e to -i for actual in-place substitution.
With GNU sed (for -f -):
sed 's!^[0-9][0-9]*$!/^&[^0-9]/d!' deletePattern | sed -f - infile.txt
The first sed transforms deletePattern into a sed script, then the second sed applies this script.
Try this:
sed '/^[123]/ d' infile.txt

sed creates duplicate line instead of replacing existing line

I have a file (foo.txt) containing the following:
some-text 0
I use the following sed-command to replace the 0 with a 1:
search_text="some-text";
sed "s/${search_text} 0/${search_text} 1/" -i foo.txt;
This results in foo.txt containing:
some-text 0
some-text 1
How can I get it to replace the found line instead of appending a new line?
It occurs with GNU sed version 4.2.1 on SL06.
If you like to try awk
awk '/some-text/ {$2=1} 1' file
do you try
search_text='some-text'
sed -e "s/\(${search_text}\) 0/\1 1/" -i foo.txt
using group pattern instead of twice the search_text
which shell are you using (cause i see ; like c end of line not often used on several line in shell) ?

sed: Argument list too long

I have created a script in Unix environment. In the script, I used the sed command as shown below to delete some lines from the file. I want to delete a specified set of lines, not necessarily a simple range, from the file, specified by line numbers.
sed -i "101d; 102d; ... 4930d;" <file_name>
When I execute this it shows the following error:
sed: Arg is too long
Can you please help to resolve this problem?
If you want to delete a contiguous range of lines, you can specify a range of line numbers:
sed -i '101,4930d' file
If you want to delete some arbitrary set of lines that can't easily be expressed as a range, you can put the commands in a file rather than on the command line, and use sed -f.
For example, if foo.sed contains:
2d
4d
6d
8d
10d
then this:
sed -i -f foo.sed file
will delete lines 2, 4, 6, 8, and 10 from file. Putting the commands in a file rather than on the command line avoids limits on command line length.
If there's some pattern to the lines you want to delete, you might consider using a more sophisticated tool such as Awk or Perl.
I had this exact same problem.
I originally put the giant sed command sed -i "101d; 102d; ... 4930d;" <file_name> in a file and tried to execute as a bash script.
To fix - put only the deletion commands in a file and run that file as a sed script. I was able to execute 18,193 deletion commands that had failed to run before.
sed -i -f to_delete.sed input_file
to_delete.sed:
101d;102d;...4930d
With awk:
awk ' NR < 101 || NR > 4930 { print } ' input_file
This might work for you (GNU sed and awk):
cat <<\! >/tmp/a
> 2
> 4
> 6
> 8
> !
seq 10 >/tmp/b
sed 's/$/d/' /tmp/a | sed -f - /tmp/b
1
3
5
7
9
10
awk 'NR==FNR{a[$0];next};FNR in a{next};1' /tmp/{a,b}
1
3
5
7
9
10

Resources